You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/content/en/docs/building-operators/golang/crds-scope.md
+14-23Lines changed: 14 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -6,33 +6,32 @@ weight: 60
6
6
7
7
## Overview
8
8
9
+
This page details the various methods to control the scope of a CRD. See the [operator scope doc](/docs/building-operators/golang/operator-scope/) for information on configuring operator scope, such as which namespaces to watch.
10
+
9
11
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.
12
+
is cluster or namespace scoped. An operator author might use a namespaced-scoped CRD
13
+
to restrict access to a CR to certain namespaces, or to have different versions of CRs accessible in different namespaces.
14
+
Alternatively, an operator author might want a cluster-scoped CRD so all namespaces have visibility and access to CRs.
15
15
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.
16
+
The CRD manifests are generated by the `operator-sdk create api` command in `config/crd/bases`. A CRD's `spec.scope` field controls API scope; valid values are [`Cluster` and `Namespaced`](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition).
17
+
For an Operator-sdk Go project, this value is determined by the `operator-sdk create api --namespaced` boolean flag, which edits the
18
+
`types.go` file for the resource. For other operator types, the command edits `spec.scope` in the CRD's YAML manifest directly.
20
19
21
20
## Set `create api` --namespaced flag
22
21
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:
22
+
When creating a new API, the `--namespaced` flag controls whether the resulting CRD will be cluster or namespace scoped.
23
+
By default, `--namespaced` is set to `true` which sets the scope to `Namespaced`. An example command to create a cluster-scoped API would be:
You can also manually set the scope in the Golang types.go file by adding a[kubebuilder scope marker][kubebuilder_crd_markers]
31
+
You can also manually set the scope in the Go `types.go` file by adding or changing the[kubebuilder scope marker][kubebuilder_crd_markers]
33
32
to your resource. This file is usually located in `api/<version>/<kind>_types.go` or `apis/<group>/<version>/<kind>_types.go` if
34
33
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:
34
+
Here is an example API type with the marker set to cluster scope:
36
35
37
36
```golang
38
37
//+kubebuilder:object:root=true
@@ -50,15 +49,10 @@ type Memcached struct {
50
49
```
51
50
To set the scope to namespaced, the marker would be set to `//+kubebuilder:resource:scope=Namespace` instead.
52
51
53
-
**NOTE**: When a Manager instance is created in the `main.go` file, it receives the namespace(s) as Options.
54
-
These namespace(s) should be watched and cached for the Client which is provided by the Controllers. Only clients
55
-
provided by cluster-scoped projects where the `Namespace` attribute is `""` will be able to manage cluster-scoped CRs.
56
-
For more information see the [Manager][manager_user_guide] topic in the user guide and the
57
-
[Manager Options][manager_options].
58
52
59
53
## Set scope in CRD YAML file
60
54
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`.
55
+
The scope can be manually set directly in the CRD's Kind YAML file, normally located in `config/crd/bases/<group>.<domain>_<kind>.yaml`.
62
56
An example YAML file for a namespace-scoped CRD is shown below:
63
57
64
58
```YAML
@@ -82,10 +76,7 @@ spec:
82
76
...
83
77
```
84
78
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.
87
79
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.
0 commit comments