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-labels
- output-image-annotations

Signed-off-by: Shivam Mukhade <[email protected]>
  • Loading branch information
Shivam Mukhade committed Oct 29, 2021
1 parent 3808c3f commit 7250bae
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/shp/cmd/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea
}
b.Spec.Env = append(b.Spec.Env, util.StringSliceToEnvVarSlice(envs)...)

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

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

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-labels"
// OutputImageAnnotationsFlag command-line flag.
OutputImageAnnotationsFlag = "output-image-annotations"
)

// 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 key-value pair for labels to set for 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 key-value pair for annotation to set for the output image",
)
}
10 changes: 10 additions & 0 deletions pkg/shp/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ func StringSliceToEnvVarSlice(envs []string) []corev1.EnvVar {
}
return envVars
}

func StringSliceToMap(kv []string) map[string]string {
m := map[string]string{}

for _, e := range kv {
d := strings.Split(e, "=")
m[d[0]] = d[1]
}
return m
}
46 changes: 46 additions & 0 deletions pkg/shp/util/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,49 @@ func TestStringSliceToEnvVar(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: "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)
}
})
}
}

0 comments on commit 7250bae

Please sign in to comment.