عجفت الغور

programming kubernetes chapter 3 - client-go

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

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

ObjectMeta

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

Client Sets

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

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

Listing and Deletions

  • ALlows to delete multiple objects of a namespace at once

Watches

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

Client Options

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

informers (k8s) and caching

work queue

api machinery (k8s)

vendoring

  • typical golang vendoring, or modules