This commit is contained in:
massimeddu-sj 2025-07-10 08:29:52 -03:00 committed by GitHub
commit 5c8719e80e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 35 additions and 14 deletions

View File

@ -28,6 +28,10 @@ func (b *BaseCommand) IsArgsEnvsRequiredInCache() bool {
return false
}
func (b *BaseCommand) IsConsiderExcludedFilesInCache() bool {
return false
}
func (b *BaseCommand) CacheCommand(v1.Image) DockerCommand {
return nil
}

View File

@ -62,6 +62,10 @@ type DockerCommand interface {
// True if need add ARGs and EVNs to composite cache string with resolved command
// need only for RUN instruction
IsArgsEnvsRequiredInCache() bool
// True if need add consider excluded files to composite cache string with resolved command
// need only for COPY --from instruction
IsConsiderExcludedFilesInCache() bool
}
func GetCommand(cmd instructions.Command, fileContext util.FileContext, useNewRun bool, cacheCopy bool, cacheRun bool) (DockerCommand, error) {

View File

@ -158,6 +158,10 @@ func (c *CopyCommand) ShouldCacheOutput() bool {
return c.shdCache
}
func (c *CopyCommand) IsConsiderExcludedFilesInCache() bool {
return c.cmd.From != ""
}
// CacheCommand returns true since this command should be cached
func (c *CopyCommand) CacheCommand(img v1.Image) DockerCommand {
return &CachingCopyCommand{

View File

@ -219,8 +219,9 @@ func (s *stageBuilder) populateCompositeKey(command commands.DockerCommand, file
// Add the next command to the cache key.
compositeKey.AddKey(command.String())
considerExcludedFiles := command.IsConsiderExcludedFilesInCache()
for _, f := range files {
if err := compositeKey.AddPath(f, s.fileContext); err != nil {
if err := compositeKey.AddPath(f, s.fileContext, considerExcludedFiles); err != nil {
return compositeKey, err
}
}

View File

@ -934,7 +934,7 @@ func Test_stageBuilder_build(t *testing.T) {
filePath := filepath.Join(dir, file)
ch := NewCompositeCache("", "meow")
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
@ -964,7 +964,7 @@ func Test_stageBuilder_build(t *testing.T) {
filePath := filepath.Join(dir, file)
ch := NewCompositeCache("", "meow")
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
@ -997,7 +997,7 @@ func Test_stageBuilder_build(t *testing.T) {
filePath := filepath.Join(dir, file)
ch := NewCompositeCache("", "meow")
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
@ -1052,7 +1052,7 @@ func Test_stageBuilder_build(t *testing.T) {
tarContent := generateTar(t, dir, filename)
ch := NewCompositeCache("", fmt.Sprintf("COPY %s foo.txt", filename))
ch.AddPath(filepath, util.FileContext{})
ch.AddPath(filepath, util.FileContext{}, false)
hash, err := ch.Hash()
if err != nil {
@ -1119,7 +1119,7 @@ func Test_stageBuilder_build(t *testing.T) {
destDir := t.TempDir()
filePath := filepath.Join(dir, filename)
ch := NewCompositeCache("", fmt.Sprintf("COPY %s foo.txt", filename))
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
hash, err := ch.Hash()
if err != nil {
@ -1185,7 +1185,7 @@ COPY %s foo.txt
}
ch.AddKey(fmt.Sprintf("COPY %s bar.txt", filename))
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
hash2, err := ch.Hash()
if err != nil {
@ -1193,7 +1193,7 @@ COPY %s foo.txt
}
ch = NewCompositeCache("", fmt.Sprintf("COPY %s foo.txt", filename))
ch.AddKey(fmt.Sprintf("COPY %s bar.txt", filename))
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
image := fakeImage{
ImageLayers: []v1.Layer{
@ -1253,7 +1253,7 @@ COPY %s bar.txt
filePath := filepath.Join(dir, filename)
ch := NewCompositeCache("", fmt.Sprintf("COPY %s bar.txt", filename))
ch.AddPath(filePath, util.FileContext{})
ch.AddPath(filePath, util.FileContext{}, false)
// copy hash
_, err := ch.Hash()

View File

@ -55,7 +55,7 @@ func (s *CompositeCache) Hash() (string, error) {
return util.SHA256(strings.NewReader(s.Key()))
}
func (s *CompositeCache) AddPath(p string, context util.FileContext) error {
func (s *CompositeCache) AddPath(p string, context util.FileContext, considerExcludedFiles bool) error {
sha := sha256.New()
fi, err := os.Lstat(p)
if err != nil {
@ -70,7 +70,7 @@ func (s *CompositeCache) AddPath(p string, context util.FileContext) error {
// Only add the hash of this directory to the key
// if there is any ignored content.
if !empty || !context.ExcludesFile(p) {
if !empty || considerExcludedFiles || !context.ExcludesFile(p) {
s.keys = append(s.keys, k)
}
return nil

View File

@ -76,7 +76,7 @@ func Test_CompositeCache_AddPath_dir(t *testing.T) {
fn := func() string {
r := NewCompositeCache()
if err := r.AddPath(tmpDir, util.FileContext{}); err != nil {
if err := r.AddPath(tmpDir, util.FileContext{}, false); err != nil {
t.Errorf("expected error to be nil but was %v", err)
}
@ -114,7 +114,7 @@ func Test_CompositeCache_AddPath_file(t *testing.T) {
p := tmpfile.Name()
fn := func() string {
r := NewCompositeCache()
if err := r.AddPath(p, util.FileContext{}); err != nil {
if err := r.AddPath(p, util.FileContext{}, false); err != nil {
t.Errorf("expected error to be nil but was %v", err)
}
@ -166,7 +166,7 @@ func setIgnoreContext(t *testing.T, content string) (util.FileContext, error) {
func hashDirectory(dirpath string, fileContext util.FileContext) (string, error) {
cache1 := NewCompositeCache()
err := cache1.AddPath(dirpath, fileContext)
err := cache1.AddPath(dirpath, fileContext, false)
if err != nil {
return "", err
}

View File

@ -50,6 +50,7 @@ type MockDockerCommand struct {
contextFiles []string
cacheCommand commands.DockerCommand
argToCompositeCache bool
considerExcludedFiles bool
}
func (m MockDockerCommand) ExecuteCommand(c *v1.Config, args *dockerfile.BuildArgs) error { return nil }
@ -83,10 +84,14 @@ func (m MockDockerCommand) ShouldDetectDeletedFiles() bool {
func (m MockDockerCommand) IsArgsEnvsRequiredInCache() bool {
return m.argToCompositeCache
}
func (m MockDockerCommand) IsConsiderExcludedFilesInCache() bool {
return m.argToCompositeCache
}
type MockCachedDockerCommand struct {
contextFiles []string
argToCompositeCache bool
considerExcludedFiles bool
}
func (m MockCachedDockerCommand) ExecuteCommand(c *v1.Config, args *dockerfile.BuildArgs) error {
@ -122,6 +127,9 @@ func (m MockCachedDockerCommand) ShouldCacheOutput() bool {
func (m MockCachedDockerCommand) IsArgsEnvsRequiredInCache() bool {
return m.argToCompositeCache
}
func (m MockCachedDockerCommand) IsConsiderExcludedFilesInCache() bool {
return m.considerExcludedFiles
}
type fakeLayerCache struct {
retrieve bool