Skip to content

skaffold init --force supports cases with 1 image and multiple builders #4973

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions pkg/skaffold/initializer/build/builders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ func TestResolveBuilderImages(t *testing.T) {
},
},
},
{
description: "successful force - 1 image 2 builders",
buildConfigs: []InitBuilder{docker.ArtifactConfig{File: "Dockerfile1"}, jib.ArtifactConfig{BuilderName: jib.PluginName(jib.JibGradle), File: "build.gradle"}},
images: []string{"image1"},
shouldMakeChoice: true,
force: true,
expectedPairs: []BuilderImagePair{
{
Builder: docker.ArtifactConfig{File: "Dockerfile1"},
ImageName: "image1",
},
},
},
{
description: "error with ambiguous force",
buildConfigs: []InitBuilder{docker.ArtifactConfig{File: "Dockerfile1"}, jib.ArtifactConfig{BuilderName: jib.PluginName(jib.JibGradle), File: "build.gradle"}},
Expand Down
37 changes: 36 additions & 1 deletion pkg/skaffold/initializer/build/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,47 @@ func (d *defaultBuildInitializer) resolveBuilderImages() error {
}

if d.force {
return errors.BuilderImageAmbiguitiesErr{}
return d.resolveBuilderImagesForcefully()
}

return d.resolveBuilderImagesInteractively()
}

func (d *defaultBuildInitializer) resolveBuilderImagesForcefully() error {
// In the case of 1 image and multiple builders, respects the ordering Docker > Jib > Bazel > Buildpacks
if len(d.unresolvedImages) == 1 {
image := d.unresolvedImages[0]
choice := d.builders[0]
for _, builder := range d.builders {
if builderRank(builder) < builderRank(choice) {
choice = builder
}
}

d.builderImagePairs = append(d.builderImagePairs, BuilderImagePair{Builder: choice, ImageName: image})
d.unresolvedImages = util.RemoveFromSlice(d.unresolvedImages, image)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should unresolvedImages be a map instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps! Could you explain your thoughts on why it should be?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I missed this comment. A map would have been more efficient for this RemoveFromSlice behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could set d.unresolvedImages to empty array instead of calling RemoveFromSlice here.

return nil
}

return errors.BuilderImageAmbiguitiesErr{}
}

func builderRank(builder InitBuilder) int {
a := builder.ArtifactType()
switch {
case a.DockerArtifact != nil:
return 1
case a.JibArtifact != nil:
return 2
case a.BazelArtifact != nil:
return 3
case a.BuildpackArtifact != nil:
return 4
}

return 5
}

func (d *defaultBuildInitializer) resolveBuilderImagesInteractively() error {
// Build map from choice string to builder config struct
choices := make([]string, len(d.builders))
Expand Down