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

Relocate the run image to the builder repo #1533

Merged
merged 1 commit into from
Mar 4, 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
6 changes: 4 additions & 2 deletions pkg/cnb/builder_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func newBuilderBldr(kpackVersion string) *builderBlder {
kpackVersion: kpackVersion,
}
}

func (bb *builderBlder) AddStack(baseImage v1.Image, clusterStack *buildapi.ClusterStack) error {
file, err := baseImage.ConfigFile()
if err != nil {
Expand All @@ -70,7 +69,6 @@ func (bb *builderBlder) AddStack(baseImage v1.Image, clusterStack *buildapi.Clus
bb.os = file.OS
bb.baseImage = baseImage
bb.stackId = clusterStack.Status.Id
bb.runImage = clusterStack.Status.RunImage.Image
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is clusterStack.Status.RunImage.Image fully qualified?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ya it's the resolved image

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like it just the spec runImage which is not necessarily fully qualified.

RunImage: buildapi.ClusterStackStatusImage{
LatestImage: runIdentifier,
Image: clusterStackSpec.RunImage.Image,
},

Copy link
Collaborator

@matthewmcnew matthewmcnew Feb 21, 2024

Choose a reason for hiding this comment

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

I think that was intentional. The run image shouldn't necessarily be fully qualified. (E.g if you are using the builder with pack and the run image was updated it is useful to make use of that run image)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suppose here, it needs to be relocated by kpack so, it is not necessarily useful.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Which I guess exposes the downside with this approach. It is only really useful if the source of the stack is not available otherwise, it provides a worse experience.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ya I guess the trade off is full build reproducibility though which a floating tag doesn't guarantee

bb.mixins = clusterStack.Status.Mixins
bb.cnbUserId = clusterStack.Status.UserID
bb.cnbGroupId = clusterStack.Status.GroupID
Expand Down Expand Up @@ -183,6 +181,10 @@ func (bb *builderBlder) WriteableImage() (v1.Image, error) {
})
}

func (bb *builderBlder) AddRunImage(runImage string) {
bb.runImage = runImage
}

func (bb *builderBlder) validateBuilder(sortedBuildpacks []DescriptiveBuildpackInfo) error {
platformApis := append(bb.LifecycleMetadata.APIs.Platform.Deprecated, bb.LifecycleMetadata.APIs.Platform.Supported...)
err := validatePlatformApis(platformApis)
Expand Down
30 changes: 25 additions & 5 deletions pkg/cnb/create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cnb

import (
"context"
"fmt"

"github.com/google/go-containerregistry/pkg/authn"
ggcrv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/pivotal/kpack/pkg/cosign"
corev1 "k8s.io/api/core/v1"

buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
"github.com/pivotal/kpack/pkg/cosign"
"github.com/pivotal/kpack/pkg/registry"
)

Expand All @@ -30,15 +31,34 @@ type RemoteBuilderCreator struct {
ImageSigner cosign.BuilderSigner
}

func (r *RemoteBuilderCreator) CreateBuilder(ctx context.Context, builderKeychain authn.Keychain, stackKeychain authn.Keychain, fetcher RemoteBuildpackFetcher, clusterStack *buildapi.ClusterStack, spec buildapi.BuilderSpec, serviceAccountSecrets []*corev1.Secret) (buildapi.BuilderRecord, error) {
buildImage, _, err := r.RegistryClient.Fetch(stackKeychain, clusterStack.Status.BuildImage.LatestImage)
func (r *RemoteBuilderCreator) CreateBuilder(
ctx context.Context,
builderKeychain authn.Keychain,
stackKeychain authn.Keychain,
fetcher RemoteBuildpackFetcher,
clusterStack *buildapi.ClusterStack,
spec buildapi.BuilderSpec,
serviceAccountSecrets []*corev1.Secret,
resolvedBuilderRef string,
) (buildapi.BuilderRecord, error) {

buildImage, _, err := r.RegistryClient.Fetch(stackKeychain, clusterStack.Status.BuildImage.LatestImage)
if err != nil {
return buildapi.BuilderRecord{}, err
}
runImage, _, err := r.RegistryClient.Fetch(stackKeychain, clusterStack.Status.RunImage.LatestImage)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to only save the image if it is in a different registry than the builder?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was thinking you might still run into a case where the pull rules could be different for a different repo

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair. There is a trade-off from having the run-image be relocated everywhere, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I'm ok with just checking the registry and saying you need to be able to pull both. Maybe putting the original image in the run image mirror would also help as a backup

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thinking more about this, i'm seeing less of an issue with always relocating. In the event that the run image is in the same registry we will just cross repo blob mount. It will also tag the run image to prevent it from being being cleaned up. If the run image is in a different repo the value is much more apparent since you don't have to worry about run image access.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking the downside is more about more confusing provenance. It is useful to see that the run image is my-registry.com/my-stack/specific-stack-name/run vs my-registry.com/some-team/custom-builder/run.

but, I agree it is not a fatal flaw.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ya that is definitely a concern, but I was hoping the tags on the relocated run image would simplify this. It also makes registry management a bit easier so you can easily see the run image associated with a particular builder

if err != nil {
return buildapi.BuilderRecord{}, err
}

builderBldr := newBuilderBldr(r.KpackVersion)

relocatedRunImage, err := r.RegistryClient.Save(builderKeychain, fmt.Sprintf("%s-run-image", resolvedBuilderRef), runImage)
if err != nil {
return buildapi.BuilderRecord{}, err
}
builderBldr.AddRunImage(relocatedRunImage)

err = builderBldr.AddStack(buildImage, clusterStack)
if err != nil {
return buildapi.BuilderRecord{}, err
Expand Down Expand Up @@ -72,7 +92,7 @@ func (r *RemoteBuilderCreator) CreateBuilder(ctx context.Context, builderKeychai
return buildapi.BuilderRecord{}, err
}

identifier, err := r.RegistryClient.Save(builderKeychain, spec.Tag, writeableImage)
identifier, err := r.RegistryClient.Save(builderKeychain, resolvedBuilderRef, writeableImage)
if err != nil {
return buildapi.BuilderRecord{}, err
}
Expand All @@ -96,7 +116,7 @@ func (r *RemoteBuilderCreator) CreateBuilder(ctx context.Context, builderKeychai
builder := buildapi.BuilderRecord{
Image: identifier,
Stack: corev1alpha1.BuildStack{
RunImage: clusterStack.Status.RunImage.LatestImage,
RunImage: relocatedRunImage,
ID: clusterStack.Status.Id,
},
Buildpacks: buildpackMetadata(builderBldr.buildpacks()),
Expand Down
85 changes: 54 additions & 31 deletions pkg/cnb/create_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
const (
stackID = "io.buildpacks.stacks.some-stack"
mixin = "some-mixin"
tag = "custom/example"
builderTag = "custom/example:test-builder"
buildImage = "index.docker.io/paketo-buildpacks/build@sha256:d19308ce0c1a9ec083432b2c850d615398f0c6a51095d589d58890a721925584"
runImage = "index.docker.io/paketo-buildpacks/run@sha256:469f092c28ab64c6798d6f5e24feb4252ae5b36c2ed79cc667ded85ffb49d996"
relocatedRunImageTag = "custom/example:test-builder-run-image"
buildImageTag = "paketo-buildpacks/build:full-cnb"
runImageTag = "paketo-buildpacks/run:full-cnb"
buildImageLayers = 10
Expand All @@ -60,8 +60,10 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
builderKeychain = authn.NewMultiKeychain(authn.DefaultKeychain)
stackKeychain = authn.NewMultiKeychain(authn.DefaultKeychain)
secretRef = registry.SecretRef{}

ctx = context.Background()
runImage = createRunImage(os)
runImageDigest = digest(runImage)
runImageRef = fmt.Sprintf("%s@%s", runImageTag, runImageDigest)
ctx = context.Background()

fetcher = &fakeFetcher{buildpacks: map[string][]buildpackLayer{}, observedGeneration: 10}

Expand Down Expand Up @@ -117,7 +119,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
Image: buildImageTag,
},
RunImage: buildapi.ClusterStackStatusImage{
LatestImage: runImage,
LatestImage: runImageRef,
Image: runImageTag,
},
Mixins: []string{"some-unused-mixin", mixin, "common-mixin", "build:another-common-mixin", "run:another-common-mixin"},
Expand All @@ -128,7 +130,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}

clusterBuilderSpec = buildapi.BuilderSpec{
Tag: "custom/example",
Tag: builderTag,
Stack: corev1.ObjectReference{
Kind: "stack",
Name: "some-stack",
Expand Down Expand Up @@ -309,7 +311,8 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
fetcher.AddBuildpack(t, "io.buildpack.4", "v4", []buildpackLayer{buildpackWithDuplicatLayer})
})

registryClient.AddSaveKeychain("custom/example", builderKeychain)
registryClient.AddSaveKeychain(builderTag, builderKeychain)
registryClient.AddImage(runImageRef, runImage, stackKeychain)

when("CreateBuilder", func() {
var (
Expand Down Expand Up @@ -355,16 +358,16 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}
})

it("creates a custom builder", func() {
builderRecord, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
it("creates a custom builder with a relocated run image", func() {
builderRecord, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.NoError(t, err)

assert.Len(t, builderRecord.Buildpacks, 4)
assert.Contains(t, builderRecord.Buildpacks, corev1alpha1.BuildpackMetadata{Id: "io.buildpack.1", Version: "v1", Homepage: "buildpack.1.com"})
assert.Contains(t, builderRecord.Buildpacks, corev1alpha1.BuildpackMetadata{Id: "io.buildpack.2", Version: "v2", Homepage: "buildpack.2.com"})
assert.Contains(t, builderRecord.Buildpacks, corev1alpha1.BuildpackMetadata{Id: "io.buildpack.3", Version: "v3", Homepage: "buildpack.3.com"})
assert.Contains(t, builderRecord.Buildpacks, corev1alpha1.BuildpackMetadata{Id: "io.buildpack.4", Version: "v4", Homepage: "buildpack.4.com"})
assert.Equal(t, corev1alpha1.BuildStack{RunImage: runImage, ID: stackID}, builderRecord.Stack)
assert.Equal(t, corev1alpha1.BuildStack{RunImage: fmt.Sprintf("%s@%s", relocatedRunImageTag, runImageDigest), ID: stackID}, builderRecord.Stack)
assert.Equal(t, int64(10), builderRecord.ObservedStoreGeneration)
assert.Equal(t, int64(11), builderRecord.ObservedStackGeneration)
assert.Equal(t, os, builderRecord.OS)
Expand All @@ -388,16 +391,20 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
},
})

assert.Len(t, registryClient.SavedImages(), 1)
savedImage := registryClient.SavedImages()[tag]
assert.Len(t, registryClient.SavedImages(), 2)
savedImage := registryClient.SavedImages()[builderTag]
require.Contains(t, registryClient.SavedImages(), relocatedRunImageTag)
digest, err := registryClient.SavedImages()[relocatedRunImageTag].Digest()
require.NoError(t, err)
require.Equal(t, digest.String(), runImageDigest)

workingDir, err := imagehelpers.GetWorkingDir(savedImage)
require.NoError(t, err)
assert.Equal(t, "/layers", workingDir)

hash, err := savedImage.Digest()
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("%s@%s", tag, hash), builderRecord.Image)
assert.Equal(t, fmt.Sprintf("%s@%s", builderTag, hash), builderRecord.Image)

layers, err := savedImage.Layers()
require.NoError(t, err)
Expand Down Expand Up @@ -487,9 +494,9 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
typeflag: tar.TypeReg,
mode: 0644,
fileContent: //language=toml
`[run-image]
image = "paketo-buildpacks/run:full-cnb"
`,
fmt.Sprintf(`[run-image]
image = "%s@%s"
`, relocatedRunImageTag, runImageDigest),
},
})
})
Expand Down Expand Up @@ -528,11 +535,11 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
buildpackMetadata, err := imagehelpers.GetStringLabel(savedImage, buildpackMetadataLabel)
assert.NoError(t, err)
assert.JSONEq(t, //language=json
`{
fmt.Sprintf(`{
"description": "Custom Builder built with kpack",
"stack": {
"runImage": {
"image": "paketo-buildpacks/run:full-cnb",
"image": "%s@%s",
"mirrors": null
}
},
Expand Down Expand Up @@ -579,7 +586,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
"homepage": "buildpack.1.com"
}
]
}`, buildpackMetadata)
}`, relocatedRunImageTag, runImageDigest), buildpackMetadata)

