Skip to content

Commit

Permalink
Fix hash key of pushed layer, when using cache
Browse files Browse the repository at this point in the history
Relates to GoogleContainerTools#899
When Kaniko is pushing a layer at the end of a Dockerfile, if previous line have COPY inside, the computeKey is not compute well.
It happen cause the cachingcopycommand (use for cached layer) doesn't support the FilesUsedFromContext method, it was using the base_command one.
I've fixed it by doing a copy paste of the copycommand, and by adding the buildcontext parameter.
I think It will need a rework to refactor it, but I don't know how to use CacheCopy instead of BaseCommand on line 169 (it crash at runtime)
  • Loading branch information
cdrappier committed Dec 11, 2019
1 parent f8507eb commit a65f258
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ func (c *CopyCommand) ShouldCacheOutput() bool {
func (c *CopyCommand) CacheCommand(img v1.Image) DockerCommand {

return &CachingCopyCommand{
img: img,
cmd: c.cmd,
img: img,
buildcontext: c.buildcontext,
cmd: c.cmd,
}
}

Expand All @@ -168,6 +169,7 @@ func (c *CopyCommand) From() string {
type CachingCopyCommand struct {
BaseCommand
img v1.Image
buildcontext string
extractedFiles []string
cmd *instructions.CopyCommand
}
Expand All @@ -183,6 +185,32 @@ func (cr *CachingCopyCommand) ExecuteCommand(config *v1.Config, buildArgs *docke
return nil
}

func (cr *CachingCopyCommand) MetadataOnly() bool {
return false
}

func (cr *CachingCopyCommand) FilesUsedFromContext(config *v1.Config, buildArgs *dockerfile.BuildArgs) ([]string, error) {
// We don't use the context if we're performing a copy --from.

if cr.cmd.From != "" {
return nil, nil
}

replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
srcs, _, err := util.ResolveEnvAndWildcards(cr.cmd.SourcesAndDest, cr.buildcontext, replacementEnvs)
if err != nil {
return nil, err
}

files := []string{}
for _, src := range srcs {
fullPath := filepath.Join(cr.buildcontext, src)
files = append(files, fullPath)
}
logrus.Infof("Using files from context: %v", files)
return files, nil
}

func (cr *CachingCopyCommand) FilesToSnapshot() []string {
return cr.extractedFiles
}
Expand Down

0 comments on commit a65f258

Please sign in to comment.