Skip to content

Commit 9d2f7ea

Browse files
authored
Merge pull request #15 from coder/cj/kaniko-follow-up-remove-fake-verbiage
chore: executor: DoCacheProbe: remove fake verbiage
2 parents 0a73fcd + 17b2eeb commit 9d2f7ea

File tree

15 files changed

+1446
-20
lines changed

15 files changed

+1446
-20
lines changed

pkg/commands/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ func (c caching) Layer() v1.Layer {
3333
return c.layer
3434
}
3535

36-
type FakeExecuteCommand interface {
37-
FakeExecuteCommand(*v1.Config, *dockerfile.BuildArgs) error
36+
type CachedExecuteCommand interface {
37+
CachedExecuteCommand(*v1.Config, *dockerfile.BuildArgs) error
3838
}

pkg/commands/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (cr *CachingCopyCommand) ExecuteCommand(config *v1.Config, buildArgs *docke
206206
return nil
207207
}
208208

209-
func (cr *CachingCopyCommand) FakeExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
209+
func (cr *CachingCopyCommand) CachedExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
210210
logrus.Infof("Found cached layer, faking extraction to filesystem")
211211
var err error
212212

pkg/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (cr *CachingRunCommand) ExecuteCommand(config *v1.Config, buildArgs *docker
256256
return nil
257257
}
258258

259-
func (cr *CachingRunCommand) FakeExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
259+
func (cr *CachingRunCommand) CachedExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
260260
logrus.Infof("Found cached layer, faking extraction to filesystem")
261261
var err error
262262

pkg/executor/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,15 @@ func (s *stageBuilder) probeCache() error {
495495
}
496496
}()
497497