buildpackLayers, err := imagehelpers.GetStringLabel(savedImage, buildpackLayersLabel)
assert.NoError(t, err)
Expand Down Expand Up @@ -653,11 +660,11 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
})

it("creates images deterministically ", func() {
original, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
original, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.NoError(t, err)

for i := 1; i <= 50; i++ {
other, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
other, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)

require.NoError(t, err)

Expand Down Expand Up @@ -688,7 +695,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
},
}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.EqualError(t, err, "validating buildpack io.buildpack.unsupported.stack@v4: stack io.buildpacks.stacks.some-stack is not supported")
})

Expand All @@ -712,7 +719,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}},
}}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.EqualError(t, err, "validating buildpack io.buildpack.unsupported.mixin@v4: stack missing mixin(s): something-missing-mixin, something-missing-mixin2")
})

Expand Down Expand Up @@ -757,7 +764,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}},
}}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.Nil(t, err)
})

Expand All @@ -782,7 +789,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}},
}}

_, err := subject.CreateBuilder(ctx, builderKeychain, nil, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, nil, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.Error(t, err, "validating buildpack io.buildpack.relaxed.old.mixin@v4: stack missing mixin(s): build:common-mixin, run:common-mixin, another-common-mixin")
})

Expand All @@ -805,7 +812,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}},
}}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.EqualError(t, err, "validating buildpack io.buildpack.unsupported.buildpack.api@v4: unsupported buildpack api: 0.1, expecting: 0.2, 0.3")
})

