Extending the Kubernetes API

Extending the Kubernetes API

How to extend the Kubernetes API using CustomResourceDefinitions and CustomResources. Also, how to write a custom controller to operate on these CustomResources.

Presented as a talk at Kubernetes Pune meetup.

Bb8ed71cd77d9da995685ca872b303e7?s=128

Nikhita Raghunath

September 09, 2017
Tweet

Transcript

  1. 2.

    Who am I? • Hi, I am Nikhita Raghunath! •

    Google Summer of Code student for CNCF (Kubernetes) • Undergrad student from VJTI, Mumbai You can find me on: ➔ Twitter: @TheNikhita ➔ Github: nikhita ➔ Website: https://www.nikhita.github.io @TheNikhita
  2. 6.

    What comes to your mind when you think of Kubernetes?

    Pods Deployments Services @TheNikhita
  3. 7.

    What comes to your mind when you think of Kubernetes?

    Pods Deployments Services StatefulSets DaemonSet CronJob ConfigMap Secrets Node PersistentVolumeClaim @TheNikhita
  4. 8.

    What comes to your mind when you think of Kubernetes?

    Pods Deployments Services StatefulSets DaemonSet CronJob ConfigMap Secrets Node PersistentVolumeClaim @TheNikhita
  5. 12.

    We are going to build our own custom resources! That’s

    cool. But...why? Operator pattern: encode domain knowledge for specific applications as an extension of Kubernetes API. @TheNikhita
  6. 13.

    Step 1: Tell Kubernetes about our object CustomResourceDefinition Step 2:

    Create the custom object CustomResource @TheNikhita
  7. 19.

    What will our controller do? 1. Try to make the

    current state same as the desired state. 2. Do something extra on add, update, delete of a resource. Example: - Write a message in a CustomResource. - Create the CustomResource. - A Github comment is created with this message. Link: https://github.com/nikhita/kube-custom-controller (inspired from https://github.com/munnerz/k8s-api-pager-demo) @TheNikhita
  8. 21.

    Step 1: Create internal types Inside pkg/apis/github - doc.go -

    register.go - types.go Step 2: Create external types Inside pkg/apis/github/v1 - doc.go - register.go - types.go Step 3: Install your API group Inside pkg/apis/github/install - install.go @TheNikhita
  9. 22.

    Step 4: GENERATE CODE! Use https://github.com/kubernetes/code-generator 1. client-gen 2. conversion-gen

    3. deepcopy-gen 4. defaulter-gen 5. informer-gen 6. lister-gen @TheNikhita
  10. 23.

    Step 5: Create the controller 1. Create clients: - kubernetes

    client => kubeconfig file - github client => github API token 2. Create an informer: Use sharedInformers: - notify or “inform” about add/update/delete of a resource - use a common cache across all controllers @TheNikhita
  11. 24.

    Step 5: Create the controller 3. Add event handler to

    the informer: - to tell what happens on add/update/delete - here, we will put it into a queue 4. Start the informer - wait for the informer cache sync across all resources @TheNikhita
  12. 25.

    Step 5: Create the controller 5. Take the object from

    the queue: - take the object from the queue - get it’s latest version - sync it! 6. Sync it! (current state -> desired state): - do some action. Here, we send a Github comment. - update the status (the current state) - update the resource @TheNikhita
  13. 26.

    That’s it! We learnt how to: - Create a CustomResourceDefinition

    - Create a CustomResource - Write your own controller to use the CustomResource! If you have even more specific needs, you should use Aggregated API Servers....but we won’t be discussing that today. @TheNikhita