Skip to content

Commit

Permalink
Enable buildpack targets for build
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Kutner <jpkutner@gmail.com>
  • Loading branch information
jkutner committed Apr 10, 2023
1 parent c6cc3ac commit 6831e14
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
14 changes: 12 additions & 2 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
14 changes: 7 additions & 7 deletions pkg/client/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,22 @@ 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)
})

it("prefers config defined run image mirror to stack defined run image mirror", func() {
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")
})
Expand All @@ -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")
})
Expand All @@ -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)
})
Expand All @@ -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")
})
Expand All @@ -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")
})
Expand Down
8 changes: 3 additions & 5 deletions pkg/client/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6831e14

Please sign in to comment.