@@ -20,9 +20,12 @@ import (
20
20
"strings"
21
21
22
22
"github.com/google/go-containerregistry/pkg/crane"
23
+ "github.com/google/go-containerregistry/pkg/logs"
23
24
"github.com/google/go-containerregistry/pkg/name"
24
25
v1 "github.com/google/go-containerregistry/pkg/v1"
26
+ "github.com/google/go-containerregistry/pkg/v1/empty"
25
27
"github.com/google/go-containerregistry/pkg/v1/mutate"
28
+ "github.com/google/go-containerregistry/pkg/v1/types"
26
29
"github.com/spf13/cobra"
27
30
)
28
31
@@ -39,33 +42,47 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
39
42
var user string
40
43
var workdir string
41
44
var ports []string
45
+ var ociEmptyBase bool
42
46
43
47
mutateCmd := & cobra.Command {
44
48
Use : "mutate" ,
45
49
Short : "Modify image labels and annotations. The container must be pushed to a registry, and the manifest is updated there." ,
46
50
Args : cobra .ExactArgs (1 ),
47
51
RunE : func (c * cobra.Command , args []string ) error {
48
- // Pull image and get config.
49
52
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 )
55
63
}
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
+ }
58
74
}
59
- }
60
75
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
+ }
64
79
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
+ }
68
84
}
85
+
69
86
if len (newLayers ) != 0 {
70
87
img , err = crane .Append (img , newLayers ... )
71
88
if err != nil {
@@ -183,6 +200,8 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
183
200
mutateCmd .Flags ().StringVarP (& user , "user" , "u" , "" , "New user to set" )
184
201
mutateCmd .Flags ().StringVarP (& workdir , "workdir" , "w" , "" , "New working dir to set" )
185
202
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
+
186
205
return mutateCmd
187
206
}
188
207
0 commit comments