Skip to content

Commit 8d0f071

Browse files
committed
Merge branch 'master' into incluster-test
2 parents db5bee2 + 72431e2 commit 8d0f071

File tree

12 files changed

+130
-19
lines changed

12 files changed

+130
-19
lines changed

MAINTAINERS

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# This is the official list of operator-sdk maintainers.
22
#
33
# Names should be added to this file like so:
4-
# Individual's name <submission email address> (@GITHUB_HANDLE) pkg:*
5-
# Individual's name <submission email address> <email2> <emailN> (@GITHUB_HANDLE) pkg:*
6-
#
7-
# Please keep the list sorted.
4+
# Individual's name <submission email address> (@GITHUB_HANDLE)
5+
# Individual's name <submission email address> <email2> <emailN> (@GITHUB_HANDLE)
86

9-
Fanmin Shi <[email protected]> <[email protected]> (@fanminshi) pkg:*
10-
Haseeb Tariq <[email protected]> <[email protected]> (@hasbro17) pkg:*
11-
Sebastien Pahl <[email protected]> (@spahl) pkg:*
12-
Verónica López <[email protected]> (@Verolop) pkg:*
13-
Rithu Leena John <[email protected]> (@rithujohn191) pkg:*
7+
Fanmin Shi <[email protected]> (@fanminshi)
8+
Haseeb Tariq <[email protected]> (@hasbro17)
9+
Sebastien Pahl <[email protected]> (@spahl)
10+
Rithu Leena John <[email protected]> (@rithujohn191)
11+
Alexander Pavel <[email protected]> (@AlexNPavel)
12+
Eric Stroczynski <[email protected]> (@estroz)
13+
Shawn Hurley <[email protected]> (@shawn-hurley)
14+
Lili Cosic <[email protected]> (@LiliC)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Create and deploy an app-operator using the SDK CLI:
5757

5858
```sh
5959
# Create an app-operator project that defines the App CR.
60+
$ mkdir -p $GOPATH/src/github.com/example-inc/
6061
$ cd $GOPATH/src/github.com/example-inc/
6162
$ operator-sdk new app-operator --api-version=app.example.com/v1alpha1 --kind=App
6263
$ cd app-operator
@@ -69,6 +70,7 @@ $ docker push quay.io/example/app-operator
6970
$ sed -i 's|REPLACE_IMAGE|quay.io/example/app-operator|g' deploy/operator.yaml
7071

7172
# Deploy the app-operator
73+
$ kubectl create -f deploy/sa.yaml
7274
$ kubectl create -f deploy/rbac.yaml
7375
$ kubectl create -f deploy/crd.yaml
7476
$ kubectl create -f deploy/operator.yaml
@@ -86,6 +88,7 @@ $ kubectl delete -f deploy/cr.yaml
8688
$ kubectl delete -f deploy/crd.yaml
8789
$ kubectl delete -f deploy/operator.yaml
8890
$ kubectl delete -f deploy/rbac.yaml
91+
$ kubectl delete -f deploy/sa.yaml
8992
```
9093

