Go to file
Kubernetes Publisher 112f77b55e Merge pull request #54660 from munnerz/namespaced-informer-factory
Automatic merge from submit-queue (batch tested with PRs 55403, 54660, 55165). 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>.

code-generator: add NewFilteredSharedInformerFactory function

**What this PR does / why we need it**:

Adds a `namespace` option to the SharedInformerFactory constructor. This is useful when building controllers that may need to scope themselves to a namespace due to RBAC constraints.

Workarounds for this involve losing type safety if a user wants to use it for core APIs as well as a SharedInformerFactory type interface, as we have to deal with plain SharedIndexInformers (example here: https://github.com/jetstack-experimental/cert-manager/blob/master/pkg/util/kube/factory.go)

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

Fixes https://github.com/kubernetes/code-generator/issues/9

**Special notes for your reviewer**:

This will require updating all uses of SharedInformerFactory throughout the codebase. I'm going to follow up with later commits in this PR with these changes, but wanted to get this here to get some feedback on the way it's implemented.

**Release note**:

```release-note
NONE
```

/cc @sttts @nikhita @deads2k

Kubernetes-commit: bab312dbcf2ea68c19bffa9f26362c86ef1987ec
2017-12-07 05:03:46 +00:00
artifacts/examples Add sample CustomResourceDefinition controller 2017-10-09 19:19:34 +01:00
Godeps Merge pull request #54660 from munnerz/namespaced-informer-factory 2017-12-07 05:03:46 +00:00
hack Add sample CustomResourceDefinition controller 2017-10-09 19:19:34 +01:00
pkg run hack/update-bazel.sh 2017-11-01 15:10:35 +00:00
vendor Merge pull request #54786 from shiywang/bump-protobuf 2017-12-07 05:03:46 +00:00
BUILD Add sample CustomResourceDefinition controller 2017-10-09 19:19:34 +01:00
controller.go Add sample CustomResourceDefinition controller 2017-10-09 19:19:34 +01:00
LICENSE Remove README and LICENSE 2017-10-23 16:56:35 +05:30
main.go Add sample CustomResourceDefinition controller 2017-10-09 19:19:34 +01:00
OWNERS Add sample CustomResourceDefinition controller 2017-10-09 19:19:34 +01:00
README.md sample-controller: add usage instructions to README 2017-10-22 15:05:37 +05:30

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

# 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"`
}

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.