Skip to content

Commit 919b0e2

Browse files
author
Daniel Chen
authored
Merge pull request #350 from buildpacks-community/issue-345-configure-additional-tags
[issue-345] adds `replace-additional-tag` flag to image patch/save
2 parents 92f477c + fdf77eb commit 919b0e2

8 files changed

Lines changed: 97 additions & 15 deletions

File tree

docs/kp_image_patch.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ The flags for this command determine how the build will retrieve source code:
1818
Local source code will be pushed to the same registry as the existing image resource tag.
1919
Therefore, you must have credentials to access the registry on your machine.
2020

21+
All tags found under Image.spec.additionalTags will be added to your built OCI image.
22+
To append to the list of tags that will be added to a built image, use the "additional-tag" flag.
23+
To remove a tag from the list of tags that will be added to a built image, use the "delete-additional-tag".
24+
To replace the entire list of tags, use the "replace-additional-tag".
25+
2126
Environment variables may be provided by using the "--env" flag or deleted by using the "--delete-env" flag.
2227
For each environment variable, supply the "--env" flag followed by the key value pair.
2328
For example, "--env key1=value1 --env key2=value2 --delete-env key3 --delete-env key3".
@@ -49,7 +54,7 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
4954
### Options
5055

5156
```
52-
--additional-tag stringArray additional tags to push the OCI image to
57+
--additional-tag stringArray adds additional tags to push the OCI image to
5358
--blob string source code blob url
5459
--builder string builder name
5560
--cache-size string cache size as a kubernetes quantity
@@ -77,6 +82,7 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
7782
The APIVersion of the outputted resources will always be the latest APIVersion known to kp (currently: v1alpha2).
7883
--registry-ca-cert-path string add CA certificate for registry API (format: /tmp/ca.crt)
7984
--registry-verify-certs set whether to verify server's certificate chain and host name (default true)
85+
--replace-additional-tag stringArray replaces all additional tags to push the OCI image to
8086
--service-account string service account name to use
8187
-s, --service-binding stringArray build time service bindings to add/replace
8288
--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")
@@ -81,6 +81,7 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
8181
The APIVersion of the outputted resources will always be the latest APIVersion known to kp (currently: v1alpha2).
8282
--registry-ca-cert-path string add CA certificate for registry API (format: /tmp/ca.crt)
8383
--registry-verify-certs set whether to verify server's certificate chain and host name (default true)
84+
--replace-additional-tag stringArray replaces all additional tags to push the OCI image to
8485
--service-account string service account name to use
8586
-s, --service-binding stringArray build time service bindings to add/replace
8687
--sub-path string build code at the sub path located within the source code directory

pkg/commands/image/patch.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ The flags for this command determine how the build will retrieve source code:
4343
Local source code will be pushed to the same registry as the existing image resource tag.
4444
Therefore, you must have credentials to access the registry on your machine.
4545
46+
All tags found under Image.spec.additionalTags will be added to your built OCI image.
47+
To append to the list of tags that will be added to a built image, use the "additional-tag" flag.
48+
To remove a tag from the list of tags that will be added to a built image, use the "delete-additional-tag".
49+
To replace the entire list of tags, use the "replace-additional-tag".
50+
4651
Environment variables may be provided by using the "--env" flag or deleted by using the "--delete-env" flag.
4752
For each environment variable, supply the "--env" flag followed by the key value pair.
4853
For example, "--env key1=value1 --env key2=value2 --delete-env key3 --delete-env key3".
@@ -103,7 +108,8 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
103108
return nil
104109
},
105110
}
106-
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "additional tags to push the OCI image to")
111+
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "adds additional tags to push the OCI image to")
112+
cmd.Flags().StringArrayVar(&factory.ReplaceAdditionalTags, "replace-additional-tag", []string{}, "replaces all additional tags to push the OCI image to")
107113
cmd.Flags().StringArrayVar(&factory.DeleteAdditionalTags, "delete-additional-tag", []string{}, "additional tags to remove")
108114
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "kubernetes namespace")
109115
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
@@ -307,6 +307,47 @@ Image Resource "some-image" patched
307307

308308
assert.Len(t, fakeImageWaiter.Calls, 0)
309309
})
310+
311+
it("add and delete additional tags is compatible", func() {
312+
testhelpers.CommandTest{
313+
Objects: []runtime.Object{
314+
existingImage,
315+
},
316+
Args: []string{
317+
"some-image",
318+
"--additional-tag", "test-new-tag",
319+
"--delete-additional-tag", "some-other-tag",
320+
},
321+
ExpectedOutput: `Patching Image Resource...
322+
Image Resource "some-image" patched
323+
`,
324+
ExpectPatches: []string{
325+
`{"spec":{"additionalTags":["test-new-tag"]}}`,
326+
},
327+
}.TestKpack(t, cmdFunc)
328+
329+
assert.Len(t, fakeImageWaiter.Calls, 0)
330+
})
331+
332+
it("replace additional tags", func() {
333+
testhelpers.CommandTest{
334+
Objects: []runtime.Object{
335+
existingImage,
336+
},
337+
Args: []string{
338+
"some-image",
339+
"--replace-additional-tag", "replace-this-tag",
340+
},
341+
ExpectedOutput: `Patching Image Resource...
342+
Image Resource "some-image" patched
343+
`,
344+
ExpectPatches: []string{
345+
`{"spec":{"additionalTags":["replace-this-tag"]}}`,
346+
},
347+
}.TestKpack(t, cmdFunc)
348+
349+
assert.Len(t, fakeImageWaiter.Calls, 0)
350+
})
310351
})
311352

312353
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
@@ -60,6 +60,7 @@ type Factory struct {
6060
DeleteServiceBinding []string
6161
Printer Printer
6262
ServiceAccount string
63+
ReplaceAdditionalTags []string
6364
}
6465

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

pkg/image/update_factory.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func (f *Factory) validateEnvVars(img *v1alpha2.Image) error {
127127
}
128128

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

@@ -235,21 +239,25 @@ func (f *Factory) setCacheSize(image *v1alpha2.Image) error {
235239
}
236240

237241
func (f *Factory) setAdditionalTags(image *v1alpha2.Image) {
238-
for _, additionalTagToDelete := range f.DeleteAdditionalTags {
239-
for i, at := range image.Spec.AdditionalTags {
240-
if at == additionalTagToDelete {
241-
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags[:i], image.Spec.AdditionalTags[i+1:]...)
242-
break
242+
if len(f.ReplaceAdditionalTags) != 0 {
243+
image.Spec.AdditionalTags = f.ReplaceAdditionalTags
244+
} else {
245+
for _, additionalTagToDelete := range f.DeleteAdditionalTags {
246+
for i, at := range image.Spec.AdditionalTags {
247+
if at == additionalTagToDelete {
248+
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags[:i], image.Spec.AdditionalTags[i+1:]...)
249+
break
250+
}
243251
}
244252
}
245-
}
246253

247-
for _, additionalTag := range f.AdditionalTags {
248-
if tagExists(additionalTag, image.Spec.AdditionalTags) {
249-
continue
250-
}
254+
for _, additionalTag := range f.AdditionalTags {
255+
if tagExists(additionalTag, image.Spec.AdditionalTags) {
256+
continue
257+
}
251258

252-
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags, additionalTag)
259+
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags, additionalTag)
260+
}
253261
}
254262
}
255263

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)