...

Source file src/edge-infra.dev/pkg/f8n/warehouse/oci/mutate/name.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/oci/mutate

     1  package mutate
     2  
     3  import (
     4  	"fmt"
     5  
     6  	v1 "github.com/google/go-containerregistry/pkg/v1"
     7  	"github.com/google/go-containerregistry/pkg/v1/mutate"
     8  
     9  	"edge-infra.dev/pkg/f8n/warehouse"
    10  	"edge-infra.dev/pkg/f8n/warehouse/oci"
    11  	"edge-infra.dev/pkg/f8n/warehouse/oci/match"
    12  )
    13  
    14  // Name mutates the name annotations of Artifact a. If a is an ImageIndex, the
    15  // reference annotations in the embedded descriptors are also mutated.
    16  func Name(a oci.Artifact, newName string) (oci.Artifact, error) {
    17  	switch a := a.(type) {
    18  	case oci.Unwrapper:
    19  		return Name(a.Unwrap(), newName)
    20  	case v1.Image:
    21  		annos, err := oci.Annotations(a)
    22  		if err != nil {
    23  			return nil, err
    24  		}
    25  		annos[warehouse.AnnotationName] = newName
    26  		return oci.Annotate(a, annos).(v1.Image), nil
    27  	case v1.ImageIndex:
    28  		m, err := a.IndexManifest()
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		name := m.Annotations[warehouse.AnnotationName]
    33  		if name == "" {
    34  			return nil, fmt.Errorf("mutate.Name: %w: missing name annotation",
    35  				oci.ErrInvalidArtifact)
    36  		}
    37  
    38  		// Update name annotation on root artifact before handling children
    39  		m.Annotations[warehouse.AnnotationName] = newName
    40  		a = oci.Annotate(a, m.Annotations).(v1.ImageIndex)
    41  
    42  		// Create matcher for finding provider variants
    43  		matcher := match.Annotation(warehouse.AnnotationRefName, name)
    44  
    45  		descs, err := match.FindManifests(a, matcher)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  
    50  		updatedDescs := make([]mutate.IndexAddendum, len(descs))
    51  		for i, d := range descs {
    52  			child, err := oci.ArtifactFromIdx(a, d)
    53  			if err != nil {
    54  				return nil, err
    55  			}
    56  			annos, err := oci.Annotations(child)
    57  			if err != nil {
    58  				return nil, err
    59  			}
    60  			annos[warehouse.AnnotationName] = newName
    61  			child = oci.Annotate(child, annos).(v1.Image)
    62  
    63  			d.Annotations[warehouse.AnnotationRefName] = newName
    64  
    65  			updatedDescs[i] = mutate.IndexAddendum{
    66  				Add:        child,
    67  				Descriptor: d,
    68  			}
    69  		}
    70  
    71  		return mutate.AppendManifests(
    72  			mutate.RemoveManifests(a, matcher),
    73  			updatedDescs...,
    74  		), nil
    75  
    76  	default:
    77  		return nil, fmt.Errorf("mutate.Name: %w", oci.ErrInvalidArtifact)
    78  	}
    79  }
    80  

View as plain text