Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for img labels and anno. in build create cmd #68

Merged
merged 1 commit into from
Nov 2, 2021
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
4 changes: 4 additions & 0 deletions pkg/shp/flags/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
},
Output: buildv1alpha1.Image{
Credentials: &corev1.LocalObjectReference{},
Labels: map[string]string{},
Annotations: map[string]string{},
},
Timeout: &metav1.Duration{},
}
Expand All @@ -39,6 +41,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
2 changes: 2 additions & 0 deletions pkg/shp/flags/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestBuildSpecFromFlags(t *testing.T) {
Output: buildv1alpha1.Image{
Credentials: &credentials,
Image: "output-image",
Labels: map[string]string{},
Annotations: map[string]string{},
},
Timeout: &metav1.Duration{
Duration: 1 * time.Second,
Expand Down
25 changes: 25 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 @@ -174,3 +178,24 @@ 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) {
flags.VarP(
NewMapValue(labels),
OutputImageLabelsFlag,
"",
"specify a set of key-value pairs that correspond to labels to set on the output image",
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line.

}

// imageLabelsFlags registers flags for output image annotations.
func imageAnnotationsFlags(flags *pflag.FlagSet, annotations map[string]string) {
flags.VarP(
NewMapValue(annotations),
OutputImageAnnotationsFlag,
"",
"specify a set of key-value pairs that correspond to annotations to set on the output image",
)
}
43 changes: 43 additions & 0 deletions pkg/shp/flags/map_value.go
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename c to _, since it's not in use.

return "stringArray"
}

// NewMapValue instantiate a MapValue sharing the map.
func NewMapValue(m map[string]string) *MapValue {
return &MapValue{kvMap: m}
}
44 changes: 44 additions & 0 deletions pkg/shp/flags/map_value_test.go
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename this variable to m it follows the original type.


// 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]"))
}
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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thank you!

# 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"
}