introduce

Kubernetes official from August 2016, the Kubernetes resource operation related core source code extraction, independent out of a project client-go, Kubernetes use client-go as the go language official programming interactive client library, Provides interactive access to API Server services. For the secondary development of K8S, it is very necessary to master client-Go.

Source address: hub.fastgit.org/kubernetes/…

Project directory resolution

. ├ ─ ─ applyconfigurations ├ ─ ─ CHANGELOG. Md ├ ─ ─ code - of - conduct. Md ├ ─ ─ CONTRIBUTING. Md ├ ─ ─ discovery through Kubernetes # ├─ Examples ├─ Go. Mod ├─ Go. Sum ├─ Informers # A really cool way to interact, through Reflector Events for the Watch resource are queued (DeltaFIFO) and distributed to OnAd in the specific ResourceEventHandler through pendingNotifications(buffer.ringgrowing) of sharedProcessor D /OnUpdate/OnDelete ├─ Install.md ├─ Kubernetes ├─ Kubernetes_Test ├─ LICENSE ├─ Listers # for each Download download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download Download ├─ README. Md ├─ REST # ├─ Restmapper ├─ ScaleClient For scaling up or scaling down Deployment, Replicaset, ├─ SECURITY_CONTACTS ├─ Testing ├─ third_party ├─ tools the /clientcmd below provides some basic tools for creating a client ├─ transport # Provides secure TCP connection, supports HTTP Stream, some operations need to transfer binary Stream between client and container, e.g., exec, attach, etc. For example, WorkQueue WorkQueue and Certificate management. 19 directories and 10 filesCopy the code

The Client types

  1. RESTClient: RESTClient is a basic infrastructure that can be used to interact with RESTClient RESTful methods such as Get(), Put(), Post(), and Delete()
    • Supports both Json and Protobuf
    • All native resources and CRDs are supported
    • However, in general, for more elegant processing, further encapsulation is required. The RESTClient is encapsulated by clientSet, and then interfaces and services are provided externally.
  2. Clientset: Clientset is the most common client to call Kubernetes resource objects and can operate on 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 using a Controller object, plus Informer;
  3. DynamicClient: DynamicClient is a DynamicClient that can handle all kubernetes resources. Unlike Clientset, DynamicClient returns a single objectmap[string]interface{}.
    • If you need to control all apis in a controller, you can use DynamicClient, which is currently used in the Garbage Collector and Namespace Controller.
    • Only support JSON

Informer analysis

An official schematic illustrating the workflow of the components in the Client-Go library and the interaction points of the custom controller code to be written

Schematic diagram analysis:

Client – go components:

  1. Reflector: Defined as a Reflector type in the /tools/cache package, Reflector monitors the Kubernetes API to get the specified resource type (Kind). The function that does this is ListAndWatch. Monitoring can be used for either built-in resources or custom resources. When Reflector receives notification of the existence of a new resource instance through the monitoring API, it uses the appropriate Listing API to fetch the newly created object and place it in the Delta Fifo queue within the watchHandler function.
  2. Informer: An Informer defined in the base Controller in the /tools/cache package pops objects from the Delta FIFO queue. The function that does this is processLoop. The job of the base controller is to save the object for later retrieval and call the Controller to pass the object to it.
  3. IndexerIndexer provides the index function for objects. It is defined in/tools/ The Indexer type in the cache package. A typical index use case is to create an index based on an object label. Indexer can maintain indexes based on multiple index functions. Indexer uses thread-safe data stores to store objects and their key values. in/tools/ Cache Store type in the cache packageDefines a name namedMetaNamespaceKeyFuncThe default function for this object is called<namespace>/<name>The combined object key value.

Custom Controller components:

  1. Informer Reference: This is a reference to an instance of Informer that knows how to use a custom resource object. 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 the object for later processing.
  3. Resource Event Handlers: These callbacks are called when the Informer wants to distribute an object to your controller. The typical pattern for writing these functions is to take the key value of the allocated object and put it into a work queue for further processing.
  4. Work Queue: This is a queue created in the controller code to decouple the distribution and location 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 the function created in the code to Process items in the Work Queue. There can be one or more other functions to perform the actual processing. These functions typically use Indexer references or Listing Wrapper to get the object corresponding to the key value.

Code analysis corresponding to schematic diagram:

Client-go/Tools/Cache.├ ── Controller.go ├─ Controller_Test.go ├─ Delta_fifo. Go # Include: NewDeltaFIFO, DeltaFIFO, AddIfNotPresent ├── Dita_fifo_Test. Go ├── Dita_Fifo_Test ├── Expiration_Cache_Test. Go ├── Expiration_cache_Test. Go ├── Expiration_Custom_Store. ├─ Heap. Go Flag school ─ Heap. Go Flag School ─ Heap. Go Flag School ─ Heap. Indexindexfunc ├─ Index_Test. Go ├─ Listers. Go ├─ Listwatch. ListerWatcher, ListWatch, List, Watch ├ ─ ─ main_test. Go ├ ─ ─ mutation_cache. Go ├ ─ ─ mutation_detector. Go ├ ─ ─ Mutation_detector_test. Go ├── OWNERS ├─ Processor_Listener_Test. Go ├── Reflector. Reflector, NewReflector, Run, ListAndWatch ├── Reflector_metrics. Go Flag ── Reflector_test. Go Flag ── Reflector_test. NewSharedInformer, WaitForCacheSync, Run, HasSynced ├─ ShareD_Informer_Test.Go ├─ Store.go # ├─ Store, ├─ go ├─ testing │ ├─ fake_controller_source. Go │ ├─ Fake_controller_source_test.go ├─ thread_safe_store.go # ├─ Undelta_store. Go ├─ undelta_store. Go ├─ Undelta_store. Go ├─ Undelta_store. Go ├─ Undelta_store. 36 filesCopy the code

Reference links:

Zhuanlan.zhihu.com/p/202611841…