Skip to content

Commit

Permalink
support mutating scratch images
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergig committed Feb 22, 2024
1 parent 8dadbe7 commit 2e3a503
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions cmd/crane/cmd/mutate.go
Expand Up @@ -20,9 +20,12 @@ import (
"strings"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/spf13/cobra"
)

Expand All @@ -39,33 +42,47 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
var user string
var workdir string
var ports []string
var ociEmptyBase bool

mutateCmd := &cobra.Command{
Use: "mutate",
Short: "Modify image labels and annotations. The container must be pushed to a registry, and the manifest is updated there.",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
// Pull image and get config.
ref := args[0]

if len(annotations) != 0 {
desc, err := crane.Head(ref, *options...)
if err != nil {
return err
var img v1.Image
var err error

if ref == "scratch" {
// Use an empty image.
logs.Warn.Printf("scratch base specified, using empty image")
img = empty.Image
if ociEmptyBase {
img = mutate.MediaType(img, types.OCIManifestSchema1)
img = mutate.ConfigMediaType(img, types.OCIConfigJSON)
}
if desc.MediaType.IsIndex() {
return errors.New("mutating annotations on an index is not yet supported")
} else {
// Pull image and get config.
if len(annotations) != 0 {
desc, err := crane.Head(ref, *options...)
if err != nil {
return err
}
if desc.MediaType.IsIndex() {
return errors.New("mutating annotations on an index is not yet supported")
}
}
}

if newRepo != "" && newRef != "" {
return errors.New("repository can't be set when a tag is specified")
}
if newRepo != "" && newRef != "" {
return errors.New("repository can't be set when a tag is specified")
}

img, err := crane.Pull(ref, *options...)
if err != nil {
return fmt.Errorf("pulling %s: %w", ref, err)
img, err = crane.Pull(ref, *options...)
if err != nil {
return fmt.Errorf("pulling %s: %w", ref, err)
}
}

if len(newLayers) != 0 {
img, err = crane.Append(img, newLayers...)
if err != nil {
Expand Down Expand Up @@ -183,6 +200,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
mutateCmd.Flags().StringVarP(&user, "user", "u", "", "New user to set")
mutateCmd.Flags().StringVarP(&workdir, "workdir", "w", "", "New working dir to set")
mutateCmd.Flags().StringSliceVar(&ports, "exposed-ports", nil, "New ports to expose")
mutateCmd.Flags().BoolVar(&ociEmptyBase, "oci-empty-base", false, "If true, scratch base image will have OCI media types instead of Docker")

return mutateCmd
}

Expand Down

0 comments on commit 2e3a503

Please sign in to comment.