Skip to content

Commit 065bcaa

Browse files
authored
fix: always add COPY directives in DoCacheProbe (#293)
1 parent 9073748 commit 065bcaa

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

envbuilder.go

+20
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,26 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
10611061
})
10621062
}
10631063

1064+
// We expect an image built and pushed by envbuilder to have the envbuilder
1065+
// binary present at a predefined path. In order to correctly replicate the
1066+
// build via executor.RunCacheProbe we need to have the *exact* copy of the
1067+
// envbuilder binary available used to build the image.
1068+
exePath := opts.BinaryPath
1069+
// Add an exception for the current running binary in kaniko ignore list
1070+
if err := util.AddAllowedPathToDefaultIgnoreList(exePath); err != nil {
1071+
return nil, xerrors.Errorf("add exe path to ignore list: %w", err)
1072+
}
1073+
// Copy the envbuilder binary into the build context.
1074+
buildParams.DockerfileContent += fmt.Sprintf(`
1075+
COPY --chmod=0755 %s %s
1076+
USER root
1077+
WORKDIR /
1078+
ENTRYPOINT [%q]`, exePath, exePath, exePath)
1079+
dst := filepath.Join(buildParams.BuildContext, exePath)
1080+
if err := copyFile(exePath, dst); err != nil {
1081+
return nil, xerrors.Errorf("copy envbuilder binary to build context: %w", err)
1082+
}
1083+
10641084
stdoutWriter, closeStdout := log.Writer(opts.Logger)
10651085
defer closeStdout()
10661086
stderrWriter, closeStderr := log.Writer(opts.Logger)

integration/integration_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,14 @@ RUN date --utc > /root/date.txt`, testImageAlpine),
11141114
_, err = remote.Image(ref)
11151115
require.ErrorContains(t, err, "MANIFEST_UNKNOWN", "expected image to not be present before build + push")
11161116

1117-
// Then: re-running envbuilder with GET_CACHED_IMAGE should succeed
1117+
// Then: re-running envbuilder with GET_CACHED_IMAGE should not succeed, as
1118+
// the envbuilder binary is not present in the pushed image.
11181119
_, err = runEnvbuilder(t, runOpts{env: []string{
11191120
envbuilderEnv("GIT_URL", srv.URL),
11201121
envbuilderEnv("CACHE_REPO", testRepo),
11211122
envbuilderEnv("GET_CACHED_IMAGE", "1"),
11221123
}})
1123-
require.NoError(t, err)
1124+
require.ErrorContains(t, err, "uncached COPY command is not supported in cache probe mode")
11241125
})
11251126

11261127
t.Run("CacheAndPush", func(t *testing.T) {

options/defaults.go

+3
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ func (o *Options) SetDefaults() {
5555
if o.WorkspaceFolder == "" {
5656
o.WorkspaceFolder = DefaultWorkspaceFolder(o.GitURL)
5757
}
58+
if o.BinaryPath == "" {
59+
o.BinaryPath = "/.envbuilder/bin/envbuilder"
60+
}
5861
}

options/defaults_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestOptions_SetDefaults(t *testing.T) {
8585
Filesystem: chmodfs.New(osfs.New("/")),
8686
GitURL: "",
8787
WorkspaceFolder: constants.EmptyWorkspaceDir,
88+
BinaryPath: "/.envbuilder/bin/envbuilder",
8889
}
8990

9091
var actual options.Options

options/options.go

+15
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ type Options struct {
156156
// used to improving cache utilization when multiple users are building
157157
// working on the same repository.
158158
RemoteRepoBuildMode bool
159+
160+
// BinaryPath is the path to the local envbuilder binary when
161+
// attempting to probe the build cache. This is only relevant when
162+
// GetCachedImage is true.
163+
BinaryPath string
159164
}
160165

161166
const envPrefix = "ENVBUILDER_"
@@ -427,6 +432,13 @@ func (o *Options) CLI() serpent.OptionSet {
427432
Description: "Print the digest of the cached image, if available. " +
428433
"Exits with an error if not found.",
429434
},
435+
{
436+
Flag: "binary-path",
437+
Env: WithEnvPrefix("BINARY_PATH"),
438+
Value: serpent.StringOf(&o.BinaryPath),
439+
Hidden: true,
440+
Description: "Specify the path to an Envbuilder binary for use when probing the build cache.",
441+
},
430442
{
431443
Flag: "remote-repo-build-mode",
432444
Env: WithEnvPrefix("REMOTE_REPO_BUILD_MODE"),
@@ -485,6 +497,9 @@ func (o *Options) Markdown() string {
485497
_, _ = sb.WriteString("| - | - | - | - |\n")
486498

487499
for _, opt := range cliOptions {
500+
if opt.Hidden {
501+
continue
502+
}
488503
d := opt.Default
489504
if d != "" {
490505
d = "`" + d + "`"

0 commit comments

Comments
 (0)