Skip to content

Commit ba7f4b4

Browse files
committed
Better documentation on watching things
1 parent 98b9835 commit ba7f4b4

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Subpackages of apis are created when running the following command to create a n
102102
kubebuilder create resource --group mygroup --version v1beta1 --kind MyKind
103103
```
104104

105+
**Note:** While `create resource` automatically runs the code generators for the user, when
106+
the user changes the resource file or adds `// +kubebuilder` annotations to the controller,
107+
they will need to run `kubebuilder generate` to rerun the code generators.
108+
105109
### pkg/controllers
106110

107111
**Users must edit packages under this package**
@@ -119,6 +123,10 @@ kubebuilder create resource --group mygroup --version v1beta1 --kind MyKind
119123
See documentation and examples of annotations in godoc format here:
120124
- [gen/controller](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/controller#example-package)
121125

126+
**Note:** While `create resource` automatically runs the code generators for the user, when
127+
the user changes the resource file or adds `// +kubebuilder` annotations to the controller,
128+
they will need to run `kubebuilder generate` to rerun the code generators.
129+
122130
### pkg/inject
123131

124132
*Most users do not need to edit this package.*
@@ -201,20 +209,26 @@ this example:
201209

202210
[GenericController](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController)
203211

204-
Use the following annotation to start additional informers for any resources you are watching in your controller.
205212

206-
```go
207-
// +informers:group=apps,version=v1,kind=Deployment
208-
```
213+
To Watch additional resources from your controller do the following in your controller.go:
209214

210-
Use the following annotation to generate RBAC rules to allow your controller to read and write resources when
211-
running in a container in a Kubernetes cluster.
215+
1. Add a `gc.Watch*` call to the `ProvideController`. e.g. Call gc.[WatchTransformationKeyOf](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController-WatchTransformationKeyOf)
216+
- This will trigger Reconcile calls for events
217+
2. Add an [// +informers:](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/controller#example-package) annotation
218+
to the `type <Kind>Controller struct` with the type of the resource you are watching
219+
- This will make sure the informers that watch for events are started
220+
3. Add an [// +rbac:](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/controller#example-package)
221+
annotation to the `type <Kind>Controller struct` with the type of the resource you are watching
222+
- This will make sure the RBAC rules that allow the controller to watch events in a cluster are generated
223+
224+
Example:
212225

213226
```go
214-
// +rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
227+
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list
228+
// +kubebuilder:informers:group=core,version=v1,kind=Pod
229+
type FooController struct{}
215230
```
216231

217-
218232
### Watching resources
219233

220234
Controllers watch resources to trigger reconcile functions. It is common for controllers to reconcile a single resource

cmd/kubebuilder/create/resource/controller.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,18 @@ func ProvideController(arguments args.InjectArgs) (*controller.GenericController
9090
return gc, err
9191
}
9292
93-
// INSERT ADDITIONAL WATCHES HERE BY CALLING gc.Watch.*() FUNCTIONS
94-
// NOTE: Informers for Kubernetes resources *MUST* be registered in the pkg/inject package so that they are started.
93+
// IMPORTANT:
94+
// To watch additional resource types - such as those created by your controller - add gc.Watch* function calls here
95+
// Watch function calls will transform each object event into a {{.Kind}} Key to be reconciled by the controller.
96+
//
97+
// **********
98+
// For any new Watched types, you MUST add the appropriate // +kubebuilder:informer and // +kubebuilder:rbac
99+
// annotations to the {{.Kind}}Controller and run "kubebuilder generate.
100+
// This will generate the code to start the informers and create the RBAC rules needed for running in a cluster.
101+
// See:
102+
// https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/controller#example-package
103+
// **********
104+
95105
return gc, nil
96106
}
97107
`

cmd/kubebuilder/create/resource/resource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ import (
6262
// {{.Kind}}Spec defines the desired state of {{.Kind}}
6363
type {{.Kind}}Spec struct {
6464
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
65+
// Important: Run "kubebuilder generate" to regenerate code after modifying this file
6566
}
6667
6768
// {{.Kind}}Status defines the observed state of {{.Kind}}
6869
type {{.Kind}}Status struct {
6970
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
71+
// Important: Run "kubebuilder generate" to regenerate code after modifying this file
7072
}
7173
7274
// +genclient

0 commit comments

Comments
 (0)