Skip to content

Commit 12f1fba

Browse files
author
Phillip Wittrock
authored
Merge pull request #51 from pwittrock/docs
Flesh out documentation on homepage
2 parents f6d0548 + 1ceb15c commit 12f1fba

File tree

1 file changed

+239
-2
lines changed

1 file changed

+239
-2
lines changed

README.md

Lines changed: 239 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,245 @@ kubebuilder docs
4949

5050
See the [user guide](docs/tools_user_guide.md) for more details
5151

52-
## Godocs
52+
## Project structure
53+
54+
Following describes the project structure setup by kubebuilder commands.
55+
56+
### cmd/
57+
58+
*Most users do not need to edit this package.*
59+
60+
The `cmd` package contains the main function for launching a new controller manager. It is responsible for parsing
61+
a `rest.Config` and invoking the `inject` package to run the various controllers. It may optionally install CRDs
62+
as part of starting up.
63+
64+
This package is created automatically by running:
65+
66+
```sh
67+
kubebuilder init --domain k8s.io
68+
```
69+
70+
### pkg/apis
71+
72+
**Users must edit packages under this package**
73+
74+
The `apis` package contains the schema definitions for the resource types you define. Resources are defined under
75+
`pkg/apis/<group>/<version>/<kind>_types.go`. Resources may be annotated with comments to identify resources
76+
to code generators. Comments are also used to generated field validation.
77+
78+
Notably client generation, CRD generation, docs generation and config generation are all driven off of annotations
79+
on resources package.
80+
81+
See documentation and examples of annotations in godoc format here:
82+
[gen/apis](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/apis#example-package)
83+
84+
Subpackages of apis are created when running the following command to create a new resource and controller:
85+
86+
```sh
87+
kubebuilder create resource --group mygroup --version v1beta1 --kind MyKind
88+
```
89+
90+
### pkg/controllers
91+
92+
**Users must packages under this package**
93+
94+
The `controllers` package contains the controllers to implement the resource APIs. Controllers are defined under
95+
`pkg/controllers/<kind>/controller.go`. Controllers may be annotated with comments to wire the controller into the
96+
inject package, start informers they require and install RBAC rules they require.
97+
98+
Subpackages of controllers are created when running the following command to create a new resource and controller:
99+
100+
```sh
101+
kubebuilder create resource --group mygroup --version v1beta1 --kind MyKind
102+
```
103+
104+
See documentation and examples of annotations in godoc format here:
105+
- [gen/controller](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/controller#example-package)
106+
107+
### pkg/inject
108+
109+
*Most users do not need to edit this package.*
110+
111+
The `inject` package contains the `RunAll` function used to start all of the registered controllers and informers.
112+
Wiring is autogenerated based on resource and controller annotations.
113+
114+
Generated wiring:
115+
- Installing CRDs
116+
- Instantiating and starting controllers
117+
- Starting sharedinformers
118+
- Installing RBAC rules
119+
120+
### pkg/inject/args
121+
122+
*Only some users need to edit this package.*
123+
124+
The `args` package contains the struct passed to the `ProvideController` function used to instantiating controllers.
125+
The `InjectArgs` struct and `CreateInjectArgs` function in this package may be edited to pass additional information
126+
to the controller provider functions. This is typically an advanced use case.
127+
128+
The `args` package uses the `kubebuilder/pkg/inject/args` package documented here:
129+
[inject/args](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/inject/args)
130+
131+
### hack/
132+
133+
The `hack` directory contains generated samples and config.
134+
135+
The hack/install.yaml config file for installing the API extension into a cluster will be created under this
136+
directory by running.
137+
138+
```sh
139+
kubebuilder create config --controller-image mycontrollerimage --name myextensionname
140+
```
141+
142+
### docs/
143+
144+
The `docs` package contains your examples and content for generating reference documentation for your APIs.
145+
146+
The docs package is created when running either
147+
148+
```sh
149+
kubebuilder docs
150+
```
151+
152+
or
153+
154+
```sh
155+
kubebuilder create example --group mygroup --version v1beta1 --kind MyKind
156+
```
157+
158+
Example reference documentation lives under `docs/reference/examples`. Conceptual reference documentation
159+
lives under `docs/reference/static_includes`.
160+
161+
### /
162+
163+
The project root directory contains several files
164+
165+
- Dockerfile.controller
166+
167+
Running `docker build` on this file will build a container image for running your controller.
168+
169+
- Gopkg.toml / Gopkg.lock
170+
171+
These files are used to update vendored go dependencies.
172+
173+
## Available controller operations
174+
175+
Controllers watch Kubernetes resources and call a "Reconcile" function in response to events. Reconcile functions
176+
are typically "level-based", that is they are notified that something has changed for given resource namespace/name key,
177+
but not specifically what changed (e.g. add, delete, update). This allows Reconcile functions to reconcile multiple
178+
events at a time and more easily self-heal, as actions are taken by comparing the current desired state (resource Spec)
179+
and the observed state of the system.
180+
181+
### Creating a new controller
182+
183+
A new GenericController is created for you by kubebuilder when creating a resource. If you are not using
184+
kubebuilder to create resources and manage your project, you may create a controller directly by following
185+
this example:
186+
187+
[GenericController](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController)
188+
189+
Use the following annotation to start additional informers for any resources you are watching in your controller.
190+
191+
```go
192+
// +informers:group=apps,version=v1,kind=Deployment
193+
```
194+
195+
Use the following annotation to generate RBAC rules to allow your controller to read and write resources when
196+
running in a container in a Kubernetes cluster.
197+
198+
```go
199+
// +rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
200+
```
201+
202+
203+
### Watching resources
204+
205+
Controllers watch resources to trigger reconcile functions. It is common for controllers to reconcile a single resource
206+
type, but watch many other resource types to trigger a reconcile. An example of this is a Deployment controller that
207+
watches Deployments, ReplicaSets (created by the controller) and Pods (created by the ReplicaSet controller). Pod
208+
status events, such as becoming healthy, may trigger actions in the Deployment controller, such as continuing
209+
a rolling update.
210+
211+
#### Watching the resource managed by the controller
212+
213+
Controllers typically watch for events on the resource they control and in response reconcile that resource instance.
214+
215+
Note: Kubebuilder will automatically call the Watch function for controllers it creates when creating a resources. If
216+
you are not using kubebuilder to create resources and manager your project, you may have your controller watch
217+
a resource by following this example:
218+
219+
[GenericController.Watch](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController-Watch)
220+
221+
#### Watching resources created by a controller and reconciling in the controller
222+
223+
Controllers frequently watch for events on the resources created by the controller or transitively created by the
224+
controller (e.g. controller creates a ReplicaSet and the ReplicaSet creates a Pod).
225+
226+
Events for created resources need to be mapped back to the resource instance responsible for their creation -
227+
e.g. if there is a Pod event -then reconcile the Deployment that owns the Pod. The owning resource is found by
228+
looking at the instance (Pod) controller reference, looking up the object with the same namespace and name as the
229+
owner reference and comparing the UID of the found object to the UID in the owner reference. This is check is done
230+
to ensure the object is infact the owner and to disambiguate multiple resources with the same name and Kind that
231+
are logically different (e.g. they are in different groups and are totally different things).
232+
233+
e.g. In response to a Pod event, find the owner reference for the Pod that has `controller=true`.
234+
Lookup the ReplicaSet with this name and compare its UID to the ownerref UID. If they are the same
235+
find the owner reference for the ReplicaSet that has `controller=true`. Lookup the Deployment
236+
with this name and compare the UID to the ownerref UID. If they are the same reconcile the Deployment with
237+
this namespace/name.
238+
239+
In order to watch objects created by a controller and reconcile the owning resource in response, the functions
240+
to lookup the ancestors object must be provided (e.g. lookup a ReplicaSet for a namespace/name,
241+
lookup a Deployment for a namespace/name).
242+
243+
You may have you controller watch a resource created by the controller and then reconcile the owning object by
244+
following this example:
245+
246+
[GenericController.WatchControllerOf](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController-WatchControllerOf)
247+
248+
[Sample](https://github.com/kubernetes-sigs/kubebuilder/blob/master/samples/controller/controller.go#L91)
249+
250+
#### Watching arbitrary resources and mapping them so the are reconciled in the controller
251+
252+
In some cases it may be necessary to watch resources not owned or created by your controller, but respond to them.
253+
An example would be taking some action in response to the deletion or creation of Nodes in the cluster. To do
254+
this, your controller must watch that object (Node) and map events to the resource type of the controller.
255+
256+
You may watch a resource and transform it into the key of the resource the controller manages by following this example:
257+
258+
[GenericController.WatchTransformationOf](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController-WatchTransformationOf)
259+
260+
#### Watching arbitrary resources and handling the events that may caused a reconciliation in the controller
261+
262+
In some cases it may be necessary to directly handle events and enqueue keys to be reconciled. You may do so
263+
by following this example:
264+
265+
[GenericController.WatchEvents](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController-WatchEvents)
266+
267+
### Watching a channel for resource names and reconciling them in the controller
268+
269+
In some cases it may be necessary to respond to external events such as webhooks. You may enqueue reconcile events
270+
from arbitrary sources by using a channel and following this example:
271+
272+
[GenericController.WatchChannel](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/controller#example-GenericController-WatchChannel)
273+
274+
## Running tests
275+
276+
In order to run the integration tests, the following environment variables must be set to bring up the test environment:
277+
278+
```sh
279+
export TEST_ASSET_KUBECTL=/usr/local/kubebuilder/bin/kubectl
280+
export TEST_ASSET_KUBE_APISERVER=/usr/local/kubebuilder/bin/kube-apiserver
281+
export TEST_ASSET_ETCD=/usr/local/kubebuilder/bin/etcd
282+
```
283+
284+
Tests can then be run with:
285+
286+
```sh
287+
go test ./pkg/...
288+
```
289+
290+
## Godoc Links
53291

54292
Many of the kubebuilder libraries can be used on their own without the kubebuilder code generation and scaffolding.
55293

@@ -64,7 +302,6 @@ Kubebuilder generates codes for custom resource fields, and controller component
64302
- [resource code generation tags](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/apis)
65303
- [controller code generation tags](https://godoc.org/github.com/kubernetes-sigs/kubebuilder/pkg/gen/controller)
66304

67-
68305
For example, you have to add controller code generation tags such as `+rbac` and `+informers` in `pkg/controller/foo/controller.go` file:
69306
```
70307
// +controller:group=foo,version=v1beta1,kind=Bar,resource=bars

0 commit comments

Comments
 (0)