1. Introduction

Client-go is an official go language client library that can interact with K8S API Server. If developers want to redevelop the K8S, they can use it directly instead of reinventing the wheel.

Client-go has been integrated into K8S source code, so K8S components also use client-Go to communicate with each other (such as Kubectl operation API Server). Client-go is stored in vendor/k8s. IO /client-go in the K8S source code, as shown in the following figure:

Client-go is stored in K8S source code

This article series will cover all aspects of Client-Go, including getting started, architectural design, and source code analysis, with the source code section focusing on the Informer mechanism that is the cornerstone of communication between K8S components.

If you want to really learn K8S well, it is far from enough to only know how to operate it. Only by truly touching the bottom layer of the code and actually understanding its internal data structure and operation mechanism, can you have the realization that “you are at the top level without being afraid of floating clouds”.

Premise 2.

This series does not require the reader must have the language background, considering that most of the people’s cognitive habits, so the author adopted a easy to difficult, step by step, after first foreshadowing way of writing, of course, if meet with loves a challenge, diligently practice bravery, an easy readers don’t bump south wall is all over, this tutorial is still suitable for, you only need to read from the backward.

While this series of articles will be as forgiving as possible, if you want to truly enjoy running your application, you are advised to configure the GO SDK, go project development environment, and K8S runtime environment. As the so-called “nine layers of Taiwan, from the tired soil; A journey of a thousand miles begins with a single step.

Lao zi and the

3. Operating environment

The environment information used in this series is as follows (readers are not required to match exactly, but it is recommended that the GO SDK version be not too low) :

  • K8S version: 1.19.0
  • Docker version: 20.10.8
  • CentOS version: 7.7.1908
  • GO SDK version: GO 1.16.6 Linux/AMD64

4. Install GO SDK

The installation steps are as follows. If you have already installed it, you can skip to the next section:

  • Download and decompress
  • Setting environment Variables
  • Verify that the installation is successful
  • Configuring the package download agent (optional but recommended)

4.1 Download and Decompress

Run the following command on the command line to download and decompress the GO SDK:

CD/opt # # yum install wget - y # wget https://golang.google.cn/dl/go1.16.6.linux-amd64.tar.gz # tar ZXVF. - Go1.16.6. Linux - amd64. Tar. Gz - C/usr/localCopy the code

4.2 Setting Environment Variables

Edit the /etc/profile file and add the following at the end of the file

export GO111MODULE=on
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
Copy the code

After the configuration is saved, run the following command to make the configuration take effect:

# source /etc/profile
Copy the code

4.3 Verifying the installation

After the installation is complete and environment variables are set, run the following command on the CLI:

# go version
go version go1.16.6 linux/amd64
Copy the code

If the GO SDK version is displayed correctly (as shown above), the SDK is successfully installed.

4.4 (Optional but recommended) Configuring the Download Agent

First of all, this step is optional, but I strongly recommend this step because there will be a lot of dependencies downloaded later in the development, and if you use the default proxy, you will either not be able to download them (for reasons you know), or the download speed will be very slow, so I recommend using the following command line to modify:

## GOPROXY="https://proxy.golang.org,direct"
# go env GOPROXY
# go env -w GOPROXY="https://goproxy.io,direct"
# go env GOPROXY
https://goproxy.io,direct
Copy the code

If the preceding command output is displayed, the change is successful and the new proxy path is changed to goproxy. IO.

From there, the GO SDK setup is complete and the reader can move on to the rest of GO development.

5 References

1 Client-go repository github.com/kubernetes/…