@@ -6,29 +6,61 @@ weight: 60
6
6
7
7
## Overview
8
8
9
- The CustomResourceDefinition (CRD) scope can also be changed for cluster-scoped operators so that there is only a single
10
- instance (for a given name) of the Custom Resource (CR) to manage across the cluster.
9
+ Custom Resource Definitions (CRDs) contain a scope field that determines whether the resulting Custom Resource (CR)
10
+ is cluster or namespace scoped. Operators can be built using this feature that then are available either on the entire cluster,
11
+ or only in one or more namespaces. This might be done for several reasons: an operator author might use a namespaced-scoped operator
12
+ to restrict access to a CR to a single namespace, or to have different versions of similar CRs accessible in different namespaces.
13
+ Alternatively, an operator author might want a cluster-scoped operator so all users can see and use a single operator.:
14
+ This page details the various methods to control the scope of an operator.
11
15
12
- The CRD manifests are generated in ` config/crd/bases ` . For each CRD that needs to be cluster-scoped, its manifest
13
- should specify ` spec.scope: Cluster ` .
16
+ The CRD manifests are generated by operator-sdk in ` config/crd/bases ` . The ` spec.scope ` field can be set to ` Cluster ` or ` Namespaced `
17
+ to determine how the CR will be scoped. For further information on this field, see [ the core k8s CRD docs] [ k8s_crd_scope ] .
18
+ For an Operator-sdk Golang project, this can be done by setting ` --namespaced ` when creating the api, by editing the Golang
19
+ ` types.go ` file for the resource, or by editing the YAML manifest for the CRD.
14
20
15
- To ensure that the CRD is always generated with ` scope: Cluster ` , add the marker
16
- ` //+kubebuilder:resource:path=<resource>,scope=Cluster ` , or if already present replace ` scope={Namespaced -> Cluster} ` ,
17
- above the CRD's Go type definition in ` api/<version>/<kind>_types.go ` or ` apis/<group>/<version>/<kind>_types.go `
18
- if you are using the [ multigroup] [ multigroup-kubebuilder-doc ] layout. Note that the ` <resource> `
19
- element must be the same lower-case plural value of the CRD's Kind, ` spec.names.plural ` .
21
+ ## Set ` create api ` --namespaced flag
20
22
21
- ** NOTE** : When a ` Manager ` instance is created in the ` main.go ` file, it receives the namespace(s) as Options.
23
+ When creating a new API, the ` --namespaced ` flag controls whether the resulting Custom Resource will be cluster or namespace scoped.
24
+ By default, ` --namespaced ` is set to ` true ` . An example command to create a cluster-scoped API would be:
25
+
26
+ ``` bash
27
+ $ operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource=true --controller=true --namespaced=false
28
+ ```
29
+
30
+ ## Set Scope Marker in types.go
31
+
32
+ You can also manually set the scope in the Golang types.go file by adding a [ kubebuilder scope marker] [ kubebuilder_crd_markers ]
33
+ to your resource. This file is usually located in ` api/<version>/<kind>_types.go ` or ` apis/<group>/<version>/<kind>_types.go ` if
34
+ you are using the [ multigroup] [ multigroup-kubebuilder-doc ] layout. Once this marker is set, the CRD files will be generated with the approriate scope.
35
+ Here is an example memcached type with the marker set to cluster scope:
36
+
37
+ ``` golang
38
+ // +kubebuilder:object:root=true
39
+ // +kubebuilder:subresource:status
40
+ // +kubebuilder:resource:scope=Cluster
41
+
42
+ // Memcached is the Schema for the memcacheds API
43
+ type Memcached struct {
44
+ metav1.TypeMeta ` json:",inline"`
45
+ metav1.ObjectMeta ` json:"metadata,omitempty"`
46
+
47
+ Spec MemcachedSpec ` json:"spec,omitempty"`
48
+ Status MemcachedStatus ` json:"status,omitempty"`
49
+ }
50
+ ```
51
+ To set the scope to namespaced, the marker would be set to ` //+kubebuilder:resource:scope=Namespace ` instead.
52
+
53
+ ** NOTE** : When a Manager instance is created in the ` main.go ` file, it receives the namespace(s) as Options.
22
54
These namespace(s) should be watched and cached for the Client which is provided by the Controllers. Only clients
23
55
provided by cluster-scoped projects where the ` Namespace ` attribute is ` "" ` will be able to manage cluster-scoped CRs.
24
56
For more information see the [ Manager] [ manager_user_guide ] topic in the user guide and the
25
57
[ Manager Options] [ manager_options ] .
26
58
27
- ## Example for changing the CRD scope from Namespaced to Cluster
59
+ ## Set scope in CRD YAML file
28
60
29
- - Check the ` spec.names.plural ` in the CRD's Kind YAML file
61
+ The scope can also be manually set directly in the CRD's Kind YAML file, normally located in ` config/crd/bases/<group>.<domain>_<kind>.yaml ` .
62
+ An example YAML file for a namespace-scoped CRD is shown below:
30
63
31
- * ` /config/crd/bases/cache.example.com_memcacheds.yaml `
32
64
``` YAML
33
65
apiVersion : apiextensions.k8s.io/v1beta1
34
66
kind : CustomResourceDefinition
@@ -50,48 +82,14 @@ spec:
50
82
...
51
83
```
52
84
53
- - Update the ` apis/<version>/<kind>_types.go ` by adding the
54
- marker ` //+kubebuilder:resource:path=<resource>,scope=Cluster `
85
+ ** NOTE ** This file will be overwritten if you regenerate the YAML manifests by running ` make manifests ` and haven't changed the corresponding resources in the types.go file.
86
+ Take care not to unintentionally overwrite your changes.
55
87
56
- * ` api/v1alpha1/memcached_types .go`
88
+ ** NOTE ** As above, you must also take care that the Manager in your ` main .go` is configured to correctly have access to your resources.
57
89
58
- ``` Go
59
- // Memcached is the Schema for the memcacheds API
60
- // +kubebuilder:resource:path=memcacheds,scope=Cluster
61
- type Memcached struct {
62
- metav1.TypeMeta ` json:",inline"`
63
- metav1.ObjectMeta ` json:"metadata,omitempty"`
64
-
65
- Spec MemcachedSpec ` json:"spec,omitempty"`
66
- Status MemcachedStatus ` json:"status,omitempty"`
67
- }
68
- ```
69
- - Run ` make manifests ` , to update the CRD manifest with the cluster scope setting, as in the following example:
70
-
71
- * ` /config/crd/bases/cache.example.com_memcacheds.yaml `
72
-
73
- ``` YAML
74
- apiVersion : apiextensions.k8s.io/v1beta1
75
- kind : CustomResourceDefinition
76
- metadata :
77
- annotations :
78
- controller-gen.kubebuilder.io/version : v0.2.5
79
- creationTimestamp : null
80
- name : memcacheds.cache.example.com
81
- spec :
82
- group : cache.example.com
83
- names :
84
- kind : Memcached
85
- listKind : MemcachedList
86
- plural : memcacheds
87
- singular : memcached
88
- scope : Cluster
89
- subresources :
90
- status : {}
91
- ...
92
- ```
93
-
94
- [ RBAC ] : https://kubernetes.io/docs/reference/access-authn-authz/rbac/
95
- [ manager_user_guide ] :/docs/building-operators/golang/tutorial/#manager
96
90
[ manager_options ] : https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/manager#Options
97
- [ multigroup-kubebuilder-doc ] : https://book.kubebuilder.io/migration/multi-group.html
91
+ [ manager_user_guide ] :/docs/building-operators/golang/tutorial/#manager
92
+ [ k8s_crd_scope] : https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition
93
+ [ kubebuilder_crd_markers ] : https://book.kubebuilder.io/reference/markers/crd.html
94
+ [ kubebuilder_multigroup ] : https://book.kubebuilder.io/migration/multi-group.html
95
+ [ RBAC ] : https://kubernetes.io/docs/reference/access-authn-authz/rbac/
0 commit comments