Skip to content

Commit d9262c4

Browse files
committed
[issue-345] adds replace-additional-tag flag to image patch/save
Signed-off-by: Caroline Scherf <fcaroline@vmware.com>
1 parent 56623c5 commit d9262c4

8 files changed

Lines changed: 90 additions & 15 deletions

File tree

docs/kp_image_patch.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
4949
### Options
5050

5151
```
52-
--additional-tag stringArray additional tags to push the OCI image to
52+
--additional-tag stringArray adds additional tags to push the OCI image to
5353
--blob string source code blob url
5454
--builder string builder name
5555
--cache-size string cache size as a kubernetes quantity
@@ -76,6 +76,7 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
7676
The APIVersion of the outputted resources will always be the latest APIVersion known to kp (currently: v1alpha2).
7777
--registry-ca-cert-path string add CA certificate for registry API (format: /tmp/ca.crt)
7878
--registry-verify-certs set whether to verify server's certificate chain and host name (default true)
79+
--replace-additional-tag stringArray replaces all additional tags to push the OCI image to
7980
--service-account string service account name to use
8081
-s, --service-binding stringArray build time service bindings to add/replace
8182
--sub-path string build code at the sub path located within the source code directory

docs/kp_image_save.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
5252
### Options
5353

5454
```
55-
--additional-tag stringArray additional tags to push the OCI image to
55+
--additional-tag stringArray adds additional tags to push the OCI image to
5656
--blob string source code blob url
5757
-b, --builder string builder name
5858
--cache-size string cache size as a kubernetes quantity (default "2G")
@@ -80,6 +80,7 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
8080
The APIVersion of the outputted resources will always be the latest APIVersion known to kp (currently: v1alpha2).
8181
--registry-ca-cert-path string add CA certificate for registry API (format: /tmp/ca.crt)
8282
--registry-verify-certs set whether to verify server's certificate chain and host name (default true)
83+
--replace-additional-tag stringArray replaces all additional tags to push the OCI image to
8384
--service-account string service account name to use
8485
-s, --service-binding stringArray build time service bindings to add/replace
8586
--sub-path string build code at the sub path located within the source code directory

pkg/commands/image/patch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
103103
return nil
104104
},
105105
}
106-
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "additional tags to push the OCI image to")
106+
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "adds additional tags to push the OCI image to")
107+
cmd.Flags().StringArrayVar(&factory.ReplaceAdditionalTags, "replace-additional-tag", []string{}, "replaces all additional tags to push the OCI image to")
107108
cmd.Flags().StringArrayVar(&factory.DeleteAdditionalTags, "delete-additional-tag", []string{}, "additional tags to remove")
108109
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "kubernetes namespace")
109110
cmd.Flags().StringVar(&factory.GitRepo, "git", "", "git repository url")

pkg/commands/image/patch_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,47 @@ Image Resource "some-image" patched
287287

288288
assert.Len(t, fakeImageWaiter.Calls, 0)
289289
})
290+
291+
it("add and delete additional tags is compatible", func() {
292+
testhelpers.CommandTest{
293+
Objects: []runtime.Object{
294+
existingImage,
295+
},
296+
Args: []string{
297+
"some-image",
298+
"--additional-tag", "test-new-tag",
299+
"--delete-additional-tag", "some-other-tag",
300+
},
301+
ExpectedOutput: `Patching Image Resource...
302+
Image Resource "some-image" patched
303+
`,
304+
ExpectPatches: []string{
305+
`{"spec":{"additionalTags":["test-new-tag"]}}`,
306+
},
307+
}.TestKpack(t, cmdFunc)
308+
309+
assert.Len(t, fakeImageWaiter.Calls, 0)
310+
})
311+
312+
it("replace additional tags", func() {
313+
testhelpers.CommandTest{
314+
Objects: []runtime.Object{
315+
existingImage,
316+
},
317+
Args: []string{
318+
"some-image",
319+
"--replace-additional-tag", "replace-this-tag",
320+
},
321+
ExpectedOutput: `Patching Image Resource...
322+
Image Resource "some-image" patched
323+
`,
324+
ExpectPatches: []string{
325+
`{"spec":{"additionalTags":["replace-this-tag"]}}`,
326+
},
327+
}.TestKpack(t, cmdFunc)
328+
329+
assert.Len(t, fakeImageWaiter.Calls, 0)
330+
})
290331
})
291332

