在client-go使用时,我们既想要程序在集群内部能够运行,也需要能够在集群外部运行,在集群内部运行能够读取serveraccount信息,在集群外部运行时读取~/.kube/config配置,可以使用下面的方式:
var config *rest.Config
var kubeConfig *string
if home := homeDir(); home != "" {
kubeConfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeConfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// 使用 ServiceAccount 创建集群配置(InCluster模式)
if config, err = rest.InClusterConfig(); err != nil {
// 使用 KubeConfig 文件创建集群配置
if config, err = clientcmd.BuildConfigFromFlags("", *kubeConfig); err != nil {
return err
}
}
标签:err,rest,kubeConfig,client,集群,kubeconfig,config
From: https://www.cnblogs.com/cosmos-wong/p/17039826.html