k8s多集群go-client实现

背景

通常我们使用k8s go-client进行基于k8s的容器平台开发,go-client 使用方法GetOutClusterClient读取本地kubeconfig实现,但是多集群这样操作不太方便。

操作方法

  1. 数据库存储多集群信息,包括masterIP,kubeconfig和元数据信息(扩展);
  2. 从数据库捞出集群信息;
  3. 实例化clientset;

核心代码

func InitClient(clusterName string) (*kubernetes.Clientset, *rest.Config, error) {
	//数据库取出集群信息
	master, kubeconfig, err := GetClusterInfo(clusterName)
	if err != nil {
		logs.Error("get db for cluster kubeconfig error. %v ", err)
		return nil, nil, err
	}
	kubeconfigJson, err := yaml.YAMLToJSON([]byte(kubeconfig))
	if err != nil {
		logs.Error("yaml to json err")
	}
	configV1 := clientcmdapiv1.Config{}
	err = json.Unmarshal(kubeconfigJson, &configV1)
	if err != nil {
		logs.Error("json unmarshal kubeconfig error. %v ", err)
		return nil, nil, err
	}
	// 切换匹配的版本
	configObject, err := clientcmdlatest.Scheme.ConvertToVersion(&configV1, clientcmdapi.SchemeGroupVersion)
	if err != nil {
		logs.Error("ConvertToVersion error. %v ", err)
		return nil, nil, err
	}
	configInternal := configObject.(*clientcmdapi.Config)

	// 实例化配置信息
	clientConfig, err := clientcmd.NewDefaultClientConfig(*configInternal, &clientcmd.ConfigOverrides{
		ClusterDefaults: clientcmdapi.Cluster{Server: master},
	}).ClientConfig()

	if err != nil {
		logs.Error("build client config error. %v ", err)
		return nil, nil, err
	}
	clientConfig.QPS = defaultQPS
	clientConfig.Burst = defaultBurst
	// 实例化客户端
	clientSet, err := kubernetes.NewForConfig(clientConfig)

	if err != nil {
		logs.Error("(%s) kubernetes.NewForConfig(%v) error.%v", master, err, clientConfig)
		return nil, nil, err
	}
	return clientSet, clientConfig, nil

}

func GetOutClusterClient(name string) (*K8sClient, error) {
	clientSet, config, err := InitClient(name)
	if err != nil {
		return nil, err
	}
	return &K8sClient{Clientset: clientSet, Config: config}, nil
}

我们也可以使用集群内的方式,通过token鉴权,实现管理多集群

1、获取集群的token,没有创建sa绑定到cluster-admin这个clusterrole

kubectl describe secrets $(kubectl get secrets -n kube-system |grep admin |cut -f1 -d ' ') -n kube-system |grep -E '^token' |cut -f2 -d':'|tr -d '\t'|tr -d ' '

核心代码

func GetInClusterClient(name string) (*Client, error) {
	cluster, err := getClusterInfo(name)
	if err != nil{
		return nil, err
	}
	config := &rest.Config{
		Host:                cluster.Master,
		APIPath:             "",
		ContentConfig:       rest.ContentConfig{},
		Username:            "",
		Password:            "",
		BearerToken:         cluster.Token,
		BearerTokenFile:     "",
		Impersonate:         rest.ImpersonationConfig{},
		AuthProvider:        nil,
		AuthConfigPersister: nil,
		ExecProvider:        nil,
		TLSClientConfig:     rest.TLSClientConfig{
			Insecure: true,
		},
		UserAgent:           "",
		DisableCompression:  false,
		Transport:           nil,
		WrapTransport:       nil,
		QPS:                 0,
		Burst:               0,
		RateLimiter:         nil,
		WarningHandler:      nil,
		Timeout:             0,
		Dial:                nil,
		Proxy:               nil,
	}
	client, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}
	return &Client{Clientset: client, Config: config}, nil
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