Skip to content

Commit 4831482

Browse files
Merge pull request #27 from coreydaley/jira_ship_0004_build_env_vars
SHIP-0004: Build Environment Variables
2 parents 44c0a85 + 0fef193 commit 4831482

File tree

308 files changed

+18206
-17770
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

308 files changed

+18206
-17770
lines changed

.github/workflows/e2e.yaml

+2-5
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ jobs:
2424

2525
- name: Check out code into the Go module directory
2626
uses: actions/checkout@v2
27+
with:
28+
submodules: recursive
2729

2830
- name: Install Tools
2931
run: sudo apt-get update && sudo apt-get install -y make gcc
3032

31-
- name: Setup BATS
32-
uses: mig4/setup-bats@v1
33-
with:
34-
bats-version: 1.2.1
35-
3633
- name: Install kubectl
3734
uses: azure/setup-kubectl@v1
3835
with:

.gitmodules

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[submodule "test/e2e/bats/core"]
2+
path = test/e2e/bats/core
3+
url = https://github.com/bats-core/bats-core
4+
[submodule "test/e2e/bats/support"]
5+
path = test/e2e/bats/support
6+
url = https://github.com/bats-core/bats-support
7+
[submodule "test/e2e/bats/assert"]
8+
path = test/e2e/bats/assert
9+
url = https://github.com/bats-core/bats-assert
10+
[submodule "test/e2e/bats/file"]
11+
path = test/e2e/bats/file
12+
url = https://github.com/bats-core/bats-file

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ test: test-unit test-e2e
4949
test-unit:
5050
go test $(GO_FLAGS) $(GO_TEST_FLAGS) $(CMD) $(PKG) $(ARGS)
5151

