Skip to content

fix: allow caching run layers with no filesystem changes #355

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 1 commit into from
Sep 25, 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
46 changes: 24 additions & 22 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,17 +456,18 @@ func Run(ctx context.Context, opts options.Options) error {
}
kOpts := &config.KanikoOptions{
// Boilerplate!
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())),
SnapshotMode: "redo",
RunV2: true,
RunStdout: stdoutWriter,
RunStderr: stderrWriter,
Destinations: destinations,
NoPush: !opts.PushImage || len(destinations) == 0,
CacheRunLayers: true,
CacheCopyLayers: true,
CompressedCaching: true,
Compression: config.ZStd,
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())),
SnapshotMode: "redo",
RunV2: true,
RunStdout: stdoutWriter,
RunStderr: stderrWriter,
Destinations: destinations,
NoPush: !opts.PushImage || len(destinations) == 0,
CacheRunLayers: true,
CacheCopyLayers: true,
ForceBuildMetadata: opts.PushImage, // Force layers with no changes to be cached, required for cache probing.
Copy link
Member

Choose a reason for hiding this comment

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

Is there any downside to always enabling this option regardless of PushImage?

Copy link
Member Author

@mafredri mafredri Sep 25, 2024

Choose a reason for hiding this comment

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

I'm not sure. It'll be an additional cache layer to download during builds vs executing the command and ending up with the same result. Mainly added it to be cautious.

Imagine the following scenario:

RUN if [[ $(date +%s) -gt 1734998400 ]]; then touch "/'member-christmas?"; fi

With force build metadata, we would still use the cache here and not produce a new image (assuming cache exists from before).

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, let's leave as-is for now 👍

CompressedCaching: true,
Compression: config.ZStd,
// Maps to "default" level, ~100-300 MB/sec according to
// benchmarks in klauspost/compress README
// https://github.com/klauspost/compress/blob/67a538e2b4df11f8ec7139388838a13bce84b5d5/zstd/encoder_options.go#L188
Expand Down Expand Up @@ -1180,17 +1181,18 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
}
kOpts := &config.KanikoOptions{
// Boilerplate!
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())),
SnapshotMode: "redo",
RunV2: true,
RunStdout: stdoutWriter,
RunStderr: stderrWriter,
Destinations: destinations,
NoPush: !opts.PushImage || len(destinations) == 0,
CacheRunLayers: true,
CacheCopyLayers: true,
CompressedCaching: true,
Compression: config.ZStd,
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())),
SnapshotMode: "redo",
RunV2: true,
RunStdout: stdoutWriter,
RunStderr: stderrWriter,
Destinations: destinations,
NoPush: true,
CacheRunLayers: true,
CacheCopyLayers: true,
ForceBuildMetadata: true, // Force layers with no changes to be cached, required for cache probing.
CompressedCaching: true,
Compression: config.ZStd,
// Maps to "default" level, ~100-300 MB/sec according to
// benchmarks in klauspost/compress README
// https://github.com/klauspost/compress/blob/67a538e2b4df11f8ec7139388838a13bce84b5d5/zstd/encoder_options.go#L188
Expand Down
50 changes: 50 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,56 @@ RUN date --utc > /root/date.txt`, testImageAlpine),
require.NotEmpty(t, strings.TrimSpace(out))
})

t.Run("CacheAndPushWithNoChangeLayers", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

srv := gittest.CreateGitServer(t, gittest.Options{
Files: map[string]string{
"Dockerfile": fmt.Sprintf(`
FROM %[1]s
RUN touch /foo
RUN echo "Hi, please don't put me in a layer (I guess you won't listen to me...)"
RUN touch /bar
`, testImageAlpine),
},
})

// Given: an empty registry
testReg := setupInMemoryRegistry(t, setupInMemoryRegistryOpts{})
testRepo := testReg + "/test"
ref, err := name.ParseReference(testRepo + ":latest")
require.NoError(t, err)
_, err = remote.Image(ref)
require.ErrorContains(t, err, "NAME_UNKNOWN", "expected image to not be present before build + push")

opts := []string{
envbuilderEnv("GIT_URL", srv.URL),
envbuilderEnv("CACHE_REPO", testRepo),
envbuilderEnv("DOCKERFILE_PATH", "Dockerfile"),
}

// When: we run envbuilder with PUSH_IMAGE set
_ = pushImage(t, ref, nil, opts...)

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)
defer cli.Close()

// Then: re-running envbuilder with GET_CACHED_IMAGE should succeed
cachedRef := getCachedImage(ctx, t, cli, opts...)

// When: we run the image we just built
ctr := startContainerFromRef(ctx, t, cli, cachedRef)

// Then: the envbuilder binary exists in the image!
out := execContainer(t, ctr.ID, "/.envbuilder/bin/envbuilder --help")
require.Regexp(t, `(?s)^USAGE:\s+envbuilder`, strings.TrimSpace(out))
require.NotEmpty(t, strings.TrimSpace(out))
})

t.Run("CacheAndPushAuth", func(t *testing.T) {
t.Parallel()

Expand Down