Skip to content

Commit 4c16a1b

Browse files
jberkhahnEric Stroczynski
andauthored
update golang docs on CRD scoping (#4812)
Signed-off-by: Jonathan Berkhahn <[email protected]> Co-authored-by: Eric Stroczynski <[email protected]>
1 parent a507c61 commit 4c16a1b

File tree

1 file changed

+44
-55
lines changed

1 file changed

+44
-55
lines changed

website/content/en/docs/building-operators/golang/crds-scope.md

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,54 @@ weight: 60
66

77
## Overview
88

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+
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.
1110

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`.
11+
Custom Resource Definitions (CRDs) contain a scope field that determines whether the resulting Custom Resource (CR)
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.
1415

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`.
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.
2019

21-
**NOTE**: When a `Manager` instance is created in the `main.go` file, it receives the namespace(s) as Options.
22-
These namespace(s) should be watched and cached for the Client which is provided by the Controllers. Only clients
23-
provided by cluster-scoped projects where the `Namespace` attribute is `""` will be able to manage cluster-scoped CRs.
24-
For more information see the [Manager][manager_user_guide] topic in the user guide and the
25-
[Manager Options][manager_options].
20+
## Set `create api` --namespaced flag
2621

27-
## Example for changing the CRD scope from Namespaced to Cluster
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:
2824

29-
- Check the `spec.names.plural` in the CRD's Kind YAML file
25+
```console
26+
$ operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource=true --controller=true --namespaced=false
27+
```
3028

31-
* `/config/crd/bases/cache.example.com_memcacheds.yaml`
32-
```YAML
33-
apiVersion: apiextensions.k8s.io/v1beta1
34-
kind: CustomResourceDefinition
35-
metadata:
36-
annotations:
37-
controller-gen.kubebuilder.io/version: v0.2.5
38-
creationTimestamp: null
39-
name: memcacheds.cache.example.com
40-
spec:
41-
group: cache.example.com
42-
names:
43-
kind: Memcached
44-
listKind: MemcachedList
45-
plural: memcacheds
46-
singular: memcached
47-
scope: Namespaced
48-
subresources:
49-
status: {}
50-
...
51-
```
29+
## Set Scope Marker in types.go
5230

53-
- Update the `apis/<version>/<kind>_types.go` by adding the
54-
marker `//+kubebuilder:resource:path=<resource>,scope=Cluster`
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]
32+
to your resource. This file is usually located in `api/<version>/<kind>_types.go` or `apis/<group>/<version>/<kind>_types.go` if
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.
34+
Here is an example API type with the marker set to cluster scope:
5535

56-
* `api/v1alpha1/memcached_types.go`
36+
```golang
37+
//+kubebuilder:object:root=true
38+
//+kubebuilder:subresource:status
39+
//+kubebuilder:resource:scope=Cluster
5740

58-
```Go
5941
// Memcached is the Schema for the memcacheds API
60-
//+kubebuilder:resource:path=memcacheds,scope=Cluster
6142
type Memcached struct {
62-
metav1.TypeMeta `json:",inline"`
63-
metav1.ObjectMeta `json:"metadata,omitempty"`
43+
metav1.TypeMeta `json:",inline"`
44+
metav1.ObjectMeta `json:"metadata,omitempty"`
6445

65-
Spec MemcachedSpec `json:"spec,omitempty"`
66-
Status MemcachedStatus `json:"status,omitempty"`
46+
Spec MemcachedSpec `json:"spec,omitempty"`
47+
Status MemcachedStatus `json:"status,omitempty"`
6748
}
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`
49+
```
50+
To set the scope to namespaced, the marker would be set to `//+kubebuilder:resource:scope=Namespace` instead.
51+
52+
53+
## Set scope in CRD YAML file
54+
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`.
56+
An example YAML file for a namespace-scoped CRD is shown below:
7257

7358
```YAML
7459
apiVersion: apiextensions.k8s.io/v1beta1
@@ -85,13 +70,17 @@ spec:
8570
listKind: MemcachedList
8671
plural: memcacheds
8772
singular: memcached
88-
scope: Cluster
73+
scope: Namespaced
8974
subresources:
9075
status: {}
9176
...
9277
```
93-
94-
[RBAC]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
95-
[manager_user_guide]:/docs/building-operators/golang/tutorial/#manager
78+
79+
80+
9681
[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
82+
[manager_user_guide]:/docs/building-operators/golang/tutorial/#manager
83+
[k8s_crd_scope]: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition
84+
[kubebuilder_crd_markers]: https://book.kubebuilder.io/reference/markers/crd.html
85+
[kubebuilder_multigroup]: https://book.kubebuilder.io/migration/multi-group.html
86+
[RBAC]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/

0 commit comments

Comments
 (0)