Introduction to Kubernetes client go

introduce

Since August 2016, kubernetes officials have extracted the core source code related to kubernetes resource operation and created an independent project client Go. Kubernetes uses client Go as the official programming interactive client library of Go language to provide interactive access to api server services. For the secondary development of k8s, it is very necessary to master client Go.

Source address: https://hub.fastgit.org/kubernetes/client-go.git

Project directory resolution

.
├── applyconfigurations
├── CHANGELOG.md
├── code-of-conduct.md
├── CONTRIBUTING.md
├── discovery  # Service discovery through Kubernetes API
├── dynamic  # A dynamic client that performs common operations on Kubernetes objects
├── examples
├── go.mod
├── go.sum
├── informers  # A very powerful interactive way is to put the events of the reflector watch resource into the queue (DeltaFIFO), and distribute the events to OnAdd/OnUpdate/OnDelete in the specific ResourceEventHandler through the pendingNotifications(buffer.RingGrowing) of the sharedProcessor This information will be described in detail later
├── INSTALL.md
├── kubernetes # Provide ClientSet client
├── kubernetes_test
├── LICENSE
├── listers # Provide Lister function for each K8S resource, which provides read-only cache data for Get and List requests
├── metadata
├── OWNERS
├── pkg
├── plugin # Provide authorized plug-ins of cloud service providers such as OpenStack, GCP and Azure
├── README.md
├── rest  # Provide a RESTClient client to perform RESTful operations on the K8S API Server
├── restmapper
├── scale # ScaleClient client is provided to expand or shrink resource objects such as deployment, replicate, and replication controller
├── SECURITY_CONTACTS
├── testing
├── third_party
├── tools # The following / clientcmd provides some basic tools for creating clients
├── transport # Provide secure TCP connection and support HTTP Stream. Some operations need to transfer binary streams between the client and the container, such as exec, attach and so on
└── util # Provide common methods. For example, WorkQueue, work queue, Certificate, Certificate management, etc

19 directories, 10 files

Client type

  1. RESTClient: RESTClient is the most basic and equivalent to the underlying infrastructure. It can interact directly through RESTful methods provided by RESTClient, such as Get(), Put(), Post(), Delete()
    • Support both Jason and protobuf
    • Support for all native resources and CRDs
    • However, generally speaking, for more elegant processing, it is necessary to further encapsulate RESTClient through clientset, and then provide external interfaces and services;
  2. Clientset: clientset is the most commonly used client for calling Kubernetes resource objects. It can operate all resource objects. It is implemented based on RESTClient.
    • When accessing a Resource, you need to specify its Group, Version and Resource;
    • The elegant pose is to use a controller object, plus Informer;
  3. Dynamic client: dynamic client is a dynamic client that can handle all kubernetes resources. Unlike Clientset, the object returned by dynamic client is a map[string]interface {}.
    • If you need to control all API s in a controller, you can use dynamic client, which is currently used in garbage collector and namespace controller.
    • Only JSON is supported

Informer analysis

The official schematic diagram illustrates the workflow of each component in the client go library and the interaction point of the custom controller code to be written

Schematic analysis:

Client go component:

  1. Reflector: defined in /Reflector type within tools/cache package The reflector in monitors the Kubernetes API to get the specified resource type (Kind). The function that does this is ListAndWatch. Monitoring can be used for built-in resources or custom resources. When the reflector receives a notification about the existence of a new resource instance through the monitoring API, it uses the corresponding listing API to obtain the newly created object and put it into the Delta Fifo queue in the watchHandler function.
  2. Informer: in /Basic controller in tools/cache package An informer defined in pops an object from the Delta FIFO queue. The function that does this is processLoop. The basic controller's task is to save the object for later retrieval, and call the controller to pass the object to it.
  3. Indexer: indexer provides indexing function for objects. It is defined in /Indexer type in tools/cache package . A typical indexing use case is to create an index based on object tags. Indexer can maintain indexes based on multiple index functions. Indexer uses thread safe data storage to store objects and their key values. stay /Store type in tools/cache package A default function named MetaNamespaceKeyFunc is defined, which generates an object key value named < namespace > / < name > combination for the object.

Custom Controller component:

  1. Informer reference: This is a reference to an informer instance that knows how to use custom resource objects. Your custom controller code needs to create the appropriate informer.
  2. Indexer reference: This is a reference to an indexer instance that knows how to use a custom resource object. Your custom controller code needs to create this. You will use this reference to retrieve objects for later processing.
  3. Resource Event Handlers: these callback functions will be called when Informer wants to distribute an object to your controller. The typical mode of writing these functions is to get the key value of the allocated object and put the key value into a work queue for further processing.
  4. Work queue: This is a queue created in the controller code to decouple the distribution and processing of objects. Write the Resource Event Handler function to extract the key value of the distributed object and add it to the work queue.
  5. Process Item: This is a function created in code to process items in the work queue. There can be one or more other functions to perform the actual processing. These functions usually use Indexer reference Or Listing wrapper to get the object corresponding to the key value.

Code analysis corresponding to schematic diagram:

client-go/tools/cache
.
├── controller.go # Including: Config, Run, processLoop, NewInformer, newindexerininformer
├── controller_test.go
├── delta_fifo.go # Including: NewDeltaFIFO, DeltaFIFO, AddIfNotPresent
├── delta_fifo_test.go
├── doc.go
├── expiration_cache_fakes.go
├── expiration_cache.go
├── expiration_cache_test.go
├── fake_custom_store.go
├── fifo.go # Including: Queue, FIFO, NewFIFO
├── fifo_test.go
├── heap.go
├── heap_test.go
├── index.go # Including: Indexer, MetaNamespaceIndexFunc
├── index_test.go
├── listers.go
├── listwatch.go # Including: ListerWatcher, ListWatch, List, Watch
├── main_test.go
├── mutation_cache.go
├── mutation_detector.go
├── mutation_detector_test.go
├── OWNERS
├── processor_listener_test.go
├── reflector.go # Including: Reflector, NewReflector, Run, ListAndWatch
├── reflector_metrics.go
├── reflector_test.go
├── shared_informer.go # Including: newsharedininformer, WaitForCacheSync, Run, HasSynced
├── shared_informer_test.go
├── store.go # Including: Store, MetaNamespaceKeyFunc, SplitMetaNamespaceKey
├── store_test.go
├── testing
│   ├── fake_controller_source.go
│   └── fake_controller_source_test.go
├── thread_safe_store.go # Including: ThreadSafeStore, threadSafeMap
├── thread_safe_store_test.go
├── undelta_store.go
└── undelta_store_test.go

1 directory, 36 files

Reference link:

https://zhuanlan.zhihu.com/p/202611841?utm_source=wechat_session

Keywords: Go Kubernetes

Added by malikah on Sun, 23 Jan 2022 14:14:39 +0200