From 6fc0460bbde92795f562b05a1c747397a4f45f17 Mon Sep 17 00:00:00 2001 From: Shivam Mukhade Date: Sun, 21 Nov 2021 15:50:20 +0530 Subject: [PATCH] Adds support for img labels and anno. in buildrun 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 --- pkg/shp/flags/buildrun.go | 4 +++ pkg/shp/flags/buildrun_test.go | 2 ++ pkg/shp/flags/flags.go | 1 - pkg/shp/flags/map_value_test.go | 12 ++++---- test/e2e/output-image-labels-annotations.bats | 29 +++++++++++++++++++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pkg/shp/flags/buildrun.go b/pkg/shp/flags/buildrun.go index ab0d3de72..d873a6593 100644 --- a/pkg/shp/flags/buildrun.go +++ b/pkg/shp/flags/buildrun.go @@ -19,6 +19,8 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildRunSpec { Timeout: &metav1.Duration{}, Output: &buildv1alpha1.Image{ Credentials: &corev1.LocalObjectReference{}, + Labels: map[string]string{}, + Annotations: map[string]string{}, }, Env: []corev1.EnvVar{}, } @@ -28,6 +30,8 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildRunSpec { timeoutFlags(flags, spec.Timeout) imageFlags(flags, "output", spec.Output) envFlags(flags, &spec.Env) + imageLabelsFlags(flags, spec.Output.Labels) + imageAnnotationsFlags(flags, spec.Output.Annotations) return spec } diff --git a/pkg/shp/flags/buildrun_test.go b/pkg/shp/flags/buildrun_test.go index 9c08cdba6..716e3ede0 100644 --- a/pkg/shp/flags/buildrun_test.go +++ b/pkg/shp/flags/buildrun_test.go @@ -30,6 +30,8 @@ func TestBuildRunSpecFromFlags(t *testing.T) { Output: &buildv1alpha1.Image{ Credentials: &corev1.LocalObjectReference{Name: "name"}, Image: str, + Labels: map[string]string{}, + Annotations: map[string]string{}, }, } diff --git a/pkg/shp/flags/flags.go b/pkg/shp/flags/flags.go index 6749f22e8..1388a2a1e 100644 --- a/pkg/shp/flags/flags.go +++ b/pkg/shp/flags/flags.go @@ -187,7 +187,6 @@ func imageLabelsFlags(flags *pflag.FlagSet, labels map[string]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. diff --git a/pkg/shp/flags/map_value_test.go b/pkg/shp/flags/map_value_test.go index dbfce4e46..c40905765 100644 --- a/pkg/shp/flags/map_value_test.go +++ b/pkg/shp/flags/map_value_test.go @@ -14,31 +14,31 @@ func TestNewMapValue(t *testing.T) { spec := &buildv1alpha1.BuildSpec{Output: buildv1alpha1.Image{ Labels: map[string]string{}, }} - c := NewMapValue(spec.Output.Labels) + m := NewMapValue(spec.Output.Labels) // expect error when key-value is not split by equal sign - err := c.Set("a") + err := m.Set("a") g.Expect(err).NotTo(o.BeNil()) // setting a simple key-value entry - err = c.Set("a=b") + err = m.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") + err = m.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") + err = m.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() + s := m.String() g.Expect(s).To(o.Equal("[a=b,\"b=c,d,e=f\",c=d e]")) } diff --git a/test/e2e/output-image-labels-annotations.bats b/test/e2e/output-image-labels-annotations.bats index 23b74e7cb..1e1c5b31c 100644 --- a/test/e2e/output-image-labels-annotations.bats +++ b/test/e2e/output-image-labels-annotations.bats @@ -32,4 +32,33 @@ teardown() { # ensure that the label and annotation were inserted into the Build object assert_output --partial "foo: bar" assert_output --partial "created-by: shipwright" + + # create a BuildRun with two environment variables + run shp buildrun create ${buildrun_name} --buildref-name=${build_name} --output-image=my-image --output-image-label=foo=bar123 --output-image-annotation=owned-by=shipwright + assert_success + + # ensure that the build was successfully created + assert_output --partial "BuildRun created \"${buildrun_name}\" for Build \"${build_name}\"" + + # get the yaml for the BuildRun object + run kubectl get buildruns.shipwright.io/${buildrun_name} -o yaml + assert_success + + # ensure that the label and annotation were inserted into the BuildRun object + assert_output --partial "foo: bar123" + assert_output --partial "owned-by: shipwright" + + # get the taskrun that we created + run kubectl get taskruns.tekton.dev --selector=buildrun.shipwright.io/name=${buildrun_name} -o name + assert_success + + run kubectl get ${output} -o yaml + assert_success + + # ensure that the annotation was inserted into the TaskRun from the Build object which is not in BuildRun + assert_output --partial "created-by=shipwright" + # ensure that the labels and annotations where inserted into the TaskRun from the BuildRun Object + # and that the value from BuildRun override the ones defined in Build + assert_output --partial "owned-by=shipwright" + assert_output --partial "foo=bar123" } \ No newline at end of file