Skip to content

chore: executor: DoCacheProbe: remove fake verbiage #15

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 2 commits into from
Jun 12, 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
4 changes: 2 additions & 2 deletions pkg/commands/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (c caching) Layer() v1.Layer {
return c.layer
}

type FakeExecuteCommand interface {
FakeExecuteCommand(*v1.Config, *dockerfile.BuildArgs) error
type CachedExecuteCommand interface {
CachedExecuteCommand(*v1.Config, *dockerfile.BuildArgs) error
}
2 changes: 1 addition & 1 deletion pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (cr *CachingCopyCommand) ExecuteCommand(config *v1.Config, buildArgs *docke
return nil
}

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (cr *CachingRunCommand) ExecuteCommand(config *v1.Config, buildArgs *docker
return nil
}

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

Expand Down
10 changes: 5 additions & 5 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,15 @@ func (s *stageBuilder) probeCache() error {
}
}()

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

args = sb.args
if err := sb.probeCache(); err != nil {
return nil, errors.Wrap(err, "error fake building stage")
return nil, errors.Wrap(err, "error probing build cache")
}

reviewConfig(stage, &sb.cf.Config)
Expand Down
75 changes: 65 additions & 10 deletions pkg/executor/cache_probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ package executor

import (
"fmt"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/google/go-containerregistry/pkg/registry"

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/GoogleContainerTools/kaniko/testutil"
Expand All @@ -37,8 +41,7 @@ func TestDoCacheProbe(t *testing.T) {
COPY foo/bar.txt copied/
`
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
// Populate the cache by doing an initial build
cacheDir := t.TempDir()
regCache := setupCacheRegistry(t)
Copy link
Member Author

Choose a reason for hiding this comment

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

review: I noticed the oci:/ repo was not actually pushing to cacheDir so I elected to stand up a temporary registry instad.

opts := &config.KanikoOptions{
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
SrcContext: filepath.Join(testDir, "workspace"),
Expand All @@ -49,10 +52,10 @@ COPY foo/bar.txt copied/
},
CacheCopyLayers: true,
CacheRunLayers: true,
CacheRepo: "oci:/" + cacheDir,
CacheRepo: regCache + "/test",
}
_, err := DoCacheProbe(opts)
if err == nil || !strings.Contains(err.Error(), "not supported in fake build") {
if err == nil || !strings.Contains(err.Error(), "uncached command") {
t.Errorf("unexpected error, got %v", err)
}
})
Expand All @@ -64,7 +67,7 @@ COPY foo/bar.txt copied/
COPY foo/bar.txt copied/
`
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
cacheDir := t.TempDir()
regCache := setupCacheRegistry(t)
opts := &config.KanikoOptions{
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
SrcContext: filepath.Join(testDir, "workspace"),
Expand All @@ -75,8 +78,9 @@ COPY foo/bar.txt copied/
},
CacheCopyLayers: true,
CacheRunLayers: true,
CacheRepo: "oci:/" + cacheDir,
CacheRepo: regCache + "/test",
}
// Populate the cache by doing an initial build
_, err := DoBuild(opts)
testutil.CheckNoError(t, err)
opts.Reproducible = true
Expand All @@ -91,18 +95,18 @@ COPY foo/bar.txt copied/
COPY foo/bar.txt copied/
`
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
cacheDir := t.TempDir()
regCache := setupCacheRegistry(t)
opts := &config.KanikoOptions{
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
SrcContext: filepath.Join(testDir, "workspace"),
SnapshotMode: constants.SnapshotModeFull,
SnapshotMode: constants.SnapshotModeRedo,
Cache: true,
CacheOptions: config.CacheOptions{
CacheTTL: time.Hour,
},
CacheCopyLayers: true,
CacheRunLayers: true,
CacheRepo: "oci:/" + cacheDir,
CacheRepo: regCache + "/test",
}
_, err := DoBuild(opts)
testutil.CheckNoError(t, err)
Expand All @@ -115,10 +119,61 @@ COPY foo/baz.txt copied/
`
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
_, err = DoCacheProbe(opts)
if err == nil || !strings.Contains(err.Error(), "not supported in fake build") {
if err == nil || !strings.Contains(err.Error(), "uncached command") {
t.Errorf("unexpected error, got %v", err)
}
})

t.Run("MultiStage", func(t *testing.T) {
t.Skip("TODO: https://github.com/coder/envbuilder/issues/230")
testDir, fn := setupMultistageTests(t)
defer fn()
dockerFile := `
FROM scratch as first
COPY foo/bam.txt copied/
ENV test test

From scratch as second
COPY --from=first copied/bam.txt output/bam.txt`
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
regCache := setupCacheRegistry(t)
opts := &config.KanikoOptions{
DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"),
SrcContext: filepath.Join(testDir, "workspace"),
SnapshotMode: constants.SnapshotModeRedo,
Cache: true,
CacheOptions: config.CacheOptions{
CacheTTL: time.Hour,
},
CacheCopyLayers: true,
CacheRunLayers: true,
CacheRepo: regCache + "/test",
}
_, err := DoBuild(opts)
testutil.CheckNoError(t, err)
os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755)
opts.Reproducible = true
_, err = DoCacheProbe(opts)
testutil.CheckNoError(t, err)
// Check Image has one layer bam.txt
files, err := readDirectory(filepath.Join(testDir, "output"))
if err != nil {
t.Fatal(err)
}
testutil.CheckDeepEqual(t, 1, len(files))
testutil.CheckDeepEqual(t, files[0].Name(), "bam.txt")
Comment on lines +127 to +164

Choose a reason for hiding this comment

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

Is this germane to this set of changes?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it's just a drive-by. I figured I may as well add the test for later since I had it hanging around unstaged.

})
}

func setupCacheRegistry(t *testing.T) string {
t.Helper()
tempDir := t.TempDir()
testReg := registry.New(registry.WithBlobHandler(registry.NewDiskBlobHandler(tempDir)))
regSrv := httptest.NewServer(testReg)
t.Cleanup(func() { regSrv.Close() })
regSrvURL, err := url.Parse(regSrv.URL)
testutil.CheckNoError(t, err)
return fmt.Sprintf("localhost:%s", regSrvURL.Port())
}

func setupCacheProbeTests(t *testing.T) (string, func()) {
Expand Down
1 change: 0 additions & 1 deletion pkg/executor/copy_multistage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ COPY --from=first / output/`
testutil.CheckDeepEqual(t, "bam.link", files[0].Name())
testutil.CheckDeepEqual(t, "bam.txt", files[1].Name())
})

}

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading