Skip to content

Commit 934a03d

Browse files
authored
*: add back service account (#629)
* *: add back service account This adds service account support back to the sdk, as well as updates some documentation and fixes the `dep` warning when creating a new project * *: rename sa.yaml to service_account.yaml
1 parent 9db4b49 commit 934a03d

25 files changed

+160
-70
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ $ docker push quay.io/example/app-operator
7676
# Update the operator manifest to use the built image name
7777
$ sed -i 's|REPLACE_IMAGE|quay.io/example/app-operator|g' deploy/operator.yaml
7878

79+
# Setup Service Account
80+
$ kubectl create -f deploy/service_account.yaml
7981
# Setup RBAC
8082
$ kubectl create -f deploy/role.yaml
8183
$ kubectl create -f deploy/role_binding.yaml
82-
# TODO: kubectl create -f deploy/service_account.yaml
8384
# Setup the CRD
8485
$ kubectl create -f deploy/crds/app_v1alpha1_appservice_crd.yaml
8586
# Deploy the app-operator
@@ -99,6 +100,7 @@ $ kubectl delete -f deploy/app_v1alpha1_appservice_cr.yaml
99100
$ kubectl delete -f deploy/operator.yaml
100101
$ kubectl delete -f deploy/role.yaml
101102
$ kubectl delete -f deploy/role_binding.yaml
103+
$ kubectl delete -f deploy/service_account.yaml
102104
$ kubectl delete -f deploy/crds/app_v1alpha1_appservice_crd.yaml
103105
```
104106

commands/operator-sdk/cmd/new.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func doScaffold() {
135135
err := s.Execute(cfg,
136136
&scaffold.Cmd{},
137137
&scaffold.Dockerfile{},
138+
&scaffold.ServiceAccount{},
138139
&scaffold.Role{},
139140
&scaffold.RoleBinding{},
140141
&scaffold.Operator{},
@@ -180,6 +181,7 @@ func doAnsibleScaffold() {
180181
GeneratePlaybook: generatePlaybook,
181182
},
182183
galaxyInit,
184+
&scaffold.ServiceAccount{},
183185
&scaffold.Role{},
184186
&scaffold.RoleBinding{},
185187
&ansible.Operator{},

commands/operator-sdk/cmd/test/local.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,18 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
6363
if len(args) != 1 {
6464
log.Fatalf("operator-sdk test local requires exactly 1 argument")
6565
}
66-
// if no namespaced manifest path is given, combine deploy/sa.yaml, deploy/rbac.yaml and deploy/operator.yaml
66+
// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
6767
if tlConfig.namespacedManPath == "" {
6868
err := os.MkdirAll("deploy/test", os.FileMode(cmdutil.DefaultDirFileMode))
6969
if err != nil {
7070
log.Fatalf("could not create deploy/test: %v", err)
7171
}
7272
tlConfig.namespacedManPath = "deploy/test/namespace-manifests.yaml"
7373

74-
// TODO: re-enable sa creation once that's added to the refactor branch
75-
/*
76-
sa, err := ioutil.ReadFile("deploy/sa.yaml")
77-
if err != nil {
78-
log.Fatalf("could not find sa manifest: %v", err)
79-
}
80-
*/
74+
sa, err := ioutil.ReadFile("deploy/service_account.yaml")
75+
if err != nil {
76+
log.Fatalf("could not find the manifest deploy/service_account.yaml: %v", err)
77+
}
8178
role, err := ioutil.ReadFile("deploy/role.yaml")
8279
if err != nil {
8380
log.Fatalf("could not find role manifest: %v", err)
@@ -90,12 +87,9 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
9087
if err != nil {
9188
log.Fatalf("could not find operator manifest: %v", err)
9289
}
93-
/*
94-
combined := append(sa, []byte("\n---\n")...)
95-
combined = append(combined, rbac...)
96-
combined = append(combined, []byte("\n---\n")...)
97-
*/
98-
combined := append(role, []byte("\n---\n")...)
90+
combined := append(sa, []byte("\n---\n")...)
91+
combined = append(combined, role...)
92+
combined = append(combined, []byte("\n---\n")...)
9993
combined = append(combined, roleBinding...)
10094
combined = append(combined, []byte("\n---\n")...)
10195
combined = append(combined, operator...)

doc/ansible/user-guide.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ resource definition the operator will be watching.
179179
Deploy the CRD:
180180

181181
```sh
182-
$ kubectl create -f deploy/crd.yaml
182+
$ kubectl create -f deploy/crds/cache_v1alpha1_memcached_crd.yaml
183183
```
184184

185185
Once this is done, there are two ways to run the operator:
@@ -207,7 +207,9 @@ $ sed -i 's|REPLACE_IMAGE|quay.io/example/memcached-operator:v0.0.1|g' deploy/op
207207
Deploy the memcached-operator:
208208

209209
```sh
210-
$ kubectl create -f deploy/rbac.yaml
210+
$ kubectl create -f deploy/service_account.yaml
211+
$ kubectl create -f deploy/role.yaml
212+
$ kubectl create -f deploy/role_binding.yaml
211213
$ kubectl create -f deploy/operator.yaml
212214
```
213215

@@ -268,7 +270,7 @@ metadata:
268270
spec:
269271
size: 3
270272
271-
$ kubectl apply -f deploy/cr.yaml
273+
$ kubectl apply -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
272274
```
273275

274276
Ensure that the memcached-operator creates the deployment for the CR:
@@ -305,7 +307,7 @@ metadata:
305307
spec:
306308
size: 4
307309
308-
$ kubectl apply -f deploy/cr.yaml
310+
$ kubectl apply -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
309311
```
310312

311313
Confirm that the operator changes the deployment size:
@@ -321,8 +323,12 @@ example-memcached 4 4 4 4 5m
321323
Clean up the resources:
322324

323325
```sh
324-
$ kubectl delete -f deploy/cr.yaml
326+
$ kubectl delete -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
325327
$ kubectl delete -f deploy/operator.yaml
328+
$ kubectl delete -f deploy/role_binding.yaml
329+
$ kubectl delete -f deploy/role.yaml
330+
$ kubectl delete -f deploy/service_account.yaml
331+
$ kubectl delete -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
326332
```
327333

328334
[layout_doc]:./project_layout.md

doc/dev/testing/end-to-end.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ testing process, the cleanup functions will not run. To manually clean up a test
4747
1. Delete the created project in $GOPATH/src/github.com/example-inc/memcached-operator
4848
2. Delete the namespaces that the tests run in, which also deletes any resources created
4949
within the namespaces. The namespaces start with `memcached-memcached-group`.
50-
3. Delete the CRD (`kubectl delete -f deploy/crd.yaml`).
50+
3. Delete the CRD (`kubectl delete -f deploy/crds/cache_v1alpha1_memcached_crd.yaml`).

doc/sdk-cli-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Runs the tests locally
199199
##### Flags
200200
* `--kubeconfig` string - location of kubeconfig for kubernetes cluster (default "~/.kube/config")
201201
* `--global-manifest` string - path to manifest for global resources (default "deploy/crd.yaml)
202-
* `--namespaced-manifest` string - path to manifest for per-test, namespaced resources (default: combines deploy/sa.yaml, deploy/rbac.yaml, and deploy/operator.yaml)
202+
* `--namespaced-manifest` string - path to manifest for per-test, namespaced resources (default: combines deploy/service_account.yaml, deploy/rbac.yaml, and deploy/operator.yaml)
203203
* `--namespace` string - if non-empty, single namespace to run tests in (e.g. "operator-test") (default: "")
204204
* `--go-test-flags` string - extra arguments to pass to `go test` (e.g. -f "-v -parallel=2")
205205
* `-h, --help` - help for local

doc/test-framework/writing-e2e-tests.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ functions will automatically be run since they were deferred when the TestCtx wa
224224

225225
To make running the tests simpler, the `operator-sdk` CLI tool has a `test` subcommand that can configure
226226
default test settings, such as locations of your global resource manifest file (by default
227-
`deploy/crd.yaml`) and your namespaced resource manifest file (by default `deploy/sa.yaml` concatenated with
227+
`deploy/crd.yaml`) and your namespaced resource manifest file (by default `deploy/service_account.yaml` concatenated with
228228
`deploy/rbac.yaml` and `deploy/operator.yaml`), and allows the user to configure runtime options. There are 2 ways to use the
229229
subcommand: local and cluster.
230230
### Local
@@ -249,8 +249,8 @@ in [MainEntry][main-entry-link] are declared, the tests will run correctly. Runn
249249
will result in undefined behavior. This is an example `go test` equivalent to the `operator-sdk test local` example above:
250250

251251
```shell
252-
# Combine sa, rbac, operator manifest into namespaced manifest
253-
$ cp deploy/sa.yaml deploy/namespace-init.yaml
252+
# Combine service_account, rbac, operator manifest into namespaced manifest
253+
$ cp deploy/service_account.yaml deploy/namespace-init.yaml
254254
$ echo -e "\n---\n" >> deploy/namespace-init.yaml
255255
$ cat deploy/rbac.yaml >> deploy/namespace-init.yaml
256256
$ echo -e "\n---\n" >> deploy/namespace-init.yaml
@@ -277,10 +277,11 @@ Once the image is ready, the tests are ready to be run. To run the tests, make s
277277
and a namespace with proper rbac configured:
278278

279279
```shell
280-
$ kubectl create -f deploy/crd.yaml
280+
$ kubectl create -f deploy/crds/cache_v1alpha1_memcached_crd.yaml
281281
$ kubectl create namespace memcached-test
282-
$ kubectl create -f deploy/sa.yaml -n memcached-test
283-
$ kubectl create -f deploy/rbac.yaml -n memcached-test
282+
$ kubectl create -f deploy/service_account.yaml -n memcached-test
283+
$ kubectl create -f deploy/role.yaml -n memcached-test
284+
$ kubectl create -f deploy/role_binding.yaml -n memcached-test
284285
```
285286

286287
Once you have your environment properly configured, you can start the tests using the `operator-sdk test cluster` command:
@@ -328,7 +329,7 @@ $ kubectl delete namespace main-153428703
328329
Since the CRD is not namespaced, it must be deleted separately. Clean up the CRD created by the tests using the CRD manifest `deploy/crd.yaml`:
329330

330331
```shell
331-
$ kubectl delete -f deploy/crd.yaml
332+
$ kubectl delete -f deploy/crds/cache_v1alpha1_memcached_crd.yaml
332333
```
333334

334335
[memcached-sample]:https://github.com/operator-framework/operator-sdk-samples/tree/master/memcached-operator

doc/user-guide.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ The Deployment manifest is generated at `deploy/operator.yaml`. Be sure to updat
185185
Setup RBAC and deploy the memcached-operator:
186186

187187
```sh
188+
$ kubectl create -f deploy/service_account.yaml
188189
$ kubectl create -f deploy/role.yaml
189190
$ kubectl create -f deploy/role_binding.yaml
190-
# TODO: $ kubectl create -f deploy/service_account.yaml
191191
$ kubectl create -f deploy/operator.yaml
192192
```
193193

@@ -311,6 +311,9 @@ Clean up the resources:
311311
```sh
312312
$ kubectl delete -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
313313
$ kubectl delete -f deploy/operator.yaml
314+
$ kubectl delete -f deploy/role_binding.yaml
315+
$ kubectl delete -f deploy/role.yaml
316+
$ kubectl delete -f deploy/service_account.yaml
314317
```
315318

316319
## Advanced Topics

pkg/scaffold/ansible/operator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ spec:
4646
labels:
4747
name: {{.ProjectName}}
4848
spec:
49+
serviceAccountName: {{.ProjectName}}
4950
containers:
5051
- name: {{.ProjectName}}
5152
# Replace this with the built image name

pkg/scaffold/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
versionFile = "version.go"
4949
docFile = "doc.go"
5050
registerFile = "register.go"
51+
serviceAccountYamlFile = "service_account.yaml"
5152
roleYamlFile = "role.yaml"
5253
roleBindingYamlFile = "role_binding.yaml"
5354
operatorYamlFile = "operator.yaml"

pkg/scaffold/gopkgtoml.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,4 @@ required = [
8484
[[prune.project]]
8585
name = "k8s.io/code-generator"
8686
non-go = false
87-
unused-packages = false
8887
`

pkg/scaffold/gopkgtoml_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,4 @@ required = [
8888
[[prune.project]]
8989
name = "k8s.io/code-generator"
9090
non-go = false
91-
unused-packages = false
9291
`

pkg/scaffold/operator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ spec:
4646
labels:
4747
name: {{.ProjectName}}
4848
spec:
49+
serviceAccountName: {{.ProjectName}}
4950
containers:
5051
- name: {{.ProjectName}}
5152
# Replace this with the built image name

pkg/scaffold/operator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ spec:
4848
labels:
4949
name: app-operator
5050
spec:
51+
serviceAccountName: app-operator
5152
containers:
5253
- name: app-operator
5354
# Replace this with the built image name

pkg/scaffold/rolebinding.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func (s *RoleBinding) GetInput() (input.Input, error) {
3535
const roleBindingTemplate = `kind: RoleBinding
3636
apiVersion: rbac.authorization.k8s.io/v1
3737
metadata:
38-
name: default-account-{{.ProjectName}}
38+
name: {{.ProjectName}}
3939
subjects:
4040
- kind: ServiceAccount
41-
name: default
41+
name: {{.ProjectName}}
4242
roleRef:
4343
kind: Role
4444
name: {{.ProjectName}}

pkg/scaffold/rolebinding_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func TestRoleBinding(t *testing.T) {
3737
const rolebindingExp = `kind: RoleBinding
3838
apiVersion: rbac.authorization.k8s.io/v1
3939
metadata:
40-
name: default-account-app-operator
40+
name: app-operator
4141
subjects:
4242
- kind: ServiceAccount
43-
name: default
43+
name: app-operator
4444
roleRef:
4545
kind: Role
4646
name: app-operator

pkg/scaffold/service_account.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package scaffold
16+
17+
import (
18+
"path/filepath"
19+
20+
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
21+
)
22+
23+
type ServiceAccount struct {
24+
input.Input
25+
}
26+
27+
func (s *ServiceAccount) GetInput() (input.Input, error) {
28+
if s.Path == "" {
29+
s.Path = filepath.Join(deployDir, serviceAccountYamlFile)
30+
}
31+
s.TemplateBody = serviceAccountTemplate
32+
return s.Input, nil
33+
}
34+
35+
const serviceAccountTemplate = `apiVersion: v1
36+
kind: ServiceAccount
37+
metadata:
38+
name: {{.ProjectName}}
39+
`

pkg/scaffold/service_account_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package scaffold
16+
17+
import (
18+
"testing"
19+
20+
"github.com/sergi/go-diff/diffmatchpatch"
21+
)
22+
23+
func TestServiceAccount(t *testing.T) {
24+
s, buf := setupScaffoldAndWriter()
25+
err := s.Execute(appConfig, &ServiceAccount{})
26+
if err != nil {
27+
t.Fatalf("failed to execute the scaffold: (%v)", err)
28+
}
29+
30+
if serviceAccountExp != buf.String() {
31+
dmp := diffmatchpatch.New()
32+
diffs := diffmatchpatch.New().DiffMain(serviceAccountExp, buf.String(), false)
33+
t.Fatalf("expected vs actual differs. Red text is missing and green text is extra.\n%v", dmp.DiffPrettyText(diffs))
34+
}
35+
}
36+
37+
const serviceAccountExp = `apiVersion: v1
38+
kind: ServiceAccount
39+
metadata:
40+
name: app-operator
41+
`
39.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)