9194
## User Guide

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
5858
if len(args) != 1 {
5959
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("operator-sdk test local requires exactly 1 argument"))
6060
}
61-
// if no namespaced manifest path is given, combine deploy/rbac.yaml and deploy/operator.yaml
61+
// if no namespaced manifest path is given, combine deploy/sa.yaml, deploy/rbac.yaml and deploy/operator.yaml
6262
if namespacedManifestPath == "" {
6363
os.Mkdir("deploy/test", os.FileMode(int(0775)))
6464
namespacedManifestPath = "deploy/test/namespace-manifests.yaml"
65+
sa, err := ioutil.ReadFile("deploy/sa.yaml")
66+
if err != nil {
67+
log.Fatalf("could not find sa manifest: %v", err)
68+
}
6569
rbac, err := ioutil.ReadFile("deploy/rbac.yaml")
6670
if err != nil {
6771
log.Fatalf("could not find rbac manifest: %v", err)
@@ -70,7 +74,9 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
7074
if err != nil {
7175
log.Fatalf("could not find operator manifest: %v", err)
7276
}
73-
combined := append(rbac, []byte("\n---\n")...)
77+
combined := append(sa, []byte("\n---\n")...)
78+
combined = append(combined, rbac...)
79+
combined = append(combined, []byte("\n---\n")...)
7480
combined = append(combined, operator...)
7581
err = ioutil.WriteFile(namespacedManifestPath, combined, os.FileMode(int(0664)))
7682
if err != nil {

doc/user-guide.md

Lines changed: 44 additions & 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
```
@@ -265,6 +266,44 @@ $ kubectl delete -f deploy/cr.yaml
265266
$ kubectl delete -f deploy/operator.yaml
266267
```
267268

269+
270+
## Advanced Topics
271+
### Adding 3rd Party Resources To Your Operator
272+
To add a resource to an operator, you must add it to a scheme. By creating an `AddToScheme` method or reusing one you can easily add a resource to your scheme. An [example][deployments_register] shows that you define a function and then use the [runtime][runtime_package] package to create a `SchemeBuilder`
273+
274+
#### Current Operator-SDK
275+
You then need to tell the operators to use these functions to add the resources to its scheme. In operator-sdk you use [AddToSDKScheme][osdk_add_to_scheme] to add this.
276+
Example of you main.go:
277+
```go
278+
import (
279+
....
280+
appsv1 "k8s.io/api/apps/v1"
281+
)
282+
283+
func main() {
284+
k8sutil.AddToSDKScheme(appsv1.AddToScheme)`
285+
sdk.Watch(appsv1.SchemeGroupVersion.String(), "Deployments", <namespace>, <resyncPeriod>)
286+
}
287+
```
288+
289+
#### Future with Controller Runtime
290+
When using controller runtime, you will also need to tell its scheme about your resourece. In controller runtime to add to the scheme, you can get the managers [scheme][manager_scheme]. If you would like to see what kubebuilder generates to add the resoureces to the [scheme][simple_resource].
291+
Example:
292+
```go
293+
import (
294+
....
295+
appsv1 "k8s.io/api/apps/v1"
296+
)
297+
298+
func main() {
299+
....
300+
if err := appsv1.AddToScheme(mgr.GetScheme()); err != nil {
301+
log.Fatal(err)
302+
}
303+
....
304+
}
305+
```
306+
268307
[memcached_handler]: ../example/memcached-operator/handler.go.tmpl
269308
[layout_doc]:./project_layout.md
270309
[dep_tool]:https://golang.github.io/dep/docs/installation.html
@@ -273,3 +312,8 @@ $ kubectl delete -f deploy/operator.yaml
273312
[docker_tool]:https://docs.docker.com/install/
274313
[kubectl_tool]:https://kubernetes.io/docs/tasks/tools/install-kubectl/
275314
[minikube_tool]:https://github.com/kubernetes/minikube#installation
315+
[manager_scheme]: https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/manager/manager.go#L61
316+
[simple_resource]: https://book.kubebuilder.io/basics/simple_resource.html
317+
[deployments_register]: https://github.com/kubernetes/api/blob/master/apps/v1/register.go#L41
318+
[runtime_package]: https://godoc.org/k8s.io/apimachinery/pkg/runtime
319+
[osdk_add_to_scheme]: https://github.com/operator-framework/operator-sdk/blob/4179b6ac459b2b0cb04ab3a1b438c280bd28d1a5/pkg/util/k8sutil/k8sutil.go#L67

pkg/generator/generator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const (
6363
config = "config.yaml"
6464
rbacYaml = "rbac.yaml"
6565
crYaml = "cr.yaml"
66+
saYaml = "sa.yaml"
6667
catalogPackageYaml = "package.yaml"
6768
catalogCSVYaml = "csv.yaml"
6869
crdYaml = "crd.yaml"
@@ -80,6 +81,7 @@ const (
8081
rbacTmplName = "deploy/rbac.yaml"
8182
crTmplName = "deploy/cr.yaml"
8283
testYamlName = "deploy/test-pod.yaml"
84+
saTmplName = "deploy/sa.yaml"
8385
pluralSuffix = "s"
8486
)
8587

@@ -240,6 +242,13 @@ func renderDeployFiles(deployDir, projectName, apiVersion, kind string) error {
240242
return err
241243
}
242244

245+
saTd := tmplData{
246+
ProjectName: projectName,
247+
}
248+
if err := renderWriteFile(filepath.Join(deployDir, saYaml), saTmplName, saYamlTmpl, saTd); err != nil {
249+
return err
250+
}
251+
243252
opTd := tmplData{
244253
ProjectName: projectName,
245254
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
@@ -459,6 +459,7 @@ spec:
459459
labels:
460460
name: {{.ProjectName}}
461461
spec:
462+
serviceAccountName: {{.ProjectName}}
462463
containers:
463464
- name: {{.ProjectName}}
464465
image: {{.Image}}
@@ -515,16 +516,22 @@ rules:
515516
kind: RoleBinding
516517
apiVersion: rbac.authorization.k8s.io/v1beta1
517518
metadata:
518-
name: default-account-{{.ProjectName}}
519+
name: {{.ProjectName}}
519520
subjects:
520521
- kind: ServiceAccount
521-
name: default
522+
name: {{.ProjectName}}
522523
roleRef:
523524
kind: Role
524525
name: {{.ProjectName}}
525526
apiGroup: rbac.authorization.k8s.io
526527
`
527528

529+
const saYamlTmpl = `apiVersion: v1
530+
kind: ServiceAccount
531+
metadata:
532+
name: {{.ProjectName}}
533+
`
534+
528535
const crYamlTmpl = `apiVersion: "{{.APIVersion}}"
529536
kind: "{{.Kind}}"
530537
metadata:

test/e2e/memcached_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ func MemcachedCluster(t *testing.T) {
273273
}
274274
}
275275

276+
// create sa
277+
saYAML, err := ioutil.ReadFile("deploy/sa.yaml")
278+
if err != nil {
279+
t.Fatal(err)
280+
}
281+
err = ctx.CreateFromYAML(saYAML)
282+
if err != nil {
283+
t.Fatal(err)
284+
}
285+
t.Log("Created sa")
286+
276287
// create rbac
277288
rbacYAML, err := ioutil.ReadFile("deploy/rbac.yaml")
278289
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)