Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/kp_image_patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ The flags for this command determine how the build will retrieve source code:
Local source code will be pushed to the same registry as the existing image resource tag.
Therefore, you must have credentials to access the registry on your machine.

All tags found under Image.spec.additionalTags will be added to your built OCI image.
To append to the list of tags that will be added to a built image, use the "additional-tag" flag.
To remove a tag from the list of tags that will be added to a built image, use the "delete-additional-tag".
To replace the entire list of tags, use the "replace-additional-tag".

Environment variables may be provided by using the "--env" flag or deleted by using the "--delete-env" flag.
For each environment variable, supply the "--env" flag followed by the key value pair.
For example, "--env key1=value1 --env key2=value2 --delete-env key3 --delete-env key3".
Expand Down Expand Up @@ -49,7 +54,7 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
### Options

```
--additional-tag stringArray additional tags to push the OCI image to
--additional-tag stringArray adds additional tags to push the OCI image to
--blob string source code blob url
--builder string builder name
--cache-size string cache size as a kubernetes quantity
Expand All @@ -76,6 +81,7 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
The APIVersion of the outputted resources will always be the latest APIVersion known to kp (currently: v1alpha2).
--registry-ca-cert-path string add CA certificate for registry API (format: /tmp/ca.crt)
--registry-verify-certs set whether to verify server's certificate chain and host name (default true)
--replace-additional-tag stringArray replaces all additional tags to push the OCI image to
--service-account string service account name to use
-s, --service-binding stringArray build time service bindings to add/replace
--sub-path string build code at the sub path located within the source code directory
Expand Down
3 changes: 2 additions & 1 deletion docs/kp_image_save.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
### Options

```
--additional-tag stringArray additional tags to push the OCI image to
--additional-tag stringArray adds additional tags to push the OCI image to
--blob string source code blob url
-b, --builder string builder name
--cache-size string cache size as a kubernetes quantity (default "2G")
Expand Down Expand Up @@ -80,6 +80,7 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
The APIVersion of the outputted resources will always be the latest APIVersion known to kp (currently: v1alpha2).
--registry-ca-cert-path string add CA certificate for registry API (format: /tmp/ca.crt)
--registry-verify-certs set whether to verify server's certificate chain and host name (default true)
--replace-additional-tag stringArray replaces all additional tags to push the OCI image to
--service-account string service account name to use
-s, --service-binding stringArray build time service bindings to add/replace
--sub-path string build code at the sub path located within the source code directory
Expand Down
8 changes: 7 additions & 1 deletion pkg/commands/image/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ The flags for this command determine how the build will retrieve source code:
Local source code will be pushed to the same registry as the existing image resource tag.
Therefore, you must have credentials to access the registry on your machine.

All tags found under Image.spec.additionalTags will be added to your built OCI image.
To append to the list of tags that will be added to a built image, use the "additional-tag" flag.
To remove a tag from the list of tags that will be added to a built image, use the "delete-additional-tag".
To replace the entire list of tags, use the "replace-additional-tag".

Environment variables may be provided by using the "--env" flag or deleted by using the "--delete-env" flag.
For each environment variable, supply the "--env" flag followed by the key value pair.
For example, "--env key1=value1 --env key2=value2 --delete-env key3 --delete-env key3".
Expand Down Expand Up @@ -103,7 +108,8 @@ kp image patch my-image --tag my-registry.com/my-repo --blob https://my-blob-hos
return nil
},
}
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "additional tags to push the OCI image to")
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "adds additional tags to push the OCI image to")
cmd.Flags().StringArrayVar(&factory.ReplaceAdditionalTags, "replace-additional-tag", []string{}, "replaces all additional tags to push the OCI image to")
Comment thread
chenbh marked this conversation as resolved.
cmd.Flags().StringArrayVar(&factory.DeleteAdditionalTags, "delete-additional-tag", []string{}, "additional tags to remove")
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "kubernetes namespace")
cmd.Flags().StringVar(&factory.GitRepo, "git", "", "git repository url")
Expand Down
41 changes: 41 additions & 0 deletions pkg/commands/image/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,47 @@ Image Resource "some-image" patched

assert.Len(t, fakeImageWaiter.Calls, 0)
})

it("add and delete additional tags is compatible", func() {
testhelpers.CommandTest{
Objects: []runtime.Object{
existingImage,
},
Args: []string{
"some-image",
"--additional-tag", "test-new-tag",
"--delete-additional-tag", "some-other-tag",
},
ExpectedOutput: `Patching Image Resource...
Image Resource "some-image" patched
`,
ExpectPatches: []string{
`{"spec":{"additionalTags":["test-new-tag"]}}`,
},
}.TestKpack(t, cmdFunc)

assert.Len(t, fakeImageWaiter.Calls, 0)
})

