Skip to content

Commit 72431e2

Browse files
Camilohasbro17
authored andcommitted
test/*,pkg/generator,commands,user-guide: Add SA for the operator (#454)
This avoids running the operator using the 'default' service account. Fixes #189
1 parent f557c8d commit 72431e2

File tree

11 files changed

+76
-10
lines changed

11 files changed

+76
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ $ docker push quay.io/example/app-operator
7070
$ sed -i 's|REPLACE_IMAGE|quay.io/example/app-operator|g' deploy/operator.yaml
7171

7272
# Deploy the app-operator
73+
$ kubectl create -f deploy/sa.yaml
7374
$ kubectl create -f deploy/rbac.yaml
7475
$ kubectl create -f deploy/crd.yaml
7576
$ kubectl create -f deploy/operator.yaml
@@ -87,6 +88,7 @@ $ kubectl delete -f deploy/cr.yaml
8788
$ kubectl delete -f deploy/crd.yaml
8889
$ kubectl delete -f deploy/operator.yaml
8990
$ kubectl delete -f deploy/rbac.yaml
91+
$ kubectl delete -f deploy/sa.yaml
9092
```
9193

9294
## User Guide

commands/operator-sdk/cmd/test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ func NewTestCmd() *cobra.Command {
5555
}
5656

5757
func testFunc(cmd *cobra.Command, args []string) {
58-
// if no namespaced manifest path is given, combine deploy/rbac.yaml and deploy/operator.yaml
58+
// if no namespaced manifest path is given, combine deploy/sa.yaml, deploy/rbac.yaml and deploy/operator.yaml
5959
if namespacedManifestPath == "" {
6060
os.Mkdir("deploy/test", os.FileMode(int(0775)))
6161
namespacedManifestPath = "deploy/test/namespace-manifests.yaml"
62+
sa, err := ioutil.ReadFile("deploy/sa.yaml")
63+
if err != nil {
64+
log.Fatalf("could not find sa manifest: %v", err)
65+
}
6266
rbac, err := ioutil.ReadFile("deploy/rbac.yaml")
6367
if err != nil {
6468
log.Fatalf("could not find rbac manifest: %v", err)
@@ -67,7 +71,9 @@ func testFunc(cmd *cobra.Command, args []string) {
6771
if err != nil {
6872
log.Fatalf("could not find operator manifest: %v", err)
6973
}
70-
combined := append(rbac, []byte("\n---\n")...)
74+
combined := append(sa, []byte("\n---\n")...)
75+
combined = append(combined, rbac...)
76+
combined = append(combined, []byte("\n---\n")...)
7177
combined = append(combined, operator...)
7278
err = ioutil.WriteFile(namespacedManifestPath, combined, os.FileMode(int(0664)))
7379
if err != nil {

doc/user-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Kubernetes deployment manifests are generated in `deploy/operator.yaml`. The dep
140140
Deploy the memcached-operator:
141141

142142
```sh
143+
$ kubectl create -f deploy/sa.yaml
143144
$ kubectl create -f deploy/rbac.yaml
144145
$ kubectl create -f deploy/operator.yaml
145146
```

pkg/generator/generator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060
config = "config.yaml"
6161
rbacYaml = "rbac.yaml"
6262
crYaml = "cr.yaml"
63+
saYaml = "sa.yaml"
6364
catalogPackageYaml = "package.yaml"
6465
catalogCSVYaml = "csv.yaml"
6566
crdYaml = "crd.yaml"
@@ -76,6 +77,7 @@ const (
7677
operatorTmplName = "deploy/operator.yaml"
7778
rbacTmplName = "deploy/rbac.yaml"
7879
crTmplName = "deploy/cr.yaml"
80+
saTmplName = "deploy/sa.yaml"
7981
pluralSuffix = "s"
8082
)
8183

@@ -236,6 +238,13 @@ func renderDeployFiles(deployDir, projectName, apiVersion, kind string) error {
236238
return err
237239
}
238240

241+
saTd := tmplData{
242+
ProjectName: projectName,
243+
}
244+
if err := renderWriteFile(filepath.Join(deployDir, saYaml), saTmplName, saYamlTmpl, saTd); err != nil {
245+
return err
246+
}
247+
239248
opTd := tmplData{
240249
ProjectName: projectName,
241250
Image: "REPLACE_IMAGE",

pkg/generator/generator_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ spec:
199199
labels:
200200
name: app-operator
201201
spec:
202+
serviceAccountName: app-operator
202203
containers:
203204
- name: app-operator
204205
image: quay.io/example-inc/app-operator:0.0.1
@@ -255,16 +256,22 @@ rules:
255256
kind: RoleBinding
256257
apiVersion: rbac.authorization.k8s.io/v1beta1
257258
metadata:
258-
name: default-account-app-operator
259+
name: app-operator
259260
subjects:
260261
- kind: ServiceAccount
261-
name: default
262+
name: app-operator
262263
roleRef:
263264
kind: Role
264265
name: app-operator
265266
apiGroup: rbac.authorization.k8s.io
266267
`
267268

269+
const saYamlExp = `apiVersion: v1
270+
kind: ServiceAccount
271+
metadata:
272+
name: app-operator
273+
`
274+
268275
func TestGenDeploy(t *testing.T) {
269276
buf := &bytes.Buffer{}
270277
crdTd := tmplData{
@@ -309,6 +316,16 @@ func TestGenDeploy(t *testing.T) {
309316
diffs := dmp.DiffMain(rbacYamlExp, buf.String(), false)
310317
t.Errorf("\nTest failed. Below is the diff of the expected vs actual results.\nRed text is missing and green text is extra.\n\n" + dmp.DiffPrettyText(diffs))
311318
}
319+
320+
buf = &bytes.Buffer{}
321+
if err := renderFile(buf, saTmplName, saYamlTmpl, tmplData{ProjectName: appProjectName}); err != nil {
322+
t.Error(err)
323+
}
324+
if saYamlExp != buf.String() {
325+
dmp := diffmatchpatch.New()
326+
diffs := dmp.DiffMain(saYamlExp, buf.String(), false)
327+
t.Errorf("\nTest failed. Below is the diff of the expected vs actual results.\nRed text is missing and green text is extra.\n\n" + dmp.DiffPrettyText(diffs))
328+
}
312329
}
313330

314331
const registerExp = `package v1alpha1

pkg/generator/templates.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ spec:
438438
labels:
439439
name: {{.ProjectName}}
440440
spec:
441+
serviceAccountName: {{.ProjectName}}
441442
containers:
442443
- name: {{.ProjectName}}
443444
image: {{.Image}}
@@ -494,16 +495,22 @@ rules:
494495
kind: RoleBinding
495496
apiVersion: rbac.authorization.k8s.io/v1beta1
496497
metadata:
497-
name: default-account-{{.ProjectName}}
498+
name: {{.ProjectName}}
498499
subjects:
499500
- kind: ServiceAccount
500-
name: default
501+
name: {{.ProjectName}}
501502
roleRef:
502503
kind: Role
503504
name: {{.ProjectName}}
504505
apiGroup: rbac.authorization.k8s.io
505506
`
506507

508+
const saYamlTmpl = `apiVersion: v1
509+
kind: ServiceAccount
510+
metadata:
511+
name: {{.ProjectName}}
512+
`
513+
507514
const crYamlTmpl = `apiVersion: "{{.APIVersion}}"
508515
kind: "{{.Kind}}"
509516
metadata:

test/e2e/memcached_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ func MemcachedCluster(t *testing.T) {
246246
}
247247
}
248248

249+
// create sa
250+
saYAML, err := ioutil.ReadFile("deploy/sa.yaml")
251+
if err != nil {
252+
t.Fatal(err)
253+
}
254+
err = ctx.CreateFromYAML(saYAML)
255+
if err != nil {
256+
t.Fatal(err)
257+
}
258+
t.Log("Created sa")
259+
249260
// create rbac
250261
rbacYAML, err := ioutil.ReadFile("deploy/rbac.yaml")
251262
rbacYAMLSplit := bytes.Split(rbacYAML, []byte("\n---\n"))

test/test-framework/deploy/namespace-init.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: memcached-operator
5+
6+
---
7+
18
kind: Role
29
apiVersion: rbac.authorization.k8s.io/v1beta1
310
metadata:
@@ -36,10 +43,10 @@ rules:
3643
kind: RoleBinding
3744
apiVersion: rbac.authorization.k8s.io/v1beta1
3845
metadata:
39-
name: default-account-memcached-operator
46+
name: memcached-operator
4047
subjects:
4148
- kind: ServiceAccount
42-
name: default
49+
name: memcached-operator
4350
roleRef:
4451
kind: Role
4552
name: memcached-operator
@@ -61,6 +68,7 @@ spec:
6168
labels:
6269
name: memcached-operator
6370
spec:
71+
serviceAccountName: memcached-operator
6472
containers:
6573
- name: memcached-operator
6674
image: quay.io/coreos/operator-sdk-dev:test-framework-operator

test/test-framework/deploy/operator.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ spec:
1212
labels:
1313
name: memcached-operator
1414
spec:
15+
serviceAccountName: memcached-operator
1516
containers:
1617
- name: memcached-operator
1718
image: quay.io/coreos/operator-sdk-dev:test-framework-operator

test/test-framework/deploy/rbac.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ rules:
3636
kind: RoleBinding
3737
apiVersion: rbac.authorization.k8s.io/v1beta1
3838
metadata:
39-
name: default-account-memcached-operator
39+
name: memcached-operator
4040
subjects:
4141
- kind: ServiceAccount
42-
name: default
42+
name: memcached-operator
4343
roleRef:
4444
kind: Role
4545
name: memcached-operator

test/test-framework/deploy/sa.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
kind: ServiceAccount
2+
apiVersion: v1
3+
metadata:
4+
name: memcached-operator

0 commit comments

Comments
 (0)