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 all 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
7 changes: 7 additions & 0 deletions docs/content/en/docs/pipeline-stages/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ When overlay directories are found, these will be listed in the generated Skaffo

*Note: order is guaranteed, since Skaffold's directory parsing is always deterministic.*

## `--force` Flag
`skaffold init` allows for use of a `--force` flag, which removes the prompts from vanilla `skaffold init`, and allows skaffold to make a best effort attempt to automatically generate a config for your project.

In a situation where one image is detected, but multiple possible builders are detected, skaffold will choose a builder as follows: Docker > Jib > Bazel > Buildpacks.

*Note: This feature is still under development, and doesn't currently support use cases such as multiple images in a project.*

## Init API
`skaffold init` also exposes an API which tools like IDEs can integrate with via flags.

Expand Down
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 = []string{}
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