Skip to content

Commit 327ae3f

Browse files
committed
fix: prevent layer overwrites in image resulting in BLOB_UNKNOWN error
Refs coder/envbuilder#385
1 parent 350cbb8 commit 327ae3f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

pkg/executor/build.go

+17
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,23 @@ func (s *stageBuilder) saveLayerToImage(layer v1.Layer, createdBy string) error
729729
if err != nil {
730730
return err
731731
}
732+
733+
// Images in google/go-containerregistry don't support adding unique layers
734+
// with duplicate diff IDs. For example, if the source image has an empty
735+
// layer which has been compressed with Gzip level 3, and the layer we're
736+
// adding is also an empty layer compressed with Gzip level 1, the diff IDs
737+
// would match but the layer blobs would be different. This would cause an
738+
// error when trying to upload the image to a registry as the manifest is
739+
// referencing a blob that has been "overwritten".
740+
diffID, err := layer.DiffID()
741+
if err != nil {
742+
return errors.Wrap(err, "checking layer diffID failed")
743+
}
744+
if el, err := s.image.LayerByDiffID(diffID); err == nil {
745+
logrus.Debugf("Layer already exists in image, using existing layer: %s", diffID)
746+
layer = el
747+
}
748+
732749
s.image, err = mutate.Append(s.image,
733750
mutate.Addendum{
734751
Layer: layer,

0 commit comments

Comments
 (0)