From 327ae3f1c802a2376e8753f05b775a50446d681b Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 20 Nov 2024 11:59:46 +0000 Subject: [PATCH] fix: prevent layer overwrites in image resulting in BLOB_UNKNOWN error Refs coder/envbuilder#385 --- pkg/executor/build.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 70fc4ab842..6a48005e70 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -729,6 +729,23 @@ func (s *stageBuilder) saveLayerToImage(layer v1.Layer, createdBy string) error if err != nil { return err } + + // Images in google/go-containerregistry don't support adding unique layers + // with duplicate diff IDs. For example, if the source image has an empty + // layer which has been compressed with Gzip level 3, and the layer we're + // adding is also an empty layer compressed with Gzip level 1, the diff IDs + // would match but the layer blobs would be different. This would cause an + // error when trying to upload the image to a registry as the manifest is + // referencing a blob that has been "overwritten". + diffID, err := layer.DiffID() + if err != nil { + return errors.Wrap(err, "checking layer diffID failed") + } + if el, err := s.image.LayerByDiffID(diffID); err == nil { + logrus.Debugf("Layer already exists in image, using existing layer: %s", diffID) + layer = el + } + s.image, err = mutate.Append(s.image, mutate.Addendum{ Layer: layer,