Share
Embed
Controller: Extending your K8s cluster
Extending your Kubernetes clusters with CustomResourceDefinitions and Controllers.
Terin Stock
May 04, 2018
Transcript
44 slides
-
Keeping it Simple • Automate processes ◦ Make correct the
easiest • Abstract implementation ◦ Move the decision making elsewhere • Remove duplicate state ◦ Say it only once -
Kubernetes Out of the box We run all of our
own physical infrastructure. So, Kubernetes the Salt way is the only option -
• No cloud load balancer ◦ Leifur: a load balancer
for bare metal • Hardware scales much slower ◦ Pyli: automate user and namespace creation and RBAC provisioning • Existing telemetry ◦ Rule Loader: configure Prometheus from ConfigMaps Without a Cloud Provider... -
Controllers are: • Simple • Reliable • Event driven •
Easy to write Can’t believe it’s not Serverless! -
Example Problem We want a namespace for every developer on
the Kubernetes cluster. Possible Solutions: • Offload it to the IT department • Onboarding tasks the new hire does their first week • Write a standalone service -
Or, Writing a Controller We can write a controller which
maintains the relationship between a User and a Namespace -
4 Steps to Writing a Controller 1. Define your Custom
Resource Definition 2. Generate Client Code 3. Listen for events 4. Handle events in queue -
Creating a Custom Resource for Users apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition
metadata: name: users.example.com spec: group: example.com version: v1 scope: Cluster names: plural: users singular: user kind: User shortNames: - usr -
Validating Objects • Resources can be checked against OpenAPI v3
schema on admission -
pkg/apis/example.com/v1/types.go // +genclient // +genclient:noStatus // +k8s:deepcopy-gen=true // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type
User struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec UserSpec`json:"spec"` } // +k8s:deepcopy-gen=true type UserSpec struct { DisplayName string `json:"display_name"` } -
Installing the Generator Add the code generator to your Gopkg.toml
file: required = ["k8s.io/code-generator/cmd/client-gen"] $ dep ensure -
pkg/ └── apis └── example.com └── v1 ├── docs.go ├──
register.go └── types.go pkg/ ├── apis │ └── example.com │ └── v1 │ ├── docs.go │ ├── register.go │ ├── types.go │ └── zz_generated.deepcopy.go └── client ├── clientset │ └── [...] ├── informers │ └── [...] └── listers │ └── [...] -
Informers • React to the changes in resources • Reduce
the burden on API server • Populate read-only cache (Lister) • Prevents polling -
Work Queues Simple, Intelligent Workqueue: • Stingy • Fair •
Multiple Consumers and Producers -
What goes on the queue? 1. Queues use equivalent to
determine duplicate keys 2. The simpler the objects the better 3. Usually .metadata.name works well -
queue := workqueue.NewRateLimitingQueue() informers := informers.NewSharedInformerFactory( clientSet, time.Second * 30
) func enqueueUser(queue workqueue.Type, obj interface) { key, _ := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) queue.Add(key) } -
informers.Example().AddEventHandler( &cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) error { return enqueueUser(queue, obj)
}, UpdateFunc: func(_, obj interface{}) error { return enqueueUser(queue, obj) }, DeleteFunc: func(obj interface{}) error { return enqueueUser(queue, obj) }, }) -
• Update the child’s metadata.ownerReferences to reflect the relationship •
In our case we want to be notified if namespace we care about changes. Watching Children -
Worker go routine • Pops items off of the queue
and calls a work function until instructed to stop • Cannot block forever on one item Work Functions: • Handle Deletion • Idempotent -
func processWorkItem( queue workqueue.Delaying, workFn func(context.Context, string) error ) {
// get the item or signal to quit key, quit := q.Get() if quit { return false } defer q.Done(key) // Tell the queue we’re done processing this item ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() err := workFn(ctx, key.(string)) if err == nil { q.Forget(key) // Mark the work as successful return true } q.AddRateLimited(key) // Retry at a later time ) -
• Don’t panic, just return • Return errors but don’t
retry in your worker functions • Let the queue retry them Don’t handle Transient Errors -
Handling Deletion Do you have external state? Do you need
to guarantee you witness a deletion? -
Don’t Handle the OnDelete Differently Avoid duplicating & complicating your
code. Consider this a best-effort optimization opportunity for later. -
No? Use Kubernetes Garbage Collection “[The garbage collector will delete]
objects that once had an owner, but no longer have an owner.” There’s no code to write! Since we’ve already set up ownerReferences for notifications. -
Yes? Use Finalizers for deletion • Don’t rely on noticing
the deletion event • Use a finalizer to handle deletions -
How do finalizers work? When you delete a resources with
Finalizers Kubernetes will wait until all existing Finalizers are removed then finally delete the resources. -
On resource deletion, Kubernetes waits for each Finalizer to complete
before removing the resource. -
func syncUser( key string, client exampleclient.ClientSet, userLister listers.UserLister, k8sClient kuberentes.ClientSet,
nsLister lister.NamespaceLister ) error { // Get the User from the cache cached, _ := userLister.Get(key) if cached.DeletionTimestamp.IsZero() && apiextensions.CRDHasFinalizer(cachedCRD, "example.com") { // HANDLE DELETE // Remove example.com from Finalizer list } // HANDLE UPDATE/CREATE }