@@ -315,33 +315,6 @@ func (d *ociImageDestination) addManifest(desc *imgspecv1.Descriptor) {
315315 d .index .Manifests = append (slices .Clone (d .index .Manifests ), * desc )
316316}
317317
318- // addSignatureManifest is similar to addManifest, but replace the entry based on imgspecv1.AnnotationRefName
319- // and returns the old digest to delete it later.
320- func (d * ociImageDestination ) addSignatureManifest (desc * imgspecv1.Descriptor ) (* imgspecv1.Descriptor , error ) {
321- if desc .Annotations == nil || desc .Annotations [imgspecv1 .AnnotationRefName ] == "" {
322- return nil , errors .New ("cannot add signature manifest without ref.name" )
323- }
324- for i , m := range d .index .Manifests {
325- if m .Annotations [imgspecv1 .AnnotationRefName ] == desc .Annotations [imgspecv1 .AnnotationRefName ] {
326- // Replace it completely.
327- oldDesc := d .index .Manifests [i ]
328- d .index .Manifests [i ] = * desc
329- return & oldDesc , nil
330- }
331- }
332- // It shouldn't happen, but if there's no entry with the same ref name, but the same digest, just replace it.
333- for i , m := range d .index .Manifests {
334- if m .Digest == desc .Digest && m .Annotations [imgspecv1 .AnnotationRefName ] == "" {
335- // Replace it completely.
336- d .index .Manifests [i ] = * desc
337- return nil , nil
338- }
339- }
340- // It's a new entry to be added to the index. Use slices.Clone() to avoid a remote dependency on how d.index was created.
341- d .index .Manifests = append (slices .Clone (d .index .Manifests ), * desc )
342- return nil , nil
343- }
344-
345318// CommitWithOptions marks the process of storing the image as successful and asks for the image to be persisted.
346319// WARNING: This does not have any transactional semantics:
347320// - Uploaded data MAY be visible to others before CommitWithOptions() is called
@@ -409,9 +382,9 @@ func (d *ociImageDestination) PutSignaturesWithFormat(ctx context.Context, signa
409382}
410383
411384func (d * ociImageDestination ) putSignaturesToSigstoreAttachment (ctx context.Context , signatures []signature.Sigstore , manifestDigest digest.Digest ) error {
412- var signConfig imgspecv1.Image // Most fields empty by default
413-
414- signManifest , err := d .ref .getSigstoreAttachmentManifest (manifestDigest , & d .index , d .sharedBlobDir )
385+ var signConfig imgspecv1.Image // Most fields empty by default
386+ var oldConfigDigest * digest. Digest // It is used for cleanup when updated
387+ signManifest , signDesc , err := d .ref .getSigstoreAttachmentManifest (manifestDigest , & d .index , d .sharedBlobDir )
415388 if err != nil {
416389 return err
417390 }
@@ -423,6 +396,7 @@ func (d *ociImageDestination) putSignaturesToSigstoreAttachment(ctx context.Cont
423396 }, nil )
424397 signConfig .RootFS .Type = "layers"
425398 } else {
399+ oldConfigDigest = & signManifest .Config .Digest
426400 logrus .Debugf ("Fetching sigstore attachment config %s" , signManifest .Config .Digest .String ())
427401 configBlob , err := d .ref .getOCIDescriptorContents (signManifest .Config .Digest , iolimits .MaxConfigBodySize , d .sharedBlobDir )
428402 if err != nil {
@@ -500,25 +474,17 @@ func (d *ociImageDestination) putSignaturesToSigstoreAttachment(ctx context.Cont
500474 if err != nil {
501475 return err
502476 }
503- oldDesc , err := d . addSignatureManifest (& imgspecv1.Descriptor {
477+ d . addManifest (& imgspecv1.Descriptor {
504478 MediaType : signManifest .MediaType ,
505479 Digest : signDigest ,
506480 Size : int64 (len (signManifestBlob )),
507481 Annotations : map [string ]string {
508482 imgspecv1 .AnnotationRefName : signTag ,
509483 },
510484 })
511- if err != nil {
512- return err
513- }
514- // If it overwrote an existing signature manifest, delete blobs referenced by the old manifest.
515- if oldDesc != nil {
516- referencedBlobs := make (map [digest.Digest ]int )
517- err = d .ref .countBlobsForDescriptor (referencedBlobs , oldDesc , d .sharedBlobDir )
518- if err != nil {
519- return fmt .Errorf ("error counting blobs for digest %s: %w" , oldDesc .Digest .String (), err )
520- }
521- d .blobDeleteCandidates .AddSeq (maps .Keys (referencedBlobs ))
485+ if signDesc != nil && oldConfigDigest != nil {
486+ d .blobDeleteCandidates .Add (signDesc .Digest )
487+ d .blobDeleteCandidates .Add (* oldConfigDigest )
522488 }
523489 return nil
524490}
0 commit comments