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.

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!

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  13. Step 1: Tell Kubernetes about our object
    CustomResourceDefinition
    Step 2: Create the custom object
    CustomResource
    @TheNikhita

    View Slide

  14. CustomResourceDefinition
    @TheNikhita

    View Slide

  15. CustomResource
    @TheNikhita

    View Slide

  16. DEMO TIME!
    @TheNikhita

    View Slide

  17. What now?
    Let’s create a
    CONTROLLER
    (yes, you’ll have to write some code)
    @TheNikhita

    View Slide

  18. CONTROLLER
    ● Active reconciliation process
    Current state Desired state
    Status Spec
    @TheNikhita

    View Slide

  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

    View Slide

  20. Kubernetes API structure
    Internal version External version(s)
    @TheNikhita

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  27. Thanks for coming!!!!

    View Slide