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

Update run image metadata when --run-image is provided #2022

Merged
merged 6 commits into from
Apr 9, 2024
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
41 changes: 33 additions & 8 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,11 @@ func testAcceptance(
bpSimpleLayersDiffID := "sha256:ade9da86859fa4ea50a513757f9b242bf1038667abf92dad3d018974a17f0ea7"
bpReadEnvDiffID := "sha256:db0797077ba8deff7054ab5578133b8f0206b6393de34b5bfd795cf50f6afdbd"
// extensions
assertImage.HasLabelWithData(builderName, "io.buildpacks.extension.layers", `{"read/env":{"read-env-version":{"api":"0.9","layerDiffID":"`+extReadEnvDiffID+`","name":"Read Env Extension"}},"simple/layers":{"simple-layers-version":{"api":"0.7","layerDiffID":"`+extSimpleLayersDiffID+`","name":"Simple Layers Extension"}}}`)
assertImage.HasLabelWithData(builderName, "io.buildpacks.buildpack.order-extensions", `[{"group":[{"id":"read/env","version":"read-env-version"},{"id":"simple/layers","version":"simple-layers-version"}]}]`)
assertImage.HasLabelContaining(builderName, "io.buildpacks.extension.layers", `{"read/env":{"read-env-version":{"api":"0.9","layerDiffID":"`+extReadEnvDiffID+`","name":"Read Env Extension"}},"simple/layers":{"simple-layers-version":{"api":"0.7","layerDiffID":"`+extSimpleLayersDiffID+`","name":"Simple Layers Extension"}}}`)
assertImage.HasLabelContaining(builderName, "io.buildpacks.buildpack.order-extensions", `[{"group":[{"id":"read/env","version":"read-env-version"},{"id":"simple/layers","version":"simple-layers-version"}]}]`)
// buildpacks
assertImage.HasLabelWithData(builderName, "io.buildpacks.buildpack.layers", `{"read/env":{"read-env-version":{"api":"0.7","stacks":[{"id":"pack.test.stack"}],"layerDiffID":"`+bpReadEnvDiffID+`","name":"Read Env Buildpack"}},"simple/layers":{"simple-layers-version":{"api":"0.7","stacks":[{"id":"pack.test.stack"}],"layerDiffID":"`+bpSimpleLayersDiffID+`","name":"Simple Layers Buildpack"}}}`)
assertImage.HasLabelWithData(builderName, "io.buildpacks.buildpack.order", `[{"group":[{"id":"read/env","version":"read-env-version","optional":true},{"id":"simple/layers","version":"simple-layers-version","optional":true}]}]`)
assertImage.HasLabelContaining(builderName, "io.buildpacks.buildpack.layers", `{"read/env":{"read-env-version":{"api":"0.7","stacks":[{"id":"pack.test.stack"}],"layerDiffID":"`+bpReadEnvDiffID+`","name":"Read Env Buildpack"}},"simple/layers":{"simple-layers-version":{"api":"0.7","stacks":[{"id":"pack.test.stack"}],"layerDiffID":"`+bpSimpleLayersDiffID+`","name":"Simple Layers Buildpack"}}}`)
assertImage.HasLabelContaining(builderName, "io.buildpacks.buildpack.order", `[{"group":[{"id":"read/env","version":"read-env-version","optional":true},{"id":"simple/layers","version":"simple-layers-version","optional":true}]}]`)
}
})

