From 8c078c72a31d8a405095a68cd03aa2cc62d4eb63 Mon Sep 17 00:00:00 2001 From: Vladimir Krumshtein Date: Fri, 21 Feb 2020 23:25:03 +0100 Subject: [PATCH 1/4] Use image digest for cacheid in FROM step --- lib/builder/step/from_step.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/builder/step/from_step.go b/lib/builder/step/from_step.go index 7ca0a33e..c7443f54 100644 --- a/lib/builder/step/from_step.go +++ b/lib/builder/step/from_step.go @@ -74,10 +74,13 @@ func (s *FromStep) GetAlias() string { return s.alias } -// SetCacheID sets the cacheID of the step using the name of the base image. -// TODO: Use the sha of that image instead of the image name itself. +// SetCacheID sets the cacheID of the step using the sha of that image. func (s *FromStep) SetCacheID(ctx *context.BuildContext, seed string) error { - checksum := crc32.ChecksumIEEE([]byte(seed + string(s.directive) + s.image)) + manifest, err := s.getManifest(ctx.ImageStore) + if err != nil { + return fmt.Errorf("get manifest: %s", err) + } + checksum := crc32.ChecksumIEEE([]byte(seed + string(manifest.Config.Digest))) s.cacheID = fmt.Sprintf("%x", checksum) return nil } From a32ca859f6568d3e7f1d9a341214d077ffa92fdf Mon Sep 17 00:00:00 2001 From: Vladimir Krumshtein Date: Sat, 22 Feb 2020 14:16:49 +0100 Subject: [PATCH 2/4] Use sha of layers --- lib/builder/step/from_step.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/builder/step/from_step.go b/lib/builder/step/from_step.go index c7443f54..d7e48b84 100644 --- a/lib/builder/step/from_step.go +++ b/lib/builder/step/from_step.go @@ -78,9 +78,17 @@ func (s *FromStep) GetAlias() string { func (s *FromStep) SetCacheID(ctx *context.BuildContext, seed string) error { manifest, err := s.getManifest(ctx.ImageStore) if err != nil { - return fmt.Errorf("get manifest: %s", err) + log.Infof("Could not get manifest: %s. Using hash of from directive as cacheID.", err) + checksum := crc32.ChecksumIEEE([]byte(seed + string(s.directive))) + s.cacheID = fmt.Sprintf("%x", checksum) + return nil + } + layerDigest := "" + for _, layer := range manifest.Layers { + log.Infof("Layers digest " + layer.Digest.Hex()) + layerDigest += string(layer.Digest) } - checksum := crc32.ChecksumIEEE([]byte(seed + string(manifest.Config.Digest))) + checksum := crc32.ChecksumIEEE([]byte(seed + layerDigest)) s.cacheID = fmt.Sprintf("%x", checksum) return nil } From 39c3b184466d429acd6ae79232e110667984889e Mon Sep 17 00:00:00 2001 From: Vladimir Krumshtein Date: Mon, 24 Feb 2020 13:56:56 +0100 Subject: [PATCH 3/4] Use s.image for cache generation as well. --- lib/builder/step/from_step.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/builder/step/from_step.go b/lib/builder/step/from_step.go index d7e48b84..9d87e2fd 100644 --- a/lib/builder/step/from_step.go +++ b/lib/builder/step/from_step.go @@ -78,8 +78,8 @@ func (s *FromStep) GetAlias() string { func (s *FromStep) SetCacheID(ctx *context.BuildContext, seed string) error { manifest, err := s.getManifest(ctx.ImageStore) if err != nil { - log.Infof("Could not get manifest: %s. Using hash of from directive as cacheID.", err) - checksum := crc32.ChecksumIEEE([]byte(seed + string(s.directive))) + log.Infof("Could not get manifest: %s. Using hash of from %s as cacheID.", err, string(s.directive)+s.image) + checksum := crc32.ChecksumIEEE([]byte(seed + string(s.directive) + s.image)) s.cacheID = fmt.Sprintf("%x", checksum) return nil } From 1d174315cccb9a4127787a948f9451fbc2a4db84 Mon Sep 17 00:00:00 2001 From: Vladimir Krumshtein Date: Mon, 24 Feb 2020 15:54:10 +0100 Subject: [PATCH 4/4] Remove a test log message --- lib/builder/step/from_step.go | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/builder/step/from_step.go b/lib/builder/step/from_step.go index 9d87e2fd..a514ace4 100644 --- a/lib/builder/step/from_step.go +++ b/lib/builder/step/from_step.go @@ -85,7 +85,6 @@ func (s *FromStep) SetCacheID(ctx *context.BuildContext, seed string) error { } layerDigest := "" for _, layer := range manifest.Layers { - log.Infof("Layers digest " + layer.Digest.Hex()) layerDigest += string(layer.Digest) } checksum := crc32.ChecksumIEEE([]byte(seed + layerDigest))