Skip to content

Commit b78860d

Browse files
committed
new: add --node flag to buildrun
- test: add node-selector.bats for build and buildrun Signed-off-by: rxinui <[email protected]>
1 parent afc2b3a commit b78860d

10 files changed

+117
-6
lines changed

docs/shp_build_create.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ shp build create <name> [flags]
1919
```
2020
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
2121
-h, --help help for create
22+
--node stringArray set of key-value pairs that correspond to labels of a node to match (default [])
2223
--output-image string image employed during the building process
2324
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
2425
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])

docs/shp_build_run.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ shp build run <name> [flags]
2222
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
2323
-F, --follow Start a build and watch its log until it completes or fails.
2424
-h, --help help for run
25+
--node stringArray set of key-value pairs that correspond to labels of a node to match (default [])
2526
--output-image string image employed during the building process
2627
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
2728
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])

docs/shp_build_upload.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ shp build upload <build-name> [path/to/source|.] [flags]
3232
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
3333
-F, --follow Start a build and watch its log until it completes or fails.
3434
-h, --help help for upload
35+
--node stringArray set of key-value pairs that correspond to labels of a node to match (default [])
3536
--output-image string image employed during the building process
3637
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
3738
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])

docs/shp_buildrun_create.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ shp buildrun create <name> [flags]
2121
--buildref-name string name of build resource to reference
2222
-e, --env stringArray specify a key-value pair for an environment variable to set for the build container (default [])
2323
-h, --help help for create
24+
--node stringArray set of key-value pairs that correspond to labels of a node to match (default [])
2425
--output-image string image employed during the building process
2526
--output-image-annotation stringArray specify a set of key-value pairs that correspond to annotations to set on the output image (default [])
2627
--output-image-label stringArray specify a set of key-value pairs that correspond to labels to set on the output image (default [])

pkg/shp/flags/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) (*buildv1beta1.BuildSpec, *string,
4444
TTLAfterFailed: &metav1.Duration{},
4545
TTLAfterSucceeded: &metav1.Duration{},
4646
},
47-
NodeSelector: make(map[string]string),
47+
NodeSelector: map[string]string{},
4848
}
4949

5050
sourceFlags(flags, spec.Source)

pkg/shp/flags/build_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestBuildSpecFromFlags(t *testing.T) {
5555
Duration: 30 * time.Minute,
5656
},
5757
},
58+
NodeSelector: map[string]string{"kubernetes.io/hostname": "worker-1"},
5859
}
5960

6061
cmd := &cobra.Command{}
@@ -115,6 +116,13 @@ func TestBuildSpecFromFlags(t *testing.T) {
115116
g.Expect(expected.Output).To(o.Equal(spec.Output), "spec.output")
116117
})
117118

119+
t.Run(".spec.nodeSelector", func(_ *testing.T) {
120+
err := flags.Set(NodeSelectorFlag, "kubernetes.io/hostname=worker-1")
121+
g.Expect(err).To(o.BeNil())
122+
// g.Expect(expected.NodeSelector).To(o.HaveKeyWithValue("kubernetes.io/hostname",spec.NodeSelector["kubernetes.io/hostname"]), ".spec.nodeSelector")
123+
g.Expect(expected.NodeSelector).To(o.Equal(spec.NodeSelector), ".spec.nodeSelector")
124+
})
125+
118126
t.Run(".spec.timeout", func(_ *testing.T) {
119127
err := flags.Set(TimeoutFlag, expected.Timeout.Duration.String())
120128
g.Expect(err).To(o.BeNil())

pkg/shp/flags/buildrun.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1beta1.BuildRunSpec {
2828
TTLAfterFailed: &metav1.Duration{},
2929
TTLAfterSucceeded: &metav1.Duration{},
3030
},
31+
NodeSelector: map[string]string{},
3132
}
3233

3334
buildRefFlags(flags, &spec.Build)
@@ -39,7 +40,7 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1beta1.BuildRunSpec {
3940
imageLabelsFlags(flags, spec.Output.Labels)
4041
imageAnnotationsFlags(flags, spec.Output.Annotations)
4142
buildRunRetentionFlags(flags, spec.Retention)
42-
43+
buildNodeFlags(flags, spec.NodeSelector)
4344
return spec
4445
}
4546

pkg/shp/flags/buildrun_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestBuildRunSpecFromFlags(t *testing.T) {
3838
Duration: 30 * time.Minute,
3939
},
4040
},
41+
NodeSelector: map[string]string{"kubernetes.io/hostname": "worker-1"},
4142
}
4243

4344
cmd := &cobra.Command{}
@@ -75,6 +76,13 @@ func TestBuildRunSpecFromFlags(t *testing.T) {
7576
g.Expect(*expected.Output).To(o.Equal(*spec.Output), "spec.output")
7677
})
7778

79+
t.Run(".spec.nodeSelector", func(_ *testing.T) {
80+
err := flags.Set(NodeSelectorFlag, "kubernetes.io/hostname=worker-1")
81+
g.Expect(err).To(o.BeNil())
82+
// g.Expect(expected.NodeSelector).To(o.HaveKeyWithValue("kubernetes.io/hostname",spec.NodeSelector["kubernetes.io/hostname"]), ".spec.nodeSelector")
83+
g.Expect(expected.NodeSelector).To(o.Equal(spec.NodeSelector), ".spec.nodeSelector")
84+
})
85+
7886
t.Run(".spec.retention.ttlAfterFailed", func(_ *testing.T) {
7987
err := flags.Set(RetentionTTLAfterFailedFlag, expected.Retention.TTLAfterFailed.Duration.String())
8088
g.Expect(err).To(o.BeNil())

pkg/shp/flags/flags.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ const (
6464
RetentionTTLAfterFailedFlag = "retention-ttl-after-failed"
6565
// RetentionTTLAfterSucceededFlag command-line flag.
6666
RetentionTTLAfterSucceededFlag = "retention-ttl-after-succeeded"
67-
// NodeFlag command-line flag.
68-
NodeFlag = "node"
67+
// NodeSelectorFlag command-line flag.
68+
NodeSelectorFlag = "node"
6969
)
7070

7171
// sourceFlags flags for ".spec.source"
@@ -261,9 +261,9 @@ func serviceAccountFlags(flags *pflag.FlagSet, sa *string) {
261261

262262
}
263263

264-
// ... registers flags for adding node selector labels
264+
// buildNodeFlags registers flags for adding BuildSpec.NodeSelector
265265
func buildNodeFlags(flags *pflag.FlagSet, nodeSelectorLabels map[string]string) {
266-
flags.Var(NewMapValue(nodeSelectorLabels), NodeFlag, "set of key-value pairs corresponds of node's labels to match")
266+
flags.Var(NewMapValue(nodeSelectorLabels), NodeSelectorFlag, "set of key-value pairs that correspond to labels of a node to match")
267267
}
268268

269269
// envFlags registers flags for adding corev1.EnvVars.

test/e2e/node-selector.bats

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env bats
2+
3+
source test/e2e/helpers.sh
4+
5+
setup() {
6+
load 'bats/support/load'
7+
load 'bats/assert/load'
8+
load 'bats/file/load'
9+
}
10+
11+
teardown() {
12+
run kubectl delete builds.shipwright.io --all
13+
run kubectl delete buildruns.shipwright.io --all
14+
}
15+
16+
@test "shp build node selector single label" {
17+
# generate random names for our build
18+
build_name=$(random_name)
19+
20+
# create a Build with node selector
21+
run shp build create ${build_name} --source-url=https://github.com/shipwright-io/sample-go --output-image=my-fake-image --node="kubernetes.io/hostname=node-1"
22+
assert_success
23+
24+
# ensure that the build was successfully created
25+
assert_output --partial "Created build \"${build_name}\""
26+
27+
# get the jsonpath of Build object .spec.nodeSelector
28+
run kubectl get builds.shipwright.io/${build_name} -ojsonpath="{.spec.nodeSelector}"
29+
assert_success
30+
31+
assert_output '{"kubernetes.io/hostname":"node-1"}'
32+
}
33+
34+
@test "shp build node selector multiple labels" {
35+
# generate random names for our build
36+
build_name=$(random_name)
37+
38+
# create a Build with node selector
39+
run shp build create ${build_name} --source-url=https://github.com/shipwright-io/sample-go --output-image=my-fake-image --node="kubernetes.io/hostname=node-1" --node="kubernetes.io/os=linux"
40+
assert_success
41+
42+
# ensure that the build was successfully created
43+
assert_output --partial "Created build \"${build_name}\""
44+
45+
# get the jsonpath of Build object .spec.nodeSelector
46+
run kubectl get builds.shipwright.io/${build_name} -ojsonpath="{.spec.nodeSelector}"
47+
assert_success
48+
49+
assert_output --partial '"kubernetes.io/hostname":"node-1"'
50+
assert_output --partial '"kubernetes.io/os":"linux"'
51+
}
52+
53+
@test "shp buildrun node selector single label" {
54+
# generate random names for our buildrun
55+
buildrun_name=$(random_name)
56+
build_name=$(random_name)
57+
58+
# create a Build with node selector
59+
run shp buildrun create ${buildrun_name} --buildref-name=${build_name} --node="kubernetes.io/hostname=node-1"
60+
assert_success
61+
62+
# ensure that the build was successfully created
63+
assert_output --partial "BuildRun created \"${buildrun_name}\" for Build \"${build_name}\""
64+
65+
# get the jsonpath of Build object .spec.nodeSelector
66+
run kubectl get buildruns.shipwright.io/${buildrun_name} -ojsonpath="{.spec.nodeSelector}"
67+
assert_success
68+
69+
assert_output '{"kubernetes.io/hostname":"node-1"}'
70+
}
71+
72+
@test "shp buildrun node selector multiple labels" {
73+
# generate random names for our buildrun
74+
buildrun_name=$(random_name)
75+
build_name=$(random_name)
76+
77+
# create a Build with node selector
78+
run shp buildrun create ${buildrun_name} --buildref-name=${build_name} --node="kubernetes.io/hostname=node-1" --node="kubernetes.io/os=linux"
79+
assert_success
80+
81+
# ensure that the build was successfully created
82+
assert_output --partial "BuildRun created \"${buildrun_name}\" for Build \"${build_name}\""
83+
84+
# get the jsonpath of Build object .spec.nodeSelector
85+
run kubectl get buildruns.shipwright.io/${buildrun_name} -ojsonpath="{.spec.nodeSelector}"
86+
assert_success
87+
88+
assert_output --partial '"kubernetes.io/hostname":"node-1"'
89+
assert_output --partial '"kubernetes.io/os":"linux"'
90+
}

0 commit comments

Comments
 (0)