292333
when("patching env vars", func() {

pkg/commands/image/save.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
119119
},
120120
}
121121
cmd.Flags().StringVarP(&tag, "tag", "t", "", "registry location where the image will be created")
122-
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "additional tags to push the OCI image to")
122+
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "adds additional tags to push the OCI image to")
123+
cmd.Flags().StringArrayVar(&factory.ReplaceAdditionalTags, "replace-additional-tag", []string{}, "replaces all additional tags to push the OCI image to")
123124
cmd.Flags().StringArrayVar(&factory.DeleteAdditionalTags, "delete-additional-tag", []string{}, "additional tags to remove")
124125
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "kubernetes namespace")
125126
cmd.Flags().StringVar(&factory.GitRepo, "git", "", "git repository url")

pkg/image/factory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Factory struct {
5757
DeleteServiceBinding []string
5858
Printer Printer
5959
ServiceAccount string
60+
ReplaceAdditionalTags []string
6061
}
6162

6263
func (f *Factory) MakeImage(name, namespace, tag string) (*v1alpha2.Image, error) {

pkg/image/update_factory.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ func (f *Factory) UpdateImage(img *v1alpha2.Image) (*v1alpha2.Image, error) {
3131
}
3232

3333
f.setAdditionalTags(updatedImage)
34+
if err != nil {
35+
return nil, err
36+
}
3437

3538
err = f.setCacheSize(updatedImage)
3639
if err != nil {
@@ -123,6 +126,10 @@ func (f *Factory) validateEnvVars(img *v1alpha2.Image) error {
123126
}
124127

125128
func (f *Factory) validateAdditionalTags(img *v1alpha2.Image) error {
129+
if len(f.ReplaceAdditionalTags) != 0 && (len(f.DeleteAdditionalTags) != 0 || len(f.AdditionalTags) != 0) {
130+
return errors.Errorf("replace-additional-tag is not compatible with additional-tag and delete-additional-tag flag")
131+
}
132+
126133
for _, deleteTag := range f.DeleteAdditionalTags {
127134
found := false
128135

@@ -231,21 +238,25 @@ func (f *Factory) setCacheSize(image *v1alpha2.Image) error {
231238
}
232239

233240
func (f *Factory) setAdditionalTags(image *v1alpha2.Image) {
234-
for _, additionalTagToDelete := range f.DeleteAdditionalTags {
235-
for i, at := range image.Spec.AdditionalTags {
236-
if at == additionalTagToDelete {
237-
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags[:i], image.Spec.AdditionalTags[i+1:]...)
238-
break
241+
if len(f.ReplaceAdditionalTags) != 0 {
242+
image.Spec.AdditionalTags = f.ReplaceAdditionalTags
243+
} else {
244+
for _, additionalTagToDelete := range f.DeleteAdditionalTags {
245+
for i, at := range image.Spec.AdditionalTags {
246+
if at == additionalTagToDelete {
247+
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags[:i], image.Spec.AdditionalTags[i+1:]...)
248+
break
249+
}
239250
}
240251
}
241-
}
242252

243-
for _, additionalTag := range f.AdditionalTags {
244-
if tagExists(additionalTag, image.Spec.AdditionalTags) {
245-
continue
246-
}
253+
for _, additionalTag := range f.AdditionalTags {
254+
if tagExists(additionalTag, image.Spec.AdditionalTags) {
255+
continue
256+
}
247257

248-
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags, additionalTag)
258+
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags, additionalTag)
259+
}
249260
}
250261
}
251262

pkg/image/update_factory_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ func testPatchFactory(t *testing.T, when spec.G, it spec.S) {
179179
})
180180
})
181181

182+
when("ReplaceAdditionalTag and DeleteAdditionalTags used together", func() {
183+
it("returns an error message", func() {
184+
factory.DeleteAdditionalTags = []string{"bar"}
185+
factory.ReplaceAdditionalTags = []string{"foo"}
186+
_, err := factory.UpdateImage(img)
187+
require.EqualError(t, err, "replace-additional-tag is not compatible with additional-tag and delete-additional-tag flag")
188+
})
189+
})
190+
191+
when("ReplaceAdditionalTag and AdditionalTags used together", func() {
192+
it("returns an error message", func() {
193+
factory.AdditionalTags = []string{"foo"}
194+
factory.ReplaceAdditionalTags = []string{"bar"}
195+
_, err := factory.UpdateImage(img)
196+
require.EqualError(t, err, "replace-additional-tag is not compatible with additional-tag and delete-additional-tag flag")
197+
})
198+
})
199+
182200
when("the image.spec.build is nil", func() {
183201
it("does not panic", func() {
184202
img.Spec.Build = nil

0 commit comments

Comments
 (0)