498-
if c, ok := command.(commands.FakeExecuteCommand); ok {
499-
if err := c.FakeExecuteCommand(&s.cf.Config, s.args); err != nil {
500-
return errors.Wrap(err, "failed to execute fake command")
498+
if c, ok := command.(commands.CachedExecuteCommand); ok {
499+
if err := c.CachedExecuteCommand(&s.cf.Config, s.args); err != nil {
500+
return errors.Wrap(err, "failed to execute cached command")
501501
}
502502
} else {
503503
switch command.(type) {
504504
case *commands.UserCommand:
505505
default:
506-
return errors.Errorf("uncached command %T is not supported in fake build", command)
506+
return errors.Errorf("uncached command %T encountered when probing cache", command)
507507
}
508508
if err := command.ExecuteCommand(&s.cf.Config, s.args); err != nil {
509509
return errors.Wrap(err, "failed to execute command")
@@ -977,7 +977,7 @@ func DoCacheProbe(opts *config.KanikoOptions) (v1.Image, error) {
977977

978978
args = sb.args
979979
if err := sb.probeCache(); err != nil {
980-
return nil, errors.Wrap(err, "error fake building stage")
980+
return nil, errors.Wrap(err, "error probing build cache")
981981
}
982982

983983
reviewConfig(stage, &sb.cf.Config)

pkg/executor/cache_probe_test.go

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ package executor
1818

1919
import (
2020
"fmt"
21+
"net/http/httptest"
22+
"net/url"
2123
"os"
2224
"path/filepath"
2325
"strings"
2426
"testing"
2527
"time"
2628

29+
"github.com/google/go-containerregistry/pkg/registry"
30+
2731
"github.com/GoogleContainerTools/kaniko/pkg/config"
2832
"github.com/GoogleContainerTools/kaniko/pkg/constants"
2933
"github.com/GoogleContainerTools/kaniko/testutil"
@@ -37,8 +41,7 @@ func TestDoCacheProbe(t *testing.T) {
3741
COPY foo/bar.txt copied/
3842
`
3943
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
40-
// Populate the cache by doing an initial build
41-
cacheDir := t.TempDir()
44+
regCache := setupCacheRegistry(t)
4245
opts := &config.KanikoOptions{
4346
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
4447
SrcContext: filepath.Join(testDir, "workspace"),
@@ -49,10 +52,10 @@ COPY foo/bar.txt copied/
4952
},
5053
CacheCopyLayers: true,
5154
CacheRunLayers: true,
52-
CacheRepo: "oci:/" + cacheDir,
55+
CacheRepo: regCache + "/test",
5356
}
5457
_, err := DoCacheProbe(opts)
55-
if err == nil || !strings.Contains(err.Error(), "not supported in fake build") {
58+
if err == nil || !strings.Contains(err.Error(), "uncached command") {
5659
t.Errorf("unexpected error, got %v", err)
5760
}
5861
})
@@ -64,7 +67,7 @@ COPY foo/bar.txt copied/
6467
COPY foo/bar.txt copied/
6568
`
6669
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
67-
cacheDir := t.TempDir()
70+
regCache := setupCacheRegistry(t)
6871
opts := &config.KanikoOptions{
6972
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
7073
SrcContext: filepath.Join(testDir, "workspace"),
@@ -75,8 +78,9 @@ COPY foo/bar.txt copied/
7578
},
7679
CacheCopyLayers: true,
7780
CacheRunLayers: true,
78-
CacheRepo: "oci:/" + cacheDir,
81+
CacheRepo: regCache + "/test",
7982
}
83+
// Populate the cache by doing an initial build
8084
_, err := DoBuild(opts)
8185
testutil.CheckNoError(t, err)
8286
opts.Reproducible = true
@@ -91,18 +95,18 @@ COPY foo/bar.txt copied/
9195
COPY foo/bar.txt copied/
9296
`
9397
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
94-
cacheDir := t.TempDir()
98+
regCache := setupCacheRegistry(t)
9599
opts := &config.KanikoOptions{
96100
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
97101
SrcContext: filepath.Join(testDir, "workspace"),
98-
SnapshotMode: constants.SnapshotModeFull,
102+
SnapshotMode: constants.SnapshotModeRedo,
99103
Cache: true,
100104
CacheOptions: config.CacheOptions{
101105
CacheTTL: time.Hour,
102106
},
103107
CacheCopyLayers: true,
104108
CacheRunLayers: true,
105-
CacheRepo: "oci:/" + cacheDir,
109+
CacheRepo: regCache + "/test",
106110
}
107111
_, err := DoBuild(opts)
108112
testutil.CheckNoError(t, err)
@@ -115,10 +119,61 @@ COPY foo/baz.txt copied/
115119
`
116120
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
117121
_, err = DoCacheProbe(opts)
118-
if err == nil || !strings.Contains(err.Error(), "not supported in fake build") {
122+
if err == nil || !strings.Contains(err.Error(), "uncached command") {
119123
t.Errorf("unexpected error, got %v", err)
120124
}
121125
})
126+
127+
t.Run("MultiStage", func(t *testing.T) {
128+
t.Skip("TODO: https://github.com/coder/envbuilder/issues/230")
129+
testDir, fn := setupMultistageTests(t)
130+
defer fn()
131+
dockerFile := `
132+
FROM scratch as first
133+
COPY foo/bam.txt copied/
134+
ENV test test
135+
136+
From scratch as second
137+
COPY --from=first copied/bam.txt output/bam.txt`
138+
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
139+
regCache := setupCacheRegistry(t)
140+
opts := &config.KanikoOptions{
141+
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
142+
SrcContext: filepath.Join(testDir, "workspace"),
143+
SnapshotMode: constants.SnapshotModeRedo,
144+
Cache: true,
145+
CacheOptions: config.CacheOptions{
146+
CacheTTL: time.Hour,
147+
},
148+
CacheCopyLayers: true,
149+
CacheRunLayers: true,
150+
CacheRepo: regCache + "/test",
151+
}
152+
_, err := DoBuild(opts)
153+
testutil.CheckNoError(t, err)
154+
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
155+
opts.Reproducible = true
156+
_, err = DoCacheProbe(opts)
157+
testutil.CheckNoError(t, err)
158+
// Check Image has one layer bam.txt
159+
files, err := readDirectory(filepath.Join(testDir, "output"))
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
testutil.CheckDeepEqual(t, 1, len(files))
164+
testutil.CheckDeepEqual(t, files[0].Name(), "bam.txt")
165+
})
166+
}
167+
168+
func setupCacheRegistry(t *testing.T) string {
169+
t.Helper()
170+
tempDir := t.TempDir()
171+
testReg := registry.New(registry.WithBlobHandler(registry.NewDiskBlobHandler(tempDir)))
172+
regSrv := httptest.NewServer(testReg)
173+
t.Cleanup(func() { regSrv.Close() })
174+
regSrvURL, err := url.Parse(regSrv.URL)
175+
testutil.CheckNoError(t, err)
176+
return fmt.Sprintf("localhost:%s", regSrvURL.Port())
122177
}
123178

124179
func setupCacheProbeTests(t *testing.T) (string, func()) {

pkg/executor/copy_multistage_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ COPY --from=first / output/`
169169
testutil.CheckDeepEqual(t, "bam.link", files[0].Name())
170170
testutil.CheckDeepEqual(t, "bam.txt", files[1].Name())
171171
})
172-
173172
}
174173

175174
func setupMultistageTests(t *testing.T) (string, func()) {

vendor/github.com/google/go-containerregistry/internal/httptest/httptest.go

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/go-containerregistry/pkg/registry/README.md

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)