Skip to content

Commit

Permalink
Adds support for img labels and anno. in build create cmd
Browse files Browse the repository at this point in the history
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 9a759d6 commit 63693a2
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/shp/cmd/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea
}
b.Spec.Env = append(b.Spec.Env, parsedEnvs...)

labels, err := c.cmd.Flags().GetStringArray(flags.OutputImageLabelsFlag)
if err != nil {
return err
}
b.Spec.Output.Labels, err = util.StringSliceToMap(labels)
if err != nil {
return err
}

annotations, err := c.cmd.Flags().GetStringArray(flags.OutputImageAnnotationsFlag)
if err != nil {
return err
}
b.Spec.Output.Annotations, err = util.StringSliceToMap(annotations)
if err != nil {
return err
}

flags.SanitizeBuildSpec(&b.Spec)

clientset, err := params.ShipwrightClientSet()
Expand Down
2 changes: 2 additions & 0 deletions pkg/shp/flags/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
imageFlags(flags, "output", &spec.Output)
timeoutFlags(flags, spec.Timeout)
envFlags(flags, spec.Env)
imageLabelsFlags(flags, spec.Output.Labels)
imageAnnotationsFlags(flags, spec.Output.Annotations)

return spec
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/shp/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const (
ServiceAccountGenerateFlag = "sa-generate"
// TimeoutFlag command-line flag.
TimeoutFlag = "timeout"
// OutputImageLabelsFlag command-line flag.
OutputImageLabelsFlag = "output-image-label"
// OutputImageAnnotationsFlag command-line flag.
OutputImageAnnotationsFlag = "output-image-annotation"
)

// sourceFlags flags for ".spec.source"
Expand Down Expand Up @@ -176,3 +180,28 @@ func envFlags(flags *pflag.FlagSet, envs []corev1.EnvVar) {
"specify a key-value pair for an environment variable to set for the build container",
)
}

// imageLabelsFlags registers flags for output image labels.
func imageLabelsFlags(flags *pflag.FlagSet, labels map[string]string) {
var l []string
flags.StringArrayVarP(
&l,
OutputImageLabelsFlag,
"",
[]string{},
"specify a set of key-value pairs that correspond to labels to set on the output image",
)

}

// imageLabelsFlags registers flags for output image annotations.
func imageAnnotationsFlags(flags *pflag.FlagSet, annotations map[string]string) {
var a []string
flags.StringArrayVarP(
&a,
OutputImageAnnotationsFlag,
"",
[]string{},
"specify a set of key-value pairs that correspond to annotations to set on the output image",
)
}
14 changes: 14 additions & 0 deletions pkg/shp/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ func StringSliceToEnvVarSlice(envs []string) ([]corev1.EnvVar, error) {

return envVars, nil
}

func StringSliceToMap(kvPairs []string) (map[string]string, error) {
m := map[string]string{}

for _, l := range kvPairs {
parts := strings.SplitN(l, "=", 2)
if len(parts) == 1 {
return nil, fmt.Errorf("failed to parse key-value pair %q, not enough parts", l)
}
m[parts[0]] = parts[1]
}

return m, nil
}
77 changes: 77 additions & 0 deletions pkg/shp/util/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,80 @@ func TestStringSliceToEnvVar_ErrorCases(t *testing.T) {
})
}
}

func TestStringSliceToMap(t *testing.T) {
type args struct {
keyVal []string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "no envs",
args: args{
keyVal: []string{},
},
want: map[string]string{},
}, {
name: "value containing '='",
args: args{
keyVal: []string{"my-label=foo=bar"},
},
want: map[string]string{
"my-label": "foo=bar",
},
}, {
name: "one env",
args: args{
keyVal: []string{"my-name=my-value"},
},
want: map[string]string{
"my-name": "my-value",
},
},
{
name: "multiple envs",
args: args{
keyVal: []string{"name-one=value-one", "name-two=value-two", "name-three=value-three"},
},
want: map[string]string{
"name-one": "value-one",
"name-two": "value-two",
"name-three": "value-three",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, _ := StringSliceToMap(tt.args.keyVal); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringSliceToMap() = %#v, want %#v", got, tt.want)
}
})
}
}

func TestStringSliceToMap_ErrorCases(t *testing.T) {
type args struct {
keyVal []string
}
tests := []struct {
name string
args args
}{
{
name: "value part missing",
args: args{
keyVal: []string{"abc"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := StringSliceToMap(tt.args.keyVal); err == nil {
t.Error("StringSliceToMap() = want error but got nil")
}
})
}
}
35 changes: 35 additions & 0 deletions test/e2e/output-image-labels-annotations.bats
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"
}

0 comments on commit 63693a2

Please sign in to comment.