diff --git a/internal/builder/builder.go b/internal/builder/builder.go index f6d9851ec8..6bd459e33f 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -236,9 +236,19 @@ func (b *Builder) Stack() StackMetadata { return b.metadata.Stack } -// RunImages returns the run image metadata +// RunImages returns all run image metadata func (b *Builder) RunImages() []RunImageMetadata { - return b.metadata.RunImages + return append(b.metadata.RunImages, b.Stack().RunImage) +} + +// DefaultRunImage returns the default run image metadata +func (b *Builder) DefaultRunImage() RunImageMetadata { + // run.images are ensured in builder.ValidateConfig() + // per the spec, we use the first one as the default + if len(b.metadata.RunImages) == 0 { + return b.Stack().RunImage + } + return b.metadata.RunImages[0] } // Mixins returns the mixins of the builder diff --git a/pkg/client/build.go b/pkg/client/build.go index 1c1b06989f..5134c29985 100644 --- a/pkg/client/build.go +++ b/pkg/client/build.go @@ -323,7 +323,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error { return errors.Wrapf(err, "invalid builder %s", style.Symbol(opts.Builder)) } - runImageName := c.resolveRunImage(opts.RunImage, imgRegistry, builderRef.Context().RegistryStr(), bldr.Stack(), opts.AdditionalMirrors, opts.Publish) + runImageName := c.resolveRunImage(opts.RunImage, imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish) fetchOptions := image.FetchOptions{Daemon: !opts.Publish, PullPolicy: opts.PullPolicy} if opts.Layout() { @@ -610,7 +610,7 @@ func (c *Client) getBuilder(img imgutil.Image) (*builder.Builder, error) { if err != nil { return nil, err } - if bldr.Stack().RunImage.Image == "" { + if bldr.Stack().RunImage.Image == "" && len(bldr.RunImages()) == 0 { return nil, errors.New("builder metadata is missing run-image") } diff --git a/pkg/client/common.go b/pkg/client/common.go index b526edd95c..d6b4901cda 100644 --- a/pkg/client/common.go +++ b/pkg/client/common.go @@ -28,7 +28,7 @@ func (c *Client) parseTagReference(imageName string) (name.Reference, error) { return ref, nil } -func (c *Client) resolveRunImage(runImage, imgRegistry, bldrRegistry string, stackInfo builder.StackMetadata, additionalMirrors map[string][]string, publish bool) string { +func (c *Client) resolveRunImage(runImage, imgRegistry, bldrRegistry string, runImageMetadata builder.RunImageMetadata, additionalMirrors map[string][]string, publish bool) string { if runImage != "" { c.logger.Debugf("Using provided run-image %s", style.Symbol(runImage)) return runImage @@ -41,15 +41,15 @@ func (c *Client) resolveRunImage(runImage, imgRegistry, bldrRegistry string, sta runImageName := getBestRunMirror( preferredRegistry, - stackInfo.RunImage.Image, - stackInfo.RunImage.Mirrors, - additionalMirrors[stackInfo.RunImage.Image], + runImageMetadata.Image, + runImageMetadata.Mirrors, + additionalMirrors[runImageMetadata.Image], ) switch { - case runImageName == stackInfo.RunImage.Image: + case runImageName == runImageMetadata.Image: c.logger.Debugf("Selected run image %s", style.Symbol(runImageName)) - case contains(stackInfo.RunImage.Mirrors, runImageName): + case contains(runImageMetadata.Mirrors, runImageName): c.logger.Debugf("Selected run image mirror %s", style.Symbol(runImageName)) default: c.logger.Debugf("Selected run image mirror %s from local config", style.Symbol(runImageName)) diff --git a/pkg/client/common_test.go b/pkg/client/common_test.go index 732c324b73..c52b1a37ac 100644 --- a/pkg/client/common_test.go +++ b/pkg/client/common_test.go @@ -59,14 +59,14 @@ func testCommon(t *testing.T, when spec.G, it spec.S) { when("passed specific run image", func() { it("selects that run image", func() { runImgFlag := "flag/passed-run-image" - runImageName := subject.resolveRunImage(runImgFlag, defaultRegistry, "", stackInfo, nil, false) + runImageName := subject.resolveRunImage(runImgFlag, defaultRegistry, "", stackInfo.RunImage, nil, false) assert.Equal(runImageName, runImgFlag) }) }) when("publish is true", func() { it("defaults to run-image in registry publishing to", func() { - runImageName := subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo, nil, true) + runImageName := subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo.RunImage, nil, true) assert.Equal(runImageName, gcrRunMirror) }) @@ -74,7 +74,7 @@ func testCommon(t *testing.T, when spec.G, it spec.S) { configMirrors := map[string][]string{ runImageName: {defaultRegistry + "/unique-run-img"}, } - runImageName := subject.resolveRunImage("", defaultRegistry, "", stackInfo, configMirrors, true) + runImageName := subject.resolveRunImage("", defaultRegistry, "", stackInfo.RunImage, configMirrors, true) assert.NotEqual(runImageName, defaultMirror) assert.Equal(runImageName, defaultRegistry+"/unique-run-img") }) @@ -83,7 +83,7 @@ func testCommon(t *testing.T, when spec.G, it spec.S) { configMirrors := map[string][]string{ runImageName: {defaultRegistry + "/unique-run-img"}, } - runImageName := subject.resolveRunImage("", "test.registry.io", "", stackInfo, configMirrors, true) + runImageName := subject.resolveRunImage("", "test.registry.io", "", stackInfo.RunImage, configMirrors, true) assert.NotEqual(runImageName, defaultMirror) assert.Equal(runImageName, defaultRegistry+"/unique-run-img") }) @@ -92,7 +92,7 @@ func testCommon(t *testing.T, when spec.G, it spec.S) { // If publish is false, we are using the local daemon, and want to match to the builder registry when("publish is false", func() { it("defaults to run-image in registry publishing to", func() { - runImageName := subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo, nil, false) + runImageName := subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo.RunImage, nil, false) assert.Equal(runImageName, defaultMirror) assert.NotEqual(runImageName, gcrRunMirror) }) @@ -101,7 +101,7 @@ func testCommon(t *testing.T, when spec.G, it spec.S) { configMirrors := map[string][]string{ runImageName: {defaultRegistry + "/unique-run-img"}, } - runImageName := subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo, configMirrors, false) + runImageName := subject.resolveRunImage("", gcrRegistry, defaultRegistry, stackInfo.RunImage, configMirrors, false) assert.NotEqual(runImageName, defaultMirror) assert.Equal(runImageName, defaultRegistry+"/unique-run-img") }) @@ -110,7 +110,7 @@ func testCommon(t *testing.T, when spec.G, it spec.S) { configMirrors := map[string][]string{ runImageName: {defaultRegistry + "/unique-run-img"}, } - runImageName := subject.resolveRunImage("", defaultRegistry, "test.registry.io", stackInfo, configMirrors, false) + runImageName := subject.resolveRunImage("", defaultRegistry, "test.registry.io", stackInfo.RunImage, configMirrors, false) assert.NotEqual(runImageName, defaultMirror) assert.Equal(runImageName, defaultRegistry+"/unique-run-img") }) diff --git a/pkg/client/rebase.go b/pkg/client/rebase.go index 03dbebbe41..8035f997e7 100644 --- a/pkg/client/rebase.go +++ b/pkg/client/rebase.go @@ -70,11 +70,9 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error { opts.RunImage, imageRef.Context().RegistryStr(), "", - builder.StackMetadata{ - RunImage: builder.RunImageMetadata{ - Image: md.Stack.RunImage.Image, - Mirrors: md.Stack.RunImage.Mirrors, - }, + builder.RunImageMetadata{ + Image: md.Stack.RunImage.Image, + Mirrors: md.Stack.RunImage.Mirrors, }, opts.AdditionalMirrors, opts.Publish)