52-
# runs all end-to-end tests using bats, it assumes bats is installed
52+
# looks for *.bats files in the test/e2e directory and runs them
5353
test-e2e:
54-
./test/e2e/e2e.bats
54+
./test/e2e/bats/core/bin/bats --recursive test/e2e/*.bats
5555

5656
# wait for KinD cluster to be on ready state, so tests can be performed
5757
verify-kind:

docs/testing.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,22 @@ The install script waits for the Controller instance to be running.
5454
[BATS][batsCore] is a testing framework for Bash. It's structured as a regular script with enhanced
5555
syntax to define test cases, collect results, and more.
5656

57-
To run BATS based tests, make sure you have `bats` installed, and then execute:
57+
The BATS core tool and it's supporting libraries are linked in this project as `git submodules`,
58+
alleviating the need to install BATS yourself.
59+
60+
To run BATS based tests, make sure that you have cloned the project (including all submodules):
61+
62+
```sh
63+
# Option 1: Clone the repository and populate submodules right away
64+
git clone --recurse-submodules --depth 1 https://github.com/shipwright-io/cli.git
65+
66+
# Option 2: Populate submodules after cloning
67+
cd /your/project/directory/cli
68+
git submodule init
69+
git submodule update
70+
```
71+
72+
then run:
5873

5974
```sh
6075
make test-e2e

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ go 1.15
55
require (
66
github.com/mailru/easyjson v0.7.1 // indirect
77
github.com/onsi/gomega v1.10.3
8-
github.com/shipwright-io/build v0.5.2-0.20210715083206-5d8fb411a1eb
9-
github.com/spf13/cobra v1.1.3
8+
github.com/shipwright-io/build v0.6.0
9+
github.com/spf13/cobra v1.2.1
1010
github.com/spf13/pflag v1.0.5
1111
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c
12-
k8s.io/api v0.20.2
13-
k8s.io/apimachinery v0.20.2
12+
k8s.io/api v0.20.6
13+
k8s.io/apimachinery v0.20.6
1414
k8s.io/cli-runtime v0.20.2
15-
k8s.io/client-go v0.20.2
15+
k8s.io/client-go v0.20.6
1616
k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f
1717
)
1818

go.sum

+415
Large diffs are not rendered by default.

pkg/shp/cmd/build/create.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"fmt"
55

66
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
7-
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
8-
"github.com/shipwright-io/cli/pkg/shp/flags"
9-
"github.com/shipwright-io/cli/pkg/shp/params"
107
"github.com/spf13/cobra"
118

129
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1310
"k8s.io/cli-runtime/pkg/genericclioptions"
11+
12+
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
13+
"github.com/shipwright-io/cli/pkg/shp/flags"
14+
"github.com/shipwright-io/cli/pkg/shp/params"
15+
"github.com/shipwright-io/cli/pkg/shp/util"
1416
)
1517

1618
// CreateCommand contains data input from user
@@ -59,6 +61,13 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea
5961
},
6062
Spec: *c.buildSpec,
6163
}
64+
65+
envs, err := c.cmd.Flags().GetStringArray("env")
66+
if err != nil {
67+
return err
68+
}
69+
b.Spec.Env = append(b.Spec.Env, util.StringSliceToEnvVarSlice(envs)...)
70+
6271
flags.SanitizeBuildSpec(&b.Spec)
6372

6473
clientset, err := params.ShipwrightClientSet()

pkg/shp/cmd/buildrun/create.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"fmt"
55

66
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
7-
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
8-
"github.com/shipwright-io/cli/pkg/shp/flags"
9-
"github.com/shipwright-io/cli/pkg/shp/params"
107
"github.com/spf13/cobra"
118

129
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1310
"k8s.io/cli-runtime/pkg/genericclioptions"
11+
12+
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
13+
"github.com/shipwright-io/cli/pkg/shp/flags"
14+
"github.com/shipwright-io/cli/pkg/shp/params"
15+
"github.com/shipwright-io/cli/pkg/shp/util"
1416
)
1517

1618
// CreateCommand reprents the build's create subcommand.
@@ -60,6 +62,13 @@ func (c *CreateCommand) Run(params *params.Params, ioStreams *genericclioptions.
6062
},
6163
Spec: *c.buildRunSpec,
6264
}
65+
66+
envs, err := c.cmd.Flags().GetStringArray("env")
67+
if err != nil {
68+
return err
69+
}
70+
br.Spec.Env = append(br.Spec.Env, util.StringSliceToEnvVarSlice(envs)...)
71+
6372
flags.SanitizeBuildRunSpec(&br.Spec)
6473

6574
clientset, err := params.ShipwrightClientSet()

pkg/shp/flags/build.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package flags
33
import (
44
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
55
"github.com/spf13/pflag"
6+
67
corev1 "k8s.io/api/core/v1"
78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
"k8s.io/utils/pointer"
@@ -37,6 +38,7 @@ func BuildSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildSpec {
3738
imageFlags(flags, "builder", spec.Builder)
3839
imageFlags(flags, "output", &spec.Output)
3940
timeoutFlags(flags, spec.Timeout)
41+
envFlags(flags, spec.Env)
4042

4143
return spec
4244
}
@@ -56,6 +58,9 @@ func SanitizeBuildSpec(b *buildv1alpha1.BuildSpec) {
5658
if b.Builder.Image == "" && b.Builder.Credentials == nil {
5759
b.Builder = nil
5860
}
61+
if len(b.Env) == 0 {
62+
b.Env = nil
63+
}
5964
}
6065
if b.Timeout != nil && b.Timeout.Duration == 0 {
6166
b.Timeout = nil

pkg/shp/flags/buildrun.go

+6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ func BuildRunSpecFromFlags(flags *pflag.FlagSet) *buildv1alpha1.BuildRunSpec {
2020
Output: &buildv1alpha1.Image{
2121
Credentials: &corev1.LocalObjectReference{},
2222
},
23+
Env: []corev1.EnvVar{},
2324
}
2425

2526
buildRefFlags(flags, spec.BuildRef)
2627
serviceAccountFlags(flags, spec.ServiceAccount)
2728
timeoutFlags(flags, spec.Timeout)
2829
imageFlags(flags, "output", spec.Output)
30+
envFlags(flags, spec.Env)
2931

3032
return spec
3133
}
@@ -52,4 +54,8 @@ func SanitizeBuildRunSpec(br *buildv1alpha1.BuildRunSpec) {
5254
if br.Timeout != nil && br.Timeout.Duration == 0 {
5355
br.Timeout = nil
5456
}
57+
58+
if len(br.Env) == 0 {
59+
br.Env = nil
60+
}
5561
}

pkg/shp/flags/flags.go

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
88
"github.com/spf13/pflag"
9+
10+
corev1 "k8s.io/api/core/v1"
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012
)
1113

@@ -18,6 +20,8 @@ const (
1820
BuilderCredentialsSecretFlag = "builder-credentials-secret"
1921
// DockerfileFlag command-line flag.
2022
DockerfileFlag = "dockerfile"
23+
// EnvFlag command-line flag.
24+
EnvFlag = "env"
2125
// SourceURLFlag command-line flag.
2226
SourceURLFlag = "source-url"
2327
// SourceRevisionFlag command-line flag.
@@ -160,3 +164,15 @@ func serviceAccountFlags(flags *pflag.FlagSet, sa *buildv1alpha1.ServiceAccount)
160164
"generate a Kubernetes service-account for the build",
161165
)
162166
}
167+
168+
// envFlags registers flags for adding corev1.EnvVars.
169+
func envFlags(flags *pflag.FlagSet, envs []corev1.EnvVar) {
170+
var e []string
171+
flags.StringArrayVarP(
172+
&e,
173+
"env",
174+
"e",
175+
[]string{},
176+
"specify a key-value pair for an environment variable to set for the build container",
177+
)
178+
}

pkg/shp/util/env.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package util
2+
3+
import (
4+
"strings"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
)
8+
9+
func StringSliceToEnvVarSlice(envs []string) []corev1.EnvVar {
10+
envVars := []corev1.EnvVar{}
11+
12+
for _, e := range envs {
13+
d := strings.Split(e, "=")
14+
envVars = append(envVars, corev1.EnvVar{Name: d[0], Value: d[1]})
15+
}
16+
return envVars
17+
}

pkg/shp/util/env_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package util
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
corev1 "k8s.io/api/core/v1"
8+
)
9+
10+
func TestStringSliceToEnvVar(t *testing.T) {
11+
type args struct {
12+
envs []string
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want []corev1.EnvVar
18+
}{
19+
{
20+
name: "no envs",
21+
args: args{
22+
envs: []string{},
23+
},
24+
want: []corev1.EnvVar{},
25+
},
26+
{
27+
name: "one env",
28+
args: args{
29+
envs: []string{"my-name=my-value"},
30+
},
31+
want: []corev1.EnvVar{
32+
{Name: "my-name", Value: "my-value"},
33+
},
34+
},
35+
{
36+
name: "multiple envs",
37+
args: args{
38+
envs: []string{"name-one=value-one", "name-two=value-two", "name-three=value-three"},
39+
},
40+
want: []corev1.EnvVar{
41+
{Name: "name-one", Value: "value-one"},
42+
{Name: "name-two", Value: "value-two"},
43+
{Name: "name-three", Value: "value-three"},
44+
},
45+
},
46+
}
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
if got := StringSliceToEnvVarSlice(tt.args.envs); !reflect.DeepEqual(got, tt.want) {
50+
t.Errorf("StringSliceToEnvVar() = %#v, want %#v", got, tt.want)
51+
}
52+
})
53+
}
54+
}

test/e2e/bats/assert

Submodule assert added at 672ad18

test/e2e/bats/core

Submodule core added at 90a051b

test/e2e/bats/file

Submodule file added at 17fa557

test/e2e/bats/support

Submodule support added at d140a65

test/e2e/e2e.bats

100755100644
+23-16
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@
22

33
source test/e2e/helpers.sh
44

5-
@test "shp binary can be executed" {
6-
result="$(shp --help)"
7-
[ ! -z "$result" ]
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
814
}
915

10-
@test "shp build create succesfully run with 1 argument" {
11-
build_name=$(random_name)
12-
result="$(shp build create ${build_name} --source-url=url --output-image=image)"
13-
[ ! -z "$result" ]
14-
result=$(kubectl get build.shipwright.io ${build_name})
15-
[ ! -z "$result" ]
16+
@test "shp binary can be executed" {
17+
run shp --help
18+
assert_success
1619
}
1720

18-
@test "shp buildrun create succesfully run with 1 argument" {
19-
build_name=$(random_name)
20-
result="$(shp build create ${build_name} --source-url=url --output-image=image)"
21-
[ ! -z "$result" ]
21+
@test "shp [build/buildrun] create should not error when a name is specified" {
22+
# generate random names for our build and buildrun
23+
build_name=$(random_name)
2224
buildrun_name=$(random_name)
23-
result="$(shp buildrun create ${buildrun_name} --buildref-name={$build_name})"
24-
result=$(kubectl get buildrun.shipwright.io ${buildrun_name})
25-
[ ! -z "$result" ]
25+
26+
# ensure that shp build create does not give an error when a build_name is specified
27+
run shp build create ${build_name} --source-url=url --output-image=image
28+
assert_success
29+
30+
# ensure that shp buildrun create does not give an error when a buildrun_name is specified
31+
run shp buildrun create ${buildrun_name} --buildref-name=${build_name}
32+
assert_success
2633
}

0 commit comments

Comments
 (0)