Expand Down Expand Up @@ -848,7 +855,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
}},
}}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.NoError(t, err)
})
})
Expand All @@ -875,14 +882,14 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
},
}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.EqualError(t, err, "unsupported platform apis in kpack lifecycle: 0.1, 0.2, 0.999, expecting one of: 0.3, 0.4, 0.5, 0.6, 0.7, 0.8")
})
})

when("signing a builder image", func() {
it("does not populate the signature paths when no secrets were present", func() {
builderRecord, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{})
builderRecord, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{}, builderTag)
require.NoError(t, err)
require.NotNil(t, builderRecord)
require.Empty(t, builderRecord.SignaturePaths)
Expand All @@ -902,7 +909,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
},
}

_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{&fakeSecret})
_, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{&fakeSecret}, builderTag)
require.Error(t, err)
})

Expand All @@ -925,7 +932,7 @@ func testCreateBuilderOs(os string, t *testing.T, when spec.G, it spec.S) {
},
}

builderRecord, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{&fakeSecret})
builderRecord, err := subject.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, stack, clusterBuilderSpec, []*corev1.Secret{&fakeSecret}, builderTag)
require.NoError(t, err)
require.NotNil(t, builderRecord)
require.NotEmpty(t, builderRecord.SignaturePaths)
Expand Down Expand Up @@ -1073,3 +1080,19 @@ func layerToRemoteBuildpack(bpLayer buildpackLayer, layer *fakeLayer, secretRef
SecretRef: secretRef,
}
}

func createRunImage(os string) v1.Image {
runImg, _ := random.Image(1, int64(5))

config, _ := runImg.ConfigFile()

config.OS = os
runImg, _ = mutate.ConfigFile(runImg, config)

return runImg
}

func digest(image v1.Image) string {
d, _ := image.Digest()
return d.String()
}
Loading
Loading