From d1dde025e0d1382576151271552d4a9cd125b073 Mon Sep 17 00:00:00 2001 From: Nick Kubala Date: Thu, 29 Apr 2021 11:54:41 -0700 Subject: [PATCH] Consolidate tag stripping logic from Kubernetes logger (#5740) --- pkg/skaffold/kubernetes/colorpicker.go | 15 +++---------- pkg/skaffold/kubernetes/log.go | 3 ++- pkg/skaffold/tag/util.go | 29 ++++++++++++++++---------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/pkg/skaffold/kubernetes/colorpicker.go b/pkg/skaffold/kubernetes/colorpicker.go index d1006f8a000..8c7a92070ab 100644 --- a/pkg/skaffold/kubernetes/colorpicker.go +++ b/pkg/skaffold/kubernetes/colorpicker.go @@ -17,11 +17,10 @@ limitations under the License. package kubernetes import ( - "strings" - v1 "k8s.io/api/core/v1" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/tag" ) var colorCodes = []color.Color{ @@ -57,7 +56,7 @@ func NewColorPicker(imageNames []string) ColorPicker { imageColors := make(map[string]color.Color) for i, imageName := range imageNames { - imageColors[stripTag(imageName)] = colorCodes[i%len(colorCodes)] + imageColors[tag.StripTag(imageName)] = colorCodes[i%len(colorCodes)] } return &colorPicker{ @@ -70,7 +69,7 @@ func NewColorPicker(imageNames []string) ColorPicker { // write with no formatting. func (p *colorPicker) Pick(pod *v1.Pod) color.Color { for _, container := range pod.Spec.Containers { - if c, present := p.imageColors[stripTag(container.Image)]; present { + if c, present := p.imageColors[tag.StripTag(container.Image)]; present { return c } } @@ -78,11 +77,3 @@ func (p *colorPicker) Pick(pod *v1.Pod) color.Color { // If no mapping is found, don't add any color formatting return color.None } - -func stripTag(image string) string { - if !strings.Contains(image, ":") { - return image - } - - return strings.SplitN(image, ":", 2)[0] -} diff --git a/pkg/skaffold/kubernetes/log.go b/pkg/skaffold/kubernetes/log.go index 8a39608c6c7..b25fcde70c3 100644 --- a/pkg/skaffold/kubernetes/log.go +++ b/pkg/skaffold/kubernetes/log.go @@ -31,6 +31,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl" latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/tag" ) // LogAggregator aggregates the logs for all the deployed pods. @@ -183,7 +184,7 @@ func (a *LogAggregator) prefix(pod *v1.Pod, container v1.ContainerStatus) string var c latest_v1.Pipeline var present bool for _, container := range pod.Spec.Containers { - if c, present = a.config.PipelineForImage(stripTag(container.Image)); present { + if c, present = a.config.PipelineForImage(tag.StripTag(container.Image)); present { break } } diff --git a/pkg/skaffold/tag/util.go b/pkg/skaffold/tag/util.go index 2786bbe008b..72e11710f9b 100644 --- a/pkg/skaffold/tag/util.go +++ b/pkg/skaffold/tag/util.go @@ -25,22 +25,29 @@ func StripTags(taggedImages []string) []string { // Remove tags from image names var images []string for _, image := range taggedImages { - parsed, err := docker.ParseReference(image) - if err != nil { - // It's possible that it's a templatized name that can't be parsed as is. - warnings.Printf("Couldn't parse image [%s]: %s", image, err.Error()) - continue + tag := StripTag(image) + if tag != "" { + images = append(images, tag) } - if parsed.Digest != "" { - warnings.Printf("Ignoring image referenced by digest: [%s]", image) - continue - } - - images = append(images, parsed.BaseName) } return images } +func StripTag(image string) string { + parsed, err := docker.ParseReference(image) + if err != nil { + // It's possible that it's a templatized name that can't be parsed as is. + warnings.Printf("Couldn't parse image [%s]: %s", image, err.Error()) + return "" + } + if parsed.Digest != "" { + warnings.Printf("Ignoring image referenced by digest: [%s]", image) + return "" + } + + return parsed.BaseName +} + func SetImageTag(image, tag string) (string, error) { parsed, err := docker.ParseReference(image) if err != nil {