Slide 1

Slide 1 text

Extending the Kubernetes API Thanks to Ashley McNamara for the wonderful artwork!

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

We are going to build our own objects! @TheNikhita

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

CustomResourceDefinition @TheNikhita

Slide 15

Slide 15 text

CustomResource @TheNikhita

Slide 16

Slide 16 text

DEMO TIME! @TheNikhita

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Thanks for coming!!!!