Expand Down Expand Up @@ -1063,10 +1063,10 @@ func testAcceptance(
assertImage.HasBaseImage(repoName, runImage)

t.Log("sets the run image metadata")
assertImage.HasLabelWithData(repoName, "io.buildpacks.lifecycle.metadata", fmt.Sprintf(`"image":"pack-test/run","mirrors":["%s"]`, runImageMirror))
assertImage.HasLabelContaining(repoName, "io.buildpacks.lifecycle.metadata", fmt.Sprintf(`"image":"pack-test/run","mirrors":["%s"]`, runImageMirror))

t.Log("sets the source metadata")
assertImage.HasLabelWithData(repoName, "io.buildpacks.project.metadata", (`{"source":{"type":"project","version":{"declared":"1.0.2"},"metadata":{"url":"https://github.com/buildpacks/pack"}}}`))
assertImage.HasLabelContaining(repoName, "io.buildpacks.project.metadata", (`{"source":{"type":"project","version":{"declared":"1.0.2"},"metadata":{"url":"https://github.com/buildpacks/pack"}}}`))

t.Log("registry is empty")
assertImage.NotExistsInRegistry(repo)
Expand All @@ -1085,6 +1085,12 @@ func testAcceptance(
assertOutput = assertions.NewOutputAssertionManager(t, output)
assertOutput.ReportsSuccessfulImageBuild(repoName)
assertOutput.ReportsSelectingRunImageMirrorFromLocalConfig(localRunImageMirror)
if pack.SupportsFeature(invoke.FixesRunImageMetadata) {
t.Log(fmt.Sprintf("run-image mirror %s was NOT added into 'io.buildpacks.lifecycle.metadata' label", localRunImageMirror))
assertImage.HasLabelNotContaining(repoName, "io.buildpacks.lifecycle.metadata", fmt.Sprintf(`"image":"%s"`, localRunImageMirror))
t.Log(fmt.Sprintf("run-image %s was added into 'io.buildpacks.lifecycle.metadata' label", runImage))
assertImage.HasLabelContaining(repoName, "io.buildpacks.lifecycle.metadata", fmt.Sprintf(`"image":"%s"`, runImage))
}
cachedLaunchLayer := "simple/layers:cached-launch-layer"

assertLifecycleOutput := assertions.NewLifecycleOutputAssertionManager(t, output)
Expand Down Expand Up @@ -1807,6 +1813,10 @@ func testAcceptance(

t.Log("uses the run image as the base image")
assertImage.HasBaseImage(repoName, runImageName)
if pack.SupportsFeature(invoke.FixesRunImageMetadata) {
t.Log(fmt.Sprintf("run-image %s was added into 'io.buildpacks.lifecycle.metadata' label", runImageName))
assertImage.HasLabelContaining(repoName, "io.buildpacks.lifecycle.metadata", fmt.Sprintf(`"image":"%s"`, runImageName))
}
})
})

Expand Down Expand Up @@ -2842,6 +2852,14 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
var localRunImageMirror string

