• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H13-Mar-2020-32

Godeps/H13-Mar-2020-445440

artifacts/examples/H13-Mar-2020-5551

docs/H13-Mar-2020-6448

hack/H03-May-2022-10838

pkg/H13-Mar-2020-1,852920

CONTRIBUTING.mdH A D13-Mar-2020756 84

LICENSEH A D13-Mar-202011.1 KiB203169

OWNERSH A D13-Mar-2020159 1311

README.mdH A D13-Mar-20207 KiB188127

SECURITY_CONTACTSH A D13-Mar-2020563 1816

code-of-conduct.mdH A D13-Mar-2020148 42

controller.goH A D13-Mar-202015.6 KiB424257

controller_test.goH A D13-Mar-20209.2 KiB314233

go.modH A D13-Mar-2020510 2217

go.sumH A D13-Mar-202023.8 KiB249248

main.goH A D13-Mar-20202.7 KiB8347

README.md

1# sample-controller
2
3This repository implements a simple controller for watching Foo resources as
4defined with a CustomResourceDefinition (CRD).
5
6**Note:** go-get or vendor this package as `k8s.io/sample-controller`.
7
8This particular example demonstrates how to perform basic operations such as:
9
10* How to register a new custom resource (custom resource type) of type `Foo` using a CustomResourceDefinition.
11* How to create/get/list instances of your new resource type `Foo`.
12* How to setup a controller on resource handling create/update/delete events.
13
14It makes use of the generators in [k8s.io/code-generator](https://github.com/kubernetes/code-generator)
15to generate a typed client, informers, listers and deep-copy functions. You can
16do this yourself using the `./hack/update-codegen.sh` script.
17
18The `update-codegen` script will automatically generate the following files &
19directories:
20
21* `pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go`
22* `pkg/generated/`
23
24Changes should not be made to these files manually, and when creating your own
25controller based off of this implementation you should not copy these files and
26instead run the `update-codegen` script to generate your own.
27
28## Details
29
30The sample controller uses [client-go library](https://github.com/kubernetes/client-go/tree/master/tools/cache) extensively.
31The details of interaction points of the sample controller with various mechanisms from this library are
32explained [here](docs/controller-client-go.md).
33
34## Fetch sample-controller and its dependencies
35
36Like the rest of Kubernetes, sample-controller has used
37[godep](https://github.com/tools/godep) and `$GOPATH` for years and is
38now adopting go 1.11 modules.  There are thus two alternative ways to
39go about fetching this demo and its dependencies.
40
41### Fetch with godep
42
43When NOT using go 1.11 modules, you can use the following commands.
44
45```sh
46go get -d k8s.io/sample-controller
47cd $GOPATH/src/k8s.io/sample-controller
48godep restore
49```
50
51### When using go 1.11 modules
52
53When using go 1.11 modules (`GO111MODULE=on`), issue the following
54commands --- starting in whatever working directory you like.
55
56```sh
57git clone https://github.com/kubernetes/sample-controller.git
58cd sample-controller
59```
60
61Note, however, that if you intend to
62[generate code](#changes-to-the-types) then you will also need the
63code-generator repo to exist in an old-style location.  One easy way
64to do this is to use the command `go mod vendor` to create and
65populate the `vendor` directory.
66
67### A Note on kubernetes/kubernetes
68
69If you are developing Kubernetes according to
70https://github.com/kubernetes/community/blob/master/contributors/guide/github-workflow.md
71then you already have a copy of this demo in
72`kubernetes/staging/src/k8s.io/sample-controller` and its dependencies
73--- including the code generator --- are in usable locations
74(valid for all Go versions).
75
76## Purpose
77
78This is an example of how to build a kube-like controller with a single type.
79
80## Running
81
82**Prerequisite**: Since the sample-controller uses `apps/v1` deployments, the Kubernetes cluster version should be greater than 1.9.
83
84```sh
85# assumes you have a working kubeconfig, not required if operating in-cluster
86go build -o sample-controller .
87./sample-controller -kubeconfig=$HOME/.kube/config
88
89# create a CustomResourceDefinition
90kubectl create -f artifacts/examples/crd.yaml
91
92# create a custom resource of type Foo
93kubectl create -f artifacts/examples/example-foo.yaml
94
95# check deployments created through the custom resource
96kubectl get deployments
97```
98
99## Use Cases
100
101CustomResourceDefinitions can be used to implement custom resource types for your Kubernetes cluster.
102These act like most other Resources in Kubernetes, and may be `kubectl apply`'d, etc.
103
104Some example use cases:
105
106* Provisioning/Management of external datastores/databases (eg. CloudSQL/RDS instances)
107* Higher level abstractions around Kubernetes primitives (eg. a single Resource to define an etcd cluster, backed by a Service and a ReplicationController)
108
109## Defining types
110
111Each instance of your custom resource has an attached Spec, which should be defined via a `struct{}` to provide data format validation.
112In practice, this Spec is arbitrary key-value data that specifies the configuration/behavior of your Resource.
113
114For example, if you were implementing a custom resource for a Database, you might provide a DatabaseSpec like the following:
115
116``` go
117type DatabaseSpec struct {
118	Databases []string `json:"databases"`
119	Users     []User   `json:"users"`
120	Version   string   `json:"version"`
121}
122
123type User struct {
124	Name     string `json:"name"`
125	Password string `json:"password"`
126}
127```
128
129## Validation
130
131To validate custom resources, use the [`CustomResourceValidation`](https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/#validation) feature.
132
133This feature is beta and enabled by default in v1.9.
134
135### Example
136
137The schema in [`crd-validation.yaml`](./artifacts/examples/crd-validation.yaml) applies the following validation on the custom resource:
138`spec.replicas` must be an integer and must have a minimum value of 1 and a maximum value of 10.
139
140In the above steps, use `crd-validation.yaml` to create the CRD:
141
142```sh
143# create a CustomResourceDefinition supporting validation
144kubectl create -f artifacts/examples/crd-validation.yaml
145```
146
147## Subresources
148
149Custom Resources support `/status` and `/scale` subresources as a [beta feature](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#subresources) in v1.11 and is enabled by default.
150This feature is [alpha](https://v1-10.docs.kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/#subresources) in v1.10 and to enable it you need to set the `CustomResourceSubresources` feature gate on the [kube-apiserver](https://kubernetes.io/docs/admin/kube-apiserver):
151
152```sh
153--feature-gates=CustomResourceSubresources=true
154```
155
156### Example
157
158The CRD in [`crd-status-subresource.yaml`](./artifacts/examples/crd-status-subresource.yaml) enables the `/status` subresource
159for custom resources.
160This means that [`UpdateStatus`](./controller.go#L330) can be used by the controller to update only the status part of the custom resource.
161
162To understand why only the status part of the custom resource should be updated, please refer to the [Kubernetes API conventions](https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status).
163
164In the above steps, use `crd-status-subresource.yaml` to create the CRD:
165
166```sh
167# create a CustomResourceDefinition supporting the status subresource
168kubectl create -f artifacts/examples/crd-status-subresource.yaml
169```
170
171## Cleanup
172
173You can clean up the created CustomResourceDefinition with:
174
175    kubectl delete crd foos.samplecontroller.k8s.io
176
177## Compatibility
178
179HEAD of this repository will match HEAD of k8s.io/apimachinery and
180k8s.io/client-go.
181
182## Where does it come from?
183
184`sample-controller` is synced from
185https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/sample-controller.
186Code changes are made in that location, merged into k8s.io/kubernetes and
187later synced here.
188