Skip to content

Commit e1a1902

Browse files
author
Shawn Hurley
authored
doc/user-guide: Adding add to scheme documentation (#509)
Adding documentation for both operator-sdk and controller runtime on how to add a resource to the scheme.
1 parent 1ce6eca commit e1a1902

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

doc/user-guide.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,44 @@ $ kubectl delete -f deploy/cr.yaml
265265
$ kubectl delete -f deploy/operator.yaml
266266
```
267267

268+
269+
## Advanced Topics
270+
### Adding 3rd Party Resources To Your Operator
271+
To add a resource to an operator, you must add it to a scheme. By creating an `AddToScheme` method or reusing one you can easily add a resource to your scheme. An [example][deployments_register] shows that you define a function and then use the [runtime][runtime_package] package to create a `SchemeBuilder`
272+
273+
#### Current Operator-SDK
274+
You then need to tell the operators to use these functions to add the resources to its scheme. In operator-sdk you use [AddToSDKScheme][osdk_add_to_scheme] to add this.
275+
Example of you main.go:
276+
```go
277+
import (
278+
....
279+
appsv1 "k8s.io/api/apps/v1"
280+
)
281+
282+
func main() {
283+
k8sutil.AddToSDKScheme(appsv1.AddToScheme)`
284+
sdk.Watch(appsv1.SchemeGroupVersion.String(), "Deployments", <namespace>, <resyncPeriod>)
285+
}
286+
```
287+
288+
#### Future with Controller Runtime
289+
When using controller runtime, you will also need to tell its scheme about your resourece. In controller runtime to add to the scheme, you can get the managers [scheme][manager_scheme]. If you would like to see what kubebuilder generates to add the resoureces to the [scheme][simple_resource].
290+
Example:
291+
```go
292+
import (
293+
....
294+
appsv1 "k8s.io/api/apps/v1"
295+
)
296+
297+
func main() {
298+
....
299+
if err := appsv1.AddToScheme(mgr.GetScheme()); err != nil {
300+
log.Fatal(err)
301+
}
302+
....
303+
}
304+
```
305+
268306
[memcached_handler]: ../example/memcached-operator/handler.go.tmpl
269307
[layout_doc]:./project_layout.md
270308
[dep_tool]:https://golang.github.io/dep/docs/installation.html
@@ -273,3 +311,8 @@ $ kubectl delete -f deploy/operator.yaml
273311
[docker_tool]:https://docs.docker.com/install/
274312
[kubectl_tool]:https://kubernetes.io/docs/tasks/tools/install-kubectl/
275313
[minikube_tool]:https://github.com/kubernetes/minikube#installation
314+
[manager_scheme]: https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/manager/manager.go#L61
315+
[simple_resource]: https://book.kubebuilder.io/basics/simple_resource.html
316+
[deployments_register]: https://github.com/kubernetes/api/blob/master/apps/v1/register.go#L41
317+
[runtime_package]: https://godoc.org/k8s.io/apimachinery/pkg/runtime
318+
[osdk_add_to_scheme]: https://github.com/operator-framework/operator-sdk/blob/4179b6ac459b2b0cb04ab3a1b438c280bd28d1a5/pkg/util/k8sutil/k8sutil.go#L67

0 commit comments

Comments
 (0)