diff --git a/cmd/crane/cmd/mutate.go b/cmd/crane/cmd/mutate.go index 4fc9b1dc0..faa809813 100644 --- a/cmd/crane/cmd/mutate.go +++ b/cmd/crane/cmd/mutate.go @@ -22,7 +22,9 @@ import ( "github.com/google/go-containerregistry/pkg/crane" "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" ) @@ -40,6 +42,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { var workdir string var ports []string var newPlatform string + var ociEmptyBase bool mutateCmd := &cobra.Command{ Use: "mutate", @@ -65,7 +68,14 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { img, err := crane.Pull(ref, *options...) if err != nil { - return fmt.Errorf("pulling %s: %w", ref, err) + if ociEmptyBase { + fmt.Println("base unspecified, using empty image") + img = empty.Image + img = mutate.MediaType(img, types.OCIManifestSchema1) + img = mutate.ConfigMediaType(img, types.OCIConfigJSON) + } else { + return fmt.Errorf("pulling %s: %w", ref, err) + } } if len(newLayers) != 0 { img, err = crane.Append(img, newLayers...) @@ -198,6 +208,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command { mutateCmd.Flags().StringSliceVar(&ports, "exposed-ports", nil, "New ports to expose") // Using "set-platform" to avoid clobbering "platform" persistent flag. mutateCmd.Flags().StringVar(&newPlatform, "set-platform", "", "New platform to set in the form os/arch[/variant][:osversion] (e.g. linux/amd64)") + mutateCmd.Flags().BoolVar(&ociEmptyBase, "oci-empty-base", false, "If true, use an empty OCI image when the base image is not found") return mutateCmd } diff --git a/cmd/crane/doc/crane_mutate.md b/cmd/crane/doc/crane_mutate.md index f97d33df1..d46ce8b67 100644 --- a/cmd/crane/doc/crane_mutate.md +++ b/cmd/crane/doc/crane_mutate.md @@ -17,6 +17,7 @@ crane mutate [flags] --exposed-ports strings New ports to expose -h, --help help for mutate -l, --label stringToString New labels to add (default []) + --oci-empty-base If true, use an empty OCI image when the base image is not found -o, --output string Path to new tarball of resulting image --repo string Repository to push the mutated image to. If provided, push by digest to this repository. --set-platform string New platform to set in the form os/arch[/variant][:osversion] (e.g. linux/amd64)