-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for img labels and anno. in build create cmd
This adds support to specify labels and annotation for output image. There are 2 flags added for the `build create` command - output-image-label - output-image-annotation Signed-off-by: Shivam Mukhade <[email protected]>
- Loading branch information
Shivam Mukhade
committed
Nov 2, 2021
1 parent
d04b9ea
commit 3522d4e
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package flags | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// MapValue implements pflag.Value interface, in order to store key-value | ||
// pairs used on Shipwright's BuildSpec which have map[string]string as field type. | ||
type MapValue struct { | ||
kvMap map[string]string | ||
} | ||
|
||
// String prints out the string representation of the map. | ||
func (m *MapValue) String() string { | ||
slice := []string{} | ||
for k, v := range m.kvMap { | ||
slice = append(slice, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
csv, _ := writeAsCSV(slice) | ||
return fmt.Sprintf("[%s]", csv) | ||
} | ||
|
||
// Set receives a key-value entry separated by equal sign ("="). | ||
func (m *MapValue) Set(value string) error { | ||
k, v, err := splitKeyValue(value) | ||
if err != nil { | ||
return err | ||
} | ||
m.kvMap[k] = v | ||
return nil | ||
} | ||
|
||
// Type analogous to the pflag "stringArray" type, where each flag entry will be translated to a | ||
// single array (slice) entry, therefore the comma (",") is accepted as part of the value, as any | ||
// other special character. | ||
func (c *MapValue) Type() string { | ||
return "stringArray" | ||
} | ||
|
||
// NewMapValue instantiate a MapValue sharing the map. | ||
func NewMapValue(m map[string]string) *MapValue { | ||
return &MapValue{kvMap: m} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package flags | ||
|
||
import ( | ||
"testing" | ||
|
||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
|
||
o "github.com/onsi/gomega" | ||
) | ||
|
||
func TestNewMapValue(t *testing.T) { | ||
g := o.NewGomegaWithT(t) | ||
|
||
spec := &buildv1alpha1.BuildSpec{Output: buildv1alpha1.Image{ | ||
Labels: map[string]string{}, | ||
}} | ||
c := NewMapValue(spec.Output.Labels) | ||
|
||
// expect error when key-value is not split by equal sign | ||
err := c.Set("a") | ||
g.Expect(err).NotTo(o.BeNil()) | ||
|
||
// setting a simple key-value entry | ||
err = c.Set("a=b") | ||
g.Expect(err).To(o.BeNil()) | ||
g.Expect(len(spec.Output.Labels)).To(o.Equal(1)) | ||
g.Expect(spec.Output.Labels["a"]).To(o.Equal("b")) | ||
|
||
// setting a key-value entry with special characters | ||
err = c.Set("b=c,d,e=f") | ||
g.Expect(err).To(o.BeNil()) | ||
g.Expect(len(spec.Output.Labels)).To(o.Equal(2)) | ||
g.Expect(spec.Output.Labels["b"]).To(o.Equal("c,d,e=f")) | ||
|
||
// setting a key-value entry with space on it | ||
err = c.Set("c=d e") | ||
g.Expect(err).To(o.BeNil()) | ||
g.Expect(len(spec.Output.Labels)).To(o.Equal(3)) | ||
g.Expect(spec.Output.Labels["c"]).To(o.Equal("d e")) | ||
|
||
// making sure the string representation produced is as expected | ||
s := c.String() | ||
g.Expect(s).To(o.Equal("[a=b,\"b=c,d,e=f\",c=d e]")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bats | ||
|
||
source test/e2e/helpers.sh | ||
|
||
setup() { | ||
load 'bats/support/load' | ||
load 'bats/assert/load' | ||
load 'bats/file/load' | ||
} | ||
|
||
teardown() { | ||
run kubectl delete builds.shipwright.io --all | ||
run kubectl delete buildruns.shipwright.io --all | ||
} | ||
|
||
@test "shp output image labels and annotation lifecycle" { | ||
# generate random names for our build and buildrun | ||
build_name=$(random_name) | ||
buildrun_name=$(random_name) | ||
|
||
# create a Build with a label and an annotation | ||
run shp build create ${build_name} --source-url=https://github.com/shipwright-io/sample-go --output-image=my-image --output-image-label=foo=bar --output-image-annotation=created-by=shipwright | ||
assert_success | ||
|
||
# ensure that the build was successfully created | ||
assert_output --partial "Created build \"${build_name}\"" | ||
|
||
# get the yaml for the Build object | ||
run kubectl get builds.shipwright.io/${build_name} -o yaml | ||
assert_success | ||
|
||
# ensure that the label and annotation were inserted into the Build object | ||
assert_output --partial "foo: bar" | ||
assert_output --partial "created-by: shipwright" | ||
} |