Skip to content

Commit eaceb3d

Browse files
jberkhahnEric Stroczynski
and
Eric Stroczynski
authored
add estroz suggestions
Co-authored-by: Eric Stroczynski <[email protected]>
1 parent a711b85 commit eaceb3d

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,32 @@ weight: 60
66

77
## Overview
88

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+
911
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.
1515

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

2120
## Set `create api` --namespaced flag
2221

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:
2524

26-
```bash
25+
```console
2726
$ operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource=true --controller=true --namespaced=false
2827
```
2928

3029
## Set Scope Marker in types.go
3130

32-
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]
3332
to your resource. This file is usually located in `api/<version>/<kind>_types.go` or `apis/<group>/<version>/<kind>_types.go` if
3433
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:
3635

3736
```golang
3837
//+kubebuilder:object:root=true
@@ -50,15 +49,10 @@ type Memcached struct {
5049
```
5150
To set the scope to namespaced, the marker would be set to `//+kubebuilder:resource:scope=Namespace` instead.
5251

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].
5852

5953
## Set scope in CRD YAML file
6054

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`.
6256
An example YAML file for a namespace-scoped CRD is shown below:
6357

6458
```YAML
@@ -82,10 +76,7 @@ spec:
8276
...
8377
```
8478

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.
8779

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.
8980

9081
[manager_options]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/manager#Options
9182
[manager_user_guide]:/docs/building-operators/golang/tutorial/#manager

0 commit comments

Comments
 (0)