it.Before(func() {
imageManager.CleanupImages(repoName)
pack.RunSuccessfully(
"build", repoName,
"-p", filepath.Join("testdata", "mock_app"),
"--builder", builderName,
"--pull-policy", "never",
)

localRunImageMirror = registryConfig.RepoName("run-after/" + h.RandString(10))
buildRunImage(localRunImageMirror, "local-mirror-after-1", "local-mirror-after-2")
pack.JustRunSuccessfully("config", "run-image-mirrors", "add", runImage, "-m", localRunImageMirror)
Expand Down Expand Up @@ -2872,9 +2890,16 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
when("image metadata has a mirror", func() {
it.Before(func() {
// clean up existing mirror first to avoid leaking images
imageManager.CleanupImages(runImageMirror)

imageManager.CleanupImages(runImageMirror, repoName)
buildRunImage(runImageMirror, "mirror-after-1", "mirror-after-2")

pack.RunSuccessfully(
"build", repoName,
"-p", filepath.Join("testdata", "mock_app"),
"--builder", builderName,
"--pull-policy", "never",
)

})

it("selects the best mirror", func() {
Expand Down
11 changes: 10 additions & 1 deletion acceptance/assertions/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a ImageAssertionManager) HasCreateTime(image string, expectedTime time.Tim
a.assert.TrueWithMessage(actualTime.Sub(expectedTime) < 5*time.Second && expectedTime.Sub(actualTime) < 5*time.Second, fmt.Sprintf("expected image create time %s to match expected time %s", actualTime, expectedTime))
}

func (a ImageAssertionManager) HasLabelWithData(image, label, data string) {
func (a ImageAssertionManager) HasLabelContaining(image, label, data string) {
a.testObject.Helper()
inspect, err := a.imageManager.InspectLocal(image)
a.assert.Nil(err)
Expand All @@ -69,6 +69,15 @@ func (a ImageAssertionManager) HasLabelWithData(image, label, data string) {
a.assert.Contains(label, data)
}

func (a ImageAssertionManager) HasLabelNotContaining(image, label, data string) {
a.testObject.Helper()
inspect, err := a.imageManager.InspectLocal(image)
a.assert.Nil(err)
label, ok := inspect.Config.Labels[label]
a.assert.TrueWithMessage(ok, fmt.Sprintf("expected label %s to exist", label))
a.assert.NotContains(label, data)
}

func (a ImageAssertionManager) HasLengthLayers(image string, length int) {
a.testObject.Helper()
inspect, err := a.imageManager.InspectLocal(image)
Expand Down
4 changes: 4 additions & 0 deletions acceptance/invoke/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ const (
MetaBuildpackFolder
PlatformRetries
FlattenBuilderCreationV2
FixesRunImageMetadata
)

var featureTests = map[Feature]func(i *PackInvoker) bool{
Expand Down Expand Up @@ -270,6 +271,9 @@ var featureTests = map[Feature]func(i *PackInvoker) bool{
FlattenBuilderCreationV2: func(i *PackInvoker) bool {
return i.atLeast("v0.33.1")
},
FixesRunImageMetadata: func(i *PackInvoker) bool {
return i.atLeast("v0.34.0")
},
}

func (i *PackInvoker) SupportsFeature(f Feature) bool {
Expand Down
15 changes: 15 additions & 0 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ type BuilderOption func(*options) error
type options struct {
toFlatten buildpack.FlattenModuleInfos
labels map[string]string
runImage string
}

func WithRunImage(name string) BuilderOption {
return func(o *options) error {
o.runImage = name
return nil
}
}

// FromImage constructs a builder from a builder image
Expand Down Expand Up @@ -140,6 +148,13 @@ func constructBuilder(img imgutil.Image, newName string, errOnMissingLabel bool,
return nil, err
}

if opts.runImage != "" {
// Do we need to look for available mirrors? for now the mirrors are gone if you override the run-image
// create an issue if you want to preserve the mirrors
metadata.RunImages = []RunImageMetadata{{Image: opts.runImage}}
metadata.Stack.RunImage = RunImageMetadata{Image: opts.runImage}
}

for labelKey, labelValue := range opts.labels {
err = img.SetLabel(labelKey, labelValue)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions internal/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,32 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
})
})
})

when("#New", func() {
when("#WithRunImage", func() {
// Current runImage information in builder image:
// "stack": {"runImage": {"image": "prev/run", "mirrors": ["prev/mirror"]}}
var newBuilder *builder.Builder
newRunImage := "another/run"

it.Before(func() {
var err error
newBuilder, err = builder.New(builderImage, "newBuilder/image", builder.WithRunImage(newRunImage))
h.AssertNil(t, err)
})

it("overrides the run image metadata (which becomes run.toml)", func() {
// RunImages() returns Stacks + RunImages metadata.
metadata := newBuilder.RunImages()
h.AssertTrue(t, len(metadata) == 2)
for _, m := range metadata {
// Both images must be equal to the expected run-image
h.AssertEq(t, m.Image, newRunImage)
h.AssertEq(t, len(m.Mirrors), 0)
}
})
})
})
})

when("flatten", func() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
buildEnvs[k] = v
}

ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, buildEnvs, order, fetchedBPs, orderExtensions, fetchedExs, usingPlatformAPI.LessThan("0.12"))
ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, buildEnvs, order, fetchedBPs, orderExtensions, fetchedExs, usingPlatformAPI.LessThan("0.12"), opts.RunImage)
if err != nil {
return err
}
Expand Down Expand Up @@ -1328,9 +1328,10 @@ func (c *Client) createEphemeralBuilder(
orderExtensions dist.Order,
extensions []buildpack.BuildModule,
validateMixins bool,
runImage string,
) (*builder.Builder, error) {
origBuilderName := rawBuilderImage.Name()
bldr, err := builder.New(rawBuilderImage, fmt.Sprintf("pack.local/builder/%x:latest", randString(10)))
bldr, err := builder.New(rawBuilderImage, fmt.Sprintf("pack.local/builder/%x:latest", randString(10)), builder.WithRunImage(runImage))
if err != nil {
return nil, errors.Wrapf(err, "invalid builder %s", style.Symbol(origBuilderName))
}
Expand Down
Loading