[Play with client-go] Obtain the ClientSet of all clusters through the Kubernetes configuration file containing multiple cluster information

Table of contents

background

example

analyze

use

Summarize


background

In the Kubernetes environment, you can interact with the cluster through the Kubernetes API Server. In the Go language, you can use the Client-Go library officially provided by Kubernetes to perform API operations. Before using the Client-Go library, you need to configure the authentication information of the Kubernetes cluster and the address of the API Server. These information are stored in the configuration file of the Kubernetes cluster.

In this blog post, we will introduce how to obtain the ClientSets of all clusters through a Kubernetes configuration file containing multiple cluster information. Before that, you need to understand some basic concepts:

  • Kubernetes configuration file: This file contains information such as authentication information related to the Kubernetes cluster and the address of the API Server. By default, this file is stored in the .kube/config file in the user's home directory.
  • rest.Config: This structure contains configuration information such as the address and authentication information of the Kubernetes API Server. When using the Client-Go library, you need to create a Kubernetes client object through this structure first.

example

Next, we will use a piece of sample code to demonstrate how to obtain *rest.Config of all clusters through a Kubernetes configuration file containing multiple cluster information. Please see the code below:

package main

import (
	"context"
	"fmt"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

func main() {
	// 从文件中加载Kubernetes配置
	cfg, err := clientcmd.LoadFromFile("./config")
	if err != nil {
		panic(err.Error())
	}

	// 初始化一个map,用于存储所有集群的*rest.Config
	configs := make(map[string]*rest.Config, 0)

	// 遍历所有的Context,获取每个集群的*rest.Config
	for name := range cfg.Contexts {
		// 构建Config对象
		restcfg, err := clientcmd.BuildConfigFromKubeconfigGetter("", func() (*clientcmdapi.Config, error) {
			cfg.CurrentContext = name
			return cfg.DeepCopy(), nil
		})
		if err != nil {
			panic(err.Error())
		}

		// 将Config对象存储到map中
		configs[name] = restcfg
	}

	// 遍历所有的*rest.Config,获取每个集群中的Namespace列表
	for _, cluster := range configs {
		// 通过*rest.Config创建Kubernetes客户端对象
		cli, err := kubernetes.NewForConfig(cluster)
		if err != nil {
			panic(err.Error())
		}

		// 获取Namespace列表
		nslist, err := cli.CoreV1().Namespaces().List(context.Background(), v1.ListOptions{})
		if err != nil {
			panic(err.Error())
		}

		// 遍历所有的Namespace,打印出名称
		for _, ns := range nslist.Items {
			fmt.Println(ns.Name)
		}
	}
}

analyze

In the above code, we first load the Kubernetes configuration from the file through the clientcmd.LoadFromFile() function, then traverse all Contexts, build a rest.Config object for each cluster, and store it in a map. In this process, we use the clientcmd.BuildConfigFromKubeconfigGetter() function to build the rest.Config object. This function takes a function as a parameter, and this function returns a clientcmdapi.Config object, which contains the relevant configuration information of the current cluster. Use the cfg.DeepCopy() function to return a copy identical to the original clientcmdapi.Config object, and then set the CurrentContext field to the name of the currently traversed Context. Finally, pass this deep copied clientcmdapi.Config object together with an empty string to the clientcmd.BuildConfigFromKubeconfigGetter() function to build a *rest.Config object.

use

Next, we iterate over all *rest.Config objects and use the kubernetes.NewForConfig() function to create a Kubernetes client object for each cluster. In this process, we use the kubernetes.CoreV1().Namespaces().List() function to obtain a list of Namespaces in each cluster, and print out the name of each Namespace.

Eventually, the program will output a list of Namespaces in each cluster. Since we added two clusters in the configuration file, the output should contain all Namespace names in both clusters.

Summarize

To sum up, through the above code examples, we can learn how to obtain the rest.Config of all clusters through a Kubernetes configuration file containing multiple cluster information, and how to use the rest.Config object to create a Kubernetes client object and obtain each cluster. Namespace list. This is a very useful skill for developers managing multiple Kubernetes clusters.

Guess you like

Origin blog.csdn.net/kingu_crimson/article/details/129916304