Skip to content

Commit 2e3a503

Browse files
committed
support mutating scratch images
1 parent 8dadbe7 commit 2e3a503

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

cmd/crane/cmd/mutate.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import (
2020
"strings"
2121

2222
"github.com/google/go-containerregistry/pkg/crane"
23+
"github.com/google/go-containerregistry/pkg/logs"
2324
"github.com/google/go-containerregistry/pkg/name"
2425
v1 "github.com/google/go-containerregistry/pkg/v1"
26+
"github.com/google/go-containerregistry/pkg/v1/empty"
2527
"github.com/google/go-containerregistry/pkg/v1/mutate"
28+
"github.com/google/go-containerregistry/pkg/v1/types"
2629
"github.com/spf13/cobra"
2730
)
2831

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

4347
mutateCmd := &cobra.Command{
4448
Use: "mutate",
4549
Short: "Modify image labels and annotations. The container must be pushed to a registry, and the manifest is updated there.",
4650
Args: cobra.ExactArgs(1),
4751
RunE: func(c *cobra.Command, args []string) error {
48-
// Pull image and get config.
4952
ref := args[0]
50-
51-
if len(annotations) != 0 {
52-
desc, err := crane.Head(ref, *options...)
53-
if err != nil {
54-
return err
53+
var img v1.Image
54+
var err error
55+
56+
if ref == "scratch" {
57+
// Use an empty image.
58+
logs.Warn.Printf("scratch base specified, using empty image")
59+
img = empty.Image
60+
if ociEmptyBase {
61+
img = mutate.MediaType(img, types.OCIManifestSchema1)
62+
img = mutate.ConfigMediaType(img, types.OCIConfigJSON)
5563
}
56-
if desc.MediaType.IsIndex() {
57-
return errors.New("mutating annotations on an index is not yet supported")
64+
} else {
65+
// Pull image and get config.
66+
if len(annotations) != 0 {
67+
desc, err := crane.Head(ref, *options...)
68+
if err != nil {
69+
return err
70+
}
71+
if desc.MediaType.IsIndex() {
72+
return errors.New("mutating annotations on an index is not yet supported")
73+
}
5874
}
59-
}
6075

61-
if newRepo != "" && newRef != "" {
62-
return errors.New("repository can't be set when a tag is specified")
63-
}
76+
if newRepo != "" && newRef != "" {
77+
return errors.New("repository can't be set when a tag is specified")
78+
}
6479

65-
img, err := crane.Pull(ref, *options...)
66-
if err != nil {
67-
return fmt.Errorf("pulling %s: %w", ref, err)
80+
img, err = crane.Pull(ref, *options...)
81+
if err != nil {
82+
return fmt.Errorf("pulling %s: %w", ref, err)
83+
}
6884
}
85+
6986
if len(newLayers) != 0 {
7087
img, err = crane.Append(img, newLayers...)
7188
if err != nil {
@@ -183,6 +200,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
183200
mutateCmd.Flags().StringVarP(&user, "user", "u", "", "New user to set")
184201
mutateCmd.Flags().StringVarP(&workdir, "workdir", "w", "", "New working dir to set")
185202
mutateCmd.Flags().StringSliceVar(&ports, "exposed-ports", nil, "New ports to expose")
203+
mutateCmd.Flags().BoolVar(&ociEmptyBase, "oci-empty-base", false, "If true, scratch base image will have OCI media types instead of Docker")
204+
186205
return mutateCmd
187206
}
188207

0 commit comments

Comments
 (0)