@@ -115,9 +115,33 @@ func Config(base v1.Image, cfg v1.Config) (v1.Image, error) {
115115 return ConfigFile (base , cf )
116116}
117117
118- // Annotatable represents a manifest that can carry annotations.
119- type Annotatable interface {
120- partial.WithRawManifest
118+ // Subject mutates the subject on an image or index manifest.
119+ //
120+ // The input is expected to be a v1.Image or v1.ImageIndex, and
121+ // returns the same type. You can type-assert the result like so:
122+ //
123+ // img := Subject(empty.Image, subj).(v1.Image)
124+ //
125+ // Or for an index:
126+ //
127+ // idx := Subject(empty.Index, subj).(v1.ImageIndex)
128+ //
129+ // If the input is not an Image or ImageIndex, the result will
130+ // attempt to lazily annotate the raw manifest.
131+ func Subject (f partial.WithRawManifest , subject v1.Descriptor ) partial.WithRawManifest {
132+ if img , ok := f .(v1.Image ); ok {
133+ return & image {
134+ base : img ,
135+ subject : & subject ,
136+ }
137+ }
138+ if idx , ok := f .(v1.ImageIndex ); ok {
139+ return & index {
140+ base : idx ,
141+ subject : & subject ,
142+ }
143+ }
144+ return arbitraryRawManifest {a : f , subject : & subject }
121145}
122146
123147// Annotations mutates the annotations on an annotatable image or index manifest.
@@ -137,7 +161,7 @@ type Annotatable interface {
137161//
138162// If the input Annotatable is not an Image or ImageIndex, the result will
139163// attempt to lazily annotate the raw manifest.
140- func Annotations (f Annotatable , anns map [string ]string ) Annotatable {
164+ func Annotations (f partial. WithRawManifest , anns map [string ]string ) partial. WithRawManifest {
141165 if img , ok := f .(v1.Image ); ok {
142166 return & image {
143167 base : img ,
@@ -150,12 +174,13 @@ func Annotations(f Annotatable, anns map[string]string) Annotatable {
150174 annotations : anns ,
151175 }
152176 }
153- return arbitraryRawManifest {f , anns }
177+ return arbitraryRawManifest {a : f , anns : anns }
154178}
155179
156180type arbitraryRawManifest struct {
157- a Annotatable
158- anns map [string ]string
181+ a partial.WithRawManifest
182+ anns map [string ]string
183+ subject * v1.Descriptor
159184}
160185
161186func (a arbitraryRawManifest ) RawManifest () ([]byte , error ) {
@@ -178,6 +203,9 @@ func (a arbitraryRawManifest) RawManifest() ([]byte, error) {
178203 } else {
179204 m ["annotations" ] = a .anns
180205 }
206+ if a .subject != nil {
207+ m ["subject" ] = a .subject
208+ }
181209 return json .Marshal (m )
182210}
183211
0 commit comments