Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

More Decks by Nikhita Raghunath

Other Decks in Programming

Transcript

  1. Extending the Kubernetes API Thanks to Ashley McNamara for the

    wonderful artwork!
  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
  3. What comes to your mind when you think of Kubernetes?

    @TheNikhita
  4. What comes to your mind when you think of Kubernetes?

    Pods @TheNikhita
  5. What comes to your mind when you think of Kubernetes?

    Pods Deployments @TheNikhita
  6. What comes to your mind when you think of Kubernetes?

    Pods Deployments Services @TheNikhita
  7. What comes to your mind when you think of Kubernetes?

    Pods Deployments Services StatefulSets DaemonSet CronJob ConfigMap Secrets Node PersistentVolumeClaim @TheNikhita
  8. What comes to your mind when you think of Kubernetes?

    Pods Deployments Services StatefulSets DaemonSet CronJob ConfigMap Secrets Node PersistentVolumeClaim @TheNikhita
  9. We are going to build our own objects! @TheNikhita

  10. We are going to build our own objects! custom @TheNikhita

  11. We are going to build our own custom resources! @TheNikhita

  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
  13. Step 1: Tell Kubernetes about our object CustomResourceDefinition Step 2:

    Create the custom object CustomResource @TheNikhita
  14. CustomResourceDefinition @TheNikhita

  15. CustomResource @TheNikhita

  16. DEMO TIME! @TheNikhita

  17. What now? Let’s create a CONTROLLER (yes, you’ll have to

    write some code) @TheNikhita
  18. CONTROLLER • Active reconciliation process Current state Desired state Status

    Spec @TheNikhita
  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
  20. Kubernetes API structure Internal version External version(s) @TheNikhita

  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
  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
  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
  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
  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
  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
  27. Thanks for coming!!!!