lushenle / sample-controller

Repository for sample controller. Complements sample-apiserver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sample-controller

This repository implements a simple controller for watching App resources as defined with a CustomResourceDefinition (CRD).

Note: go-get or vendor this package as github.com/lushenle/sample-controller.

This particular example demonstrates how to perform basic operations such as:

  • How to register a new custom resource (custom resource type) of type App using a CustomResourceDefinition.
  • How to create/get/list instances of your new resource type App.
  • 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/generated/

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.

Details

The sample controller uses client-go library extensively. The details of interaction points of the sample controller with various mechanisms from this library are explained here.

Fetch sample-controller and its dependencies

Like the rest of Kubernetes, sample-controller has used godep and $GOPATH for years and is now adopting go 1.11 modules. There are thus two alternative ways to go about fetching this demo and its dependencies.

Fetch with godep

When NOT using go 1.11 modules, you can use the following commands.

go get -d github.com/lushenle/sample-controller
cd $GOPATH/src/github.com/lushenle/sample-controller
godep restore

When using go 1.11 modules

When using go 1.11 modules (GO111MODULE=on), issue the following commands --- starting in whatever working directory you like.

git clone https://github.com/kubernetes/sample-controller.git
cd sample-controller

Note, however, that if you intend to generate code then you will also need the code-generator repo to exist in an old-style location. One easy way to do this is to use the command go mod vendor to create and populate the vendor directory.

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 build -o sample-controller .
./sample-controller

# create a CustomResourceDefinition
controller-gen crd paths=./... output:crd:dir=artifacts/examples
kubectl create -f artifacts/examples/samplecontroller.shenle.lu_apps.yaml

# create a custom resource of type App
kubectl create -f artifacts/examples/example.yaml

# check deployments created through the custom resource
kubectl get deployments

About

Repository for sample controller. Complements sample-apiserver

License:Apache License 2.0


Languages

Language:Go 87.9%Language:Shell 9.1%Language:Dockerfile 3.0%