Skip to content

Commit

Permalink
test/*,pkg/generator,commands,user-guide: Add SA for the operator (op…
Browse files Browse the repository at this point in the history
…erator-framework#454)

This avoids running the operator using the 'default' service account.

Fixes operator-framework#189
  • Loading branch information
Camilo authored and dymurray committed Sep 26, 2018
1 parent 139bffc commit ef00bed
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ $ docker push quay.io/example/app-operator
$ sed -i 's|REPLACE_IMAGE|quay.io/example/app-operator|g' deploy/operator.yaml

# Deploy the app-operator
$ kubectl create -f deploy/sa.yaml
$ kubectl create -f deploy/rbac.yaml
$ kubectl create -f deploy/crd.yaml
$ kubectl create -f deploy/operator.yaml
Expand All @@ -87,6 +88,7 @@ $ kubectl delete -f deploy/cr.yaml
$ kubectl delete -f deploy/crd.yaml
$ kubectl delete -f deploy/operator.yaml
$ kubectl delete -f deploy/rbac.yaml
$ kubectl delete -f deploy/sa.yaml
```

## User Guide
Expand Down
10 changes: 8 additions & 2 deletions commands/operator-sdk/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ func NewTestCmd() *cobra.Command {
}

func testFunc(cmd *cobra.Command, args []string) {
// if no namespaced manifest path is given, combine deploy/rbac.yaml and deploy/operator.yaml
// if no namespaced manifest path is given, combine deploy/sa.yaml, deploy/rbac.yaml and deploy/operator.yaml
if namespacedManifestPath == "" {
os.Mkdir("deploy/test", os.FileMode(int(0775)))
namespacedManifestPath = "deploy/test/namespace-manifests.yaml"
sa, err := ioutil.ReadFile("deploy/sa.yaml")
if err != nil {
log.Fatalf("could not find sa manifest: %v", err)
}
rbac, err := ioutil.ReadFile("deploy/rbac.yaml")
if err != nil {
log.Fatalf("could not find rbac manifest: %v", err)
Expand All @@ -67,7 +71,9 @@ func testFunc(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatalf("could not find operator manifest: %v", err)
}
combined := append(rbac, []byte("\n---\n")...)
combined := append(sa, []byte("\n---\n")...)
combined = append(combined, rbac...)
combined = append(combined, []byte("\n---\n")...)
combined = append(combined, operator...)
err = ioutil.WriteFile(namespacedManifestPath, combined, os.FileMode(int(0664)))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions doc/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Kubernetes deployment manifests are generated in `deploy/operator.yaml`. The dep
Deploy the memcached-operator:

```sh
$ kubectl create -f deploy/sa.yaml
$ kubectl create -f deploy/rbac.yaml
$ kubectl create -f deploy/operator.yaml
```
Expand Down
9 changes: 9 additions & 0 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
config = "config.yaml"
rbacYaml = "rbac.yaml"
crYaml = "cr.yaml"
saYaml = "sa.yaml"
catalogPackageYaml = "package.yaml"
catalogCSVYaml = "csv.yaml"
crdYaml = "crd.yaml"
Expand All @@ -76,6 +77,7 @@ const (
operatorTmplName = "deploy/operator.yaml"
rbacTmplName = "deploy/rbac.yaml"
crTmplName = "deploy/cr.yaml"
saTmplName = "deploy/sa.yaml"
pluralSuffix = "s"
)

Expand Down Expand Up @@ -236,6 +238,13 @@ func renderDeployFiles(deployDir, projectName, apiVersion, kind string) error {
return err
}

saTd := tmplData{
ProjectName: projectName,
}
if err := renderWriteFile(filepath.Join(deployDir, saYaml), saTmplName, saYamlTmpl, saTd); err != nil {
return err
}

opTd := tmplData{
ProjectName: projectName,
Image: "REPLACE_IMAGE",
Expand Down
21 changes: 19 additions & 2 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ spec:
labels:
name: app-operator
spec:
serviceAccountName: app-operator
containers:
- name: app-operator
image: quay.io/example-inc/app-operator:0.0.1
Expand Down Expand Up @@ -255,16 +256,22 @@ rules:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: default-account-app-operator
name: app-operator
subjects:
- kind: ServiceAccount
name: default
name: app-operator
roleRef:
kind: Role
name: app-operator
apiGroup: rbac.authorization.k8s.io
`

const saYamlExp = `apiVersion: v1
kind: ServiceAccount
metadata:
name: app-operator
`

func TestGenDeploy(t *testing.T) {
buf := &bytes.Buffer{}
crdTd := tmplData{
Expand Down Expand Up @@ -309,6 +316,16 @@ func TestGenDeploy(t *testing.T) {
diffs := dmp.DiffMain(rbacYamlExp, buf.String(), false)
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))
}

buf = &bytes.Buffer{}
if err := renderFile(buf, saTmplName, saYamlTmpl, tmplData{ProjectName: appProjectName}); err != nil {
t.Error(err)
}
if saYamlExp != buf.String() {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(saYamlExp, buf.String(), false)
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))
}
}

const registerExp = `package v1alpha1
Expand Down
11 changes: 9 additions & 2 deletions pkg/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ spec:
labels:
name: {{.ProjectName}}
spec:
serviceAccountName: {{.ProjectName}}
containers:
- name: {{.ProjectName}}
image: {{.Image}}
Expand Down Expand Up @@ -494,16 +495,22 @@ rules:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: default-account-{{.ProjectName}}
name: {{.ProjectName}}
subjects:
- kind: ServiceAccount
name: default
name: {{.ProjectName}}
roleRef:
kind: Role
name: {{.ProjectName}}
apiGroup: rbac.authorization.k8s.io
`

const saYamlTmpl = `apiVersion: v1
kind: ServiceAccount
metadata:
name: {{.ProjectName}}
`

const crYamlTmpl = `apiVersion: "{{.APIVersion}}"
kind: "{{.Kind}}"
metadata:
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ func MemcachedCluster(t *testing.T) {
}
}

// create sa
saYAML, err := ioutil.ReadFile("deploy/sa.yaml")
if err != nil {
t.Fatal(err)
}
err = ctx.CreateFromYAML(saYAML)
if err != nil {
t.Fatal(err)
}
t.Log("Created sa")

// create rbac
rbacYAML, err := ioutil.ReadFile("deploy/rbac.yaml")
rbacYAMLSplit := bytes.Split(rbacYAML, []byte("\n---\n"))
Expand Down
12 changes: 10 additions & 2 deletions test/test-framework/deploy/namespace-init.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: memcached-operator

---

kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
Expand Down Expand Up @@ -36,10 +43,10 @@ rules:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: default-account-memcached-operator
name: memcached-operator
subjects:
- kind: ServiceAccount
name: default
name: memcached-operator
roleRef:
kind: Role
name: memcached-operator
Expand All @@ -61,6 +68,7 @@ spec:
labels:
name: memcached-operator
spec:
serviceAccountName: memcached-operator
containers:
- name: memcached-operator
image: quay.io/coreos/operator-sdk-dev:test-framework-operator
Expand Down
1 change: 1 addition & 0 deletions test/test-framework/deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ spec:
labels:
name: memcached-operator
spec:
serviceAccountName: memcached-operator
containers:
- name: memcached-operator
image: quay.io/coreos/operator-sdk-dev:test-framework-operator
Expand Down
4 changes: 2 additions & 2 deletions test/test-framework/deploy/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ rules:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: default-account-memcached-operator
name: memcached-operator
subjects:
- kind: ServiceAccount
name: default
name: memcached-operator
roleRef:
kind: Role
name: memcached-operator
Expand Down
4 changes: 4 additions & 0 deletions test/test-framework/deploy/sa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: ServiceAccount
apiVersion: v1
metadata:
name: memcached-operator

0 comments on commit ef00bed

Please sign in to comment.