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

Navigating Kubernetes Custom Resources using co...

Navigating Kubernetes Custom Resources using code generation

Marta Paciorkowska

December 04, 2019
Tweet

More Decks by Marta Paciorkowska

Other Decks in Technology

Transcript

  1. Access to native K8s API Pretty good performance Statically typed

    Have access to error types, etc. Compiled to machine code. Comparably short compilation time. Control over data structure gives safety.
  2. K8S HAS SOME BUILT-IN TYPES… Horizontal Pod Autoscaler Automatically scales

    the number of Pods based on observed resource utilization. Deployment Ingress Defines HTTPS endpoints for your Services. A set of identical Pods that get automatically replaced on failure. Service A load balancer for a group of pods.
  3. …AND YOU CAN DEFINE YOUR OWN A Custom Resource is

    an extension of the API. A custom controller treats such a resource as desired state, and maintains it. Examples: Postgres operator, fiaas-deploy-daemon.
  4. WALKTHROUGH 03 We’ll create a Custom Resource and a basic

    custom controller, using fiaas-deploy-daemon as inspiration.
  5. Define types & comments Run generation script Write controller Set

    up your project STEP 2 STEP 3 STEP 4 STEP 1 PROCESS
  6. Define types & comments Run generation script Write controller Set

    up your project STEP 2 STEP 3 STEP 4 STEP 1 PROCESS
  7. Define types & comments Run generation script Write controller Set

    up your project STEP 2 STEP 3 STEP 4 STEP 1 PROCESS
  8. TYPE/DATA STRUCTURE Our example: a simple Application Custom Resource that

    can help us abstract four built-in types mentioned earlier: 1. Application The top level type. It has an… 2. ApplicationSpec This struct has only two fields: Image and Name.
  9. A NOTE ON MAGIC COMMENTS Let you control what will

    be generated. Note that the full list is hard to find. One person aggregated a list of code gen tags. What we’ll use today: // +genclient - generate all client verb functions // +genclient:nonNamespaced - generate without namespace // +groupName=gooslo.io – used in the fake client as the full group name, defines the fully qualified API group name. // +k8s:deepcopy-gen:interfaces - used in cases where you define API types that have fields of some interface type.
  10. Define types & comments Run generation script Write controller Set

    up your project STEP 2 STEP 3 STEP 4 STEP 1 PROCESS
  11. DEEPCOPY FUNCTIONS AND INTERFACES Custom Resources in Go have to

    implement the runtime.Object interface. This in done using deep copy functions, generated by the deepcopy-gen package.
  12. LISTERS list and get your custom resource CLIENTSET your master

    key INFORMERS one way of caching for processing AVAILABLE COMPONENTS
  13. LISTERS list and get your custom resource CLIENTSET your master

    key INFORMERS one way of caching for processing AVAILABLE COMPONENTS
  14. Resources are members of API groups (core, extensions). Groups have

    versions (core/v1, extensions/v1beta) Clients for each versioned group are collected in a clientset. HOW DO CLIENTSETS WORK?
  15. LISTERS list and get your custom resource CLIENTSET your master

    key INFORMERS one way of caching for processing AVAILABLE COMPONENTS
  16. Define types & comments Run generation script Write controller Set

    up your project STEP 2 STEP 3 STEP 4 STEP 1 PROCESS
  17. USING OUR NEW CRD CLIENTSET The controller uses an infinite

    loop that watches for changes (events) to our Application custom resource. It then prints out the type of event, name of the application, and what Docker image it should run.
  18. HOW DO WE TEST THE CODE? Our generated code has

    a fake client that can be used to mock the real objects. From Naiserator: appClient := nais_fake.NewSimpleClientset() app, err := appClient. NaiseratorV1alpha1(). Applications(namespace). Create(app) … persistedApp, err := appClient. NaiseratorV1alpha1(). Applications(namespace). Get(name, metav1.GetOptions{})
  19. THANK YOU! Does anyone have any questions? This code lives

    at: github.com/xamebax/kubernetes-crd-demo Contact me: mpaciorkowska @ Go slack a_meba @ Twitter
  20. CREDITS ◂ Presentation template by Slidesgo ◂ Icons by Flaticon

    ◂ Infographics by Freepik ◂ Author introduction slide photo created by Freepik ◂ Text & Image slide photo created by Freepik.com