it("replace additional tags", func() {
testhelpers.CommandTest{
Objects: []runtime.Object{
existingImage,
},
Args: []string{
"some-image",
"--replace-additional-tag", "replace-this-tag",
},
ExpectedOutput: `Patching Image Resource...
Image Resource "some-image" patched
`,
ExpectPatches: []string{
`{"spec":{"additionalTags":["replace-this-tag"]}}`,
},
}.TestKpack(t, cmdFunc)

assert.Len(t, fakeImageWaiter.Calls, 0)
})
})

when("patching env vars", func() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/commands/image/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ kp image save my-image --tag my-registry.com/my-repo --blob https://my-blob-host
},
}
cmd.Flags().StringVarP(&tag, "tag", "t", "", "registry location where the image will be created")
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "additional tags to push the OCI image to")
cmd.Flags().StringArrayVar(&factory.AdditionalTags, "additional-tag", []string{}, "adds additional tags to push the OCI image to")
cmd.Flags().StringArrayVar(&factory.ReplaceAdditionalTags, "replace-additional-tag", []string{}, "replaces all additional tags to push the OCI image to")
cmd.Flags().StringArrayVar(&factory.DeleteAdditionalTags, "delete-additional-tag", []string{}, "additional tags to remove")
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "kubernetes namespace")
cmd.Flags().StringVar(&factory.GitRepo, "git", "", "git repository url")
Expand Down
1 change: 1 addition & 0 deletions pkg/image/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Factory struct {
DeleteServiceBinding []string
Printer Printer
ServiceAccount string
ReplaceAdditionalTags []string
}

func (f *Factory) MakeImage(name, namespace, tag string) (*v1alpha2.Image, error) {
Expand Down
30 changes: 19 additions & 11 deletions pkg/image/update_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (f *Factory) validateEnvVars(img *v1alpha2.Image) error {
}

func (f *Factory) validateAdditionalTags(img *v1alpha2.Image) error {
if len(f.ReplaceAdditionalTags) != 0 && (len(f.DeleteAdditionalTags) != 0 || len(f.AdditionalTags) != 0) {
return errors.Errorf("replace-additional-tag is not compatible with additional-tag and delete-additional-tag flag")
}

for _, deleteTag := range f.DeleteAdditionalTags {
found := false

Expand Down Expand Up @@ -231,21 +235,25 @@ func (f *Factory) setCacheSize(image *v1alpha2.Image) error {
}

func (f *Factory) setAdditionalTags(image *v1alpha2.Image) {
for _, additionalTagToDelete := range f.DeleteAdditionalTags {
for i, at := range image.Spec.AdditionalTags {
if at == additionalTagToDelete {
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags[:i], image.Spec.AdditionalTags[i+1:]...)
break
if len(f.ReplaceAdditionalTags) != 0 {
image.Spec.AdditionalTags = f.ReplaceAdditionalTags
} else {
for _, additionalTagToDelete := range f.DeleteAdditionalTags {
for i, at := range image.Spec.AdditionalTags {
if at == additionalTagToDelete {
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags[:i], image.Spec.AdditionalTags[i+1:]...)
break
}
}
}
}

for _, additionalTag := range f.AdditionalTags {
if tagExists(additionalTag, image.Spec.AdditionalTags) {
continue
}
for _, additionalTag := range f.AdditionalTags {
if tagExists(additionalTag, image.Spec.AdditionalTags) {
continue
}

image.Spec.AdditionalTags = append(image.Spec.AdditionalTags, additionalTag)
image.Spec.AdditionalTags = append(image.Spec.AdditionalTags, additionalTag)
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/image/update_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ func testPatchFactory(t *testing.T, when spec.G, it spec.S) {
})
})

when("ReplaceAdditionalTag and DeleteAdditionalTags used together", func() {
it("returns an error message", func() {
factory.DeleteAdditionalTags = []string{"bar"}
factory.ReplaceAdditionalTags = []string{"foo"}
_, err := factory.UpdateImage(img)
require.EqualError(t, err, "replace-additional-tag is not compatible with additional-tag and delete-additional-tag flag")
})
})

when("ReplaceAdditionalTag and AdditionalTags used together", func() {
it("returns an error message", func() {
factory.AdditionalTags = []string{"foo"}
factory.ReplaceAdditionalTags = []string{"bar"}
_, err := factory.UpdateImage(img)
require.EqualError(t, err, "replace-additional-tag is not compatible with additional-tag and delete-additional-tag flag")
})
})

when("the image.spec.build is nil", func() {
it("does not panic", func() {
img.Spec.Build = nil
Expand Down