Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init --analyze should return unique image names #3141

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,20 @@ func canonicalizeName(name string) string {
return canonicalized[:253]
}

// deduplicate removes any duplicated values and returns a new slice, keeping the order unchanged
func deduplicate(s []string) []string {
encountered := map[string]bool{}
ret := make([]string, 0)
caulagi marked this conversation as resolved.
Show resolved Hide resolved
for i := range s {
if encountered[s[i]] {
continue
}
encountered[s[i]] = true
ret = append(ret, s[i])
}
return ret
}

func printAnalyzeJSONNoJib(out io.Writer, skipBuild bool, pairs []builderImagePair, unresolvedBuilders []InitBuilder, unresolvedImages []string) error {
if !skipBuild && len(unresolvedBuilders) == 0 {
return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again, or run `skaffold init --skip-build`")
Expand All @@ -470,7 +484,7 @@ func printAnalyzeJSONNoJib(out io.Writer, skipBuild bool, pairs []builderImagePa
a := struct {
Dockerfiles []string `json:"dockerfiles,omitempty"`
Images []string `json:"images,omitempty"`
}{Images: unresolvedImages}
}{Images: deduplicate(unresolvedImages)}

for _, pair := range pairs {
if pair.Builder.Name() == docker.Name {
Expand Down Expand Up @@ -536,6 +550,8 @@ func printAnalyzeJSON(out io.Writer, skipBuild bool, pairs []builderImagePair, u
Images []Image `json:"images,omitempty"`
}{}

seen := make(map[string]bool)

for _, pair := range pairs {
a.Builders = append(a.Builders, Builder{Name: pair.Builder.Name(), Payload: pair.Builder})
a.Images = append(a.Images, Image{Name: pair.ImageName, FoundMatch: true})
Expand All @@ -544,7 +560,10 @@ func printAnalyzeJSON(out io.Writer, skipBuild bool, pairs []builderImagePair, u
a.Builders = append(a.Builders, Builder{Name: config.Name(), Payload: config})
}
for _, image := range unresolvedImages {
a.Images = append(a.Images, Image{Name: image, FoundMatch: false})
if _, ok := seen[image]; !ok {
caulagi marked this conversation as resolved.
Show resolved Hide resolved
a.Images = append(a.Images, Image{Name: image, FoundMatch: false})
seen[image] = true
}
}

contents, err := json.Marshal(a)
Expand Down
12 changes: 12 additions & 0 deletions pkg/skaffold/initializer/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func TestPrintAnalyzeJSON(t *testing.T) {
skipBuild: true,
expected: `{"images":[{"name":"image1","foundMatch":false},{"name":"image2","foundMatch":false}]}`,
},
{
description: "image names should be unique",
images: []string{"image1", "image2", "image1"},
skipBuild: true,
expected: `{"images":[{"name":"image1","foundMatch":false},{"name":"image2","foundMatch":false}]}`,
},
{
description: "no dockerfile",
images: []string{"image1", "image2"},
Expand Down Expand Up @@ -98,6 +104,12 @@ func TestPrintAnalyzeJSONNoJib(t *testing.T) {
skipBuild: true,
expected: `{"images":["image1","image2"]}`,
},
{
description: "image names should be unique",
images: []string{"image1", "image2", "image1"},
skipBuild: true,
expected: `{"images":["image1","image2"]}`,
},
{
description: "no dockerfile",
images: []string{"image1", "image2"},
Expand Down