github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/manifest_annotate.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/google/go-containerregistry/pkg/name"
     8  
     9  	"github.com/buildpacks/pack/internal/style"
    10  	"github.com/buildpacks/pack/pkg/image"
    11  )
    12  
    13  type ManifestAnnotateOptions struct {
    14  	// Image index we want to update
    15  	IndexRepoName string
    16  
    17  	// Name of image within the index that we wish to update
    18  	RepoName string
    19  
    20  	// 'os' of the image we wish to update in the image index
    21  	OS string
    22  
    23  	// 'architecture' of the image we wish to update in the image index
    24  	OSArch string
    25  
    26  	// 'os variant' of the image we wish to update in the image index
    27  	OSVariant string
    28  
    29  	// 'annotations' of the image we wish to update in the image index
    30  	Annotations map[string]string
    31  }
    32  
    33  // AnnotateManifest implements commands.PackClient.
    34  func (c *Client) AnnotateManifest(ctx context.Context, opts ManifestAnnotateOptions) error {
    35  	idx, err := c.indexFactory.LoadIndex(opts.IndexRepoName)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	imageRef, err := name.ParseReference(opts.RepoName, name.WeakValidation)
    41  	if err != nil {
    42  		return fmt.Errorf("'%s' is not a valid image reference: %s", opts.RepoName, err)
    43  	}
    44  
    45  	imageToAnnotate, err := c.imageFetcher.Fetch(ctx, imageRef.Name(), image.FetchOptions{Daemon: false})
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	hash, err := imageToAnnotate.Identifier()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	digest, err := name.NewDigest(hash.String())
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	if opts.OS != "" {
    61  		if err = idx.SetOS(digest, opts.OS); err != nil {
    62  			return fmt.Errorf("failed to set the 'os' for %s: %w", style.Symbol(opts.RepoName), err)
    63  		}
    64  	}
    65  	if opts.OSArch != "" {
    66  		if err = idx.SetArchitecture(digest, opts.OSArch); err != nil {
    67  			return fmt.Errorf("failed to set the 'arch' for %s: %w", style.Symbol(opts.RepoName), err)
    68  		}
    69  	}
    70  	if opts.OSVariant != "" {
    71  		if err = idx.SetVariant(digest, opts.OSVariant); err != nil {
    72  			return fmt.Errorf("failed to set the 'os variant' for %s: %w", style.Symbol(opts.RepoName), err)
    73  		}
    74  	}
    75  	if len(opts.Annotations) != 0 {
    76  		if err = idx.SetAnnotations(digest, opts.Annotations); err != nil {
    77  			return fmt.Errorf("failed to set the 'annotations' for %s: %w", style.Symbol(opts.RepoName), err)
    78  		}
    79  	}
    80  
    81  	if err = idx.SaveDir(); err != nil {
    82  		return fmt.Errorf("failed to save manifest list %s to local storage: %w", style.Symbol(opts.RepoName), err)
    83  	}
    84  
    85  	c.logger.Infof("Successfully annotated image %s in index %s", style.Symbol(opts.RepoName), style.Symbol(opts.IndexRepoName))
    86  	return nil
    87  }