Skip to content

Commit

Permalink
Consolidate tag stripping logic from Kubernetes logger (#5740)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkubala authored Apr 29, 2021
1 parent 11a27cb commit d1dde02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
15 changes: 3 additions & 12 deletions pkg/skaffold/kubernetes/colorpicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -70,19 +69,11 @@ 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
}
}

// 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]
}
3 changes: 2 additions & 1 deletion pkg/skaffold/kubernetes/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand Down
29 changes: 18 additions & 11 deletions pkg/skaffold/tag/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d1dde02

Please sign in to comment.