Keyboard shortcuts

/ or ⌘/Ctrl K
Find a note
j / k
Next / previous section or linked note
h / l
Collapse or go to parent / expand or enter
e or Alt-click
Read a linked note here
o
Open focused note on its own
g g / G
First / last section or linked note
g h / g a
Home / all notes
g b / g t
Backlinks / table of contents
t
Cycle System, Light, Dark
? / Esc
Show / close this reference

Search: ↑/↓ or Ctrl N/P, Enter to open. Shortcuts pause while typing.

programming kubernetes chapter 3 - client-go [c45d33e0]

Tags: hausenblas - programming kubernetes

  // basic client

  import (
      metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
      "k8s.io/client-go/tools/clientcmd"
      "k8s.io/client-go/kubernetes"
  )

  kubeconfig = flag.String("kubeconfig, "~/.kube/config", "kubeconfig file")
  flag.Parse()
  config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  clientset, err := kubernetes.NewForConfig(config)

  pod, err := clientset.CoreV1().Pods("book").Get("example", metav1.GetOptions{})

This parses the appropriate kubeconfig and then gets the pods using the GetOptions

If you run a binary inside of a pod inside a cluster, kubelet automatically mounts a sservice account into /var/run/secrets/kubernetes.io/serviceaccount

CRUD operations are run through the XOptions, such as CreateOptions or DeleteOptions. ALl of htem are part of ObjectMeta

  • alpha/beta/vN verisons are what you would expect (alpha changes, beta is unlikely to break, vN is stable)
  • each object stored in etcd is stored as a specific version known as the storage version of that resource
  • anything served as a kind is a struct

    • all objects can be retrived and deepcopied
    • each object stores its groupversionkind

1. type meta

example of a pod declaration

  type Pod struct {
      metav1.TypeMeta `json:",inline"`

      metav1.ObjectMeta `json:"metadata,omitempty"`

      Spec PodSpec `json:"spec,omitempty"`

      Status PodStatus `json:"status,omitempty"`
  }

Note that the type and version are not set by the user, they are set when being marshaled in the versionserializer

2. ObjectMeta

  • corresponds to the metadata section in yaml
  • rarely read or written from in client-go code, but very key to the system

3. Client Sets

  • gives access to multiple API groups and resources
  • also gives access to the discovery client

4. Status subresources

  • each deployment has a status subresource, updatestatus uses an additional http endpoint with /status
  • used with /apis/apps/v1beta1/namespaces/ns/deployments/name/status can change the status of an object

5. Listing and Deletions

  • ALlows to delete multiple objects of a namespace at once

6. Watches

  • Gives an event interface for all changes, returns a channel

7. Client Options

  • can set:

    • the version
    • wire format
    • user agent (allows for debugging and better log readability)

8. informers (k8s) and caching

9. work queue

10. api machinery (k8s)

11. vendoring

  • typical golang vendoring, or modules