b8b76143ce
Automatic merge from submit-queue (batch tested with PRs 59158, 38320, 59059, 55516, 59357). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Promote v1alpha1 meta to v1beta1 No code changes, just renames. We can discuss if there are any field / naming changes here or in a follow-up Parent #58536 Fixes #53224 Prereq to #55637 @kubernetes/sig-api-machinery-pr-reviews @deads2k ```release-note The `meta.k8s.io/v1alpha1` objects for retrieving tabular responses from the server (`Table`) or fetching just the `ObjectMeta` for an object (as `PartialObjectMetadata`) are now beta as part of `meta.k8s.io/v1beta1`. Clients may request alternate representations of normal Kubernetes objects by passing an `Accept` header like `application/json;as=Table;g=meta.k8s.io;v=v1beta1` or `application/json;as=PartialObjectMetadata;g=meta.k8s.io;v1=v1beta1`. Older servers will ignore this representation or return an error if it is not available. Clients may request fallback to the normal object by adding a non-qualified mime-type to their `Accept` header like `application/json` - the server will then respond with either the alternate representation if it is supported or the fallback mime-type which is the normal object response. ``` Kubernetes-commit: 9ee71b720ed2300d6298bb936d0a7873b5ecf2ac |
||
---|---|---|
.github | ||
artifacts/examples | ||
Godeps | ||
hack | ||
pkg | ||
vendor | ||
BUILD | ||
code-of-conduct.md | ||
CONTRIBUTING.md | ||
controller.go | ||
LICENSE | ||
main.go | ||
OWNERS | ||
README.md |
sample-controller
This repository implements a simple controller for watching Foo resources as defined with a CustomResourceDefinition (CRD).
This particular example demonstrates how to perform basic operations such as:
- How to register a new custom resource (custom resource type) of type
Foo
using a CustomResourceDefinition. - How to create/get/list instances of your new resource type
Foo
. - How to setup a controller on resource handling create/update/delete events.
It makes use of the generators in k8s.io/code-generator
to generate a typed client, informers, listers and deep-copy functions. You can
do this yourself using the ./hack/update-codegen.sh
script.
The update-codegen
script will automatically generate the following files &
directories:
pkg/apis/samplecontroller/v1alpha1/zz_generated.deepcopy.go
pkg/client/
Changes should not be made to these files manually, and when creating your own
controller based off of this implementation you should not copy these files and
instead run the update-codegen
script to generate your own.
Purpose
This is an example of how to build a kube-like controller with a single type.
Running
Prerequisite: Since the sample-controller uses apps/v1
deployments, the Kubernetes cluster version should be greater than 1.9.
# assumes you have a working kubeconfig, not required if operating in-cluster
$ go run *.go -kubeconfig=$HOME/.kube/config
# create a CustomResourceDefinition
$ kubectl create -f artifacts/examples/crd.yaml
# create a custom resource of type Foo
$ kubectl create -f artifacts/examples/example-foo.yaml
# check deployments created through the custom resource
$ kubectl get deployments
Use Cases
CustomResourceDefinitions can be used to implement custom resource types for your Kubernetes cluster.
These act like most other Resources in Kubernetes, and may be kubectl apply
'd, etc.
Some example use cases:
- Provisioning/Management of external datastores/databases (eg. CloudSQL/RDS instances)
- Higher level abstractions around Kubernetes primitives (eg. a single Resource to define an etcd cluster, backed by a Service and a ReplicationController)
Defining types
Each instance of your custom resource has an attached Spec, which should be defined via a struct{}
to provide data format validation.
In practice, this Spec is arbitrary key-value data that specifies the configuration/behavior of your Resource.
For example, if you were implementing a custom resource for a Database, you might provide a DatabaseSpec like the following:
type DatabaseSpec struct {
Databases []string `json:"databases"`
Users []User `json:"users"`
Version string `json:"version"`
}
type User struct {
Name string `json:"name"`
Password string `json:"password"`
}
Validation
To validate custom resources, use the CustomResourceValidation
feature.
This feature is beta and enabled by default in v1.9. If you are using v1.8, enable the feature using
the CustomResourceValidation
feature gate on the kube-apiserver:
--feature-gates=CustomResourceValidation=true
Example
The schema in the example CRD applies the following validation on the custom resource:
spec.replicas
must be an integer and must have a minimum value of 1 and a maximum value of 10.
Cleanup
You can clean up the created CustomResourceDefinition with:
$ kubectl delete crd foos.samplecontroller.k8s.io
Compatibility
HEAD of this repository will match HEAD of k8s.io/apimachinery and k8s.io/client-go.
Where does it come from?
sample-controller
is synced from
https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/sample-controller.
Code changes are made in that location, merged into k8s.io/kubernetes and
later synced here.