37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
galal-hussein 提交于 2019-02-28 07:21 . Fix using InClusterConfig
package k8s
import (
"context"
"fmt"
"io/ioutil"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func GetConfig(ctx context.Context, k8sMode string, kubeConfig string) (bool, context.Context, *rest.Config, error) {
var (
cfg *rest.Config
err error
)
switch k8sMode {
case "auto":
return getAuto(ctx, kubeConfig)
case "embedded":
return getEmbedded(ctx)
case "external":
cfg, err = getExternal(kubeConfig)
default:
return false, nil, nil, fmt.Errorf("invalid k8s-mode %s", k8sMode)
}
return false, ctx, cfg, err
}
func getAuto(ctx context.Context, kubeConfig string) (bool, context.Context, *rest.Config, error) {
if kubeConfig != "" {
cfg, err := getExternal(kubeConfig)
return false, ctx, cfg, err
}
if config, err := rest.InClusterConfig(); err == nil {
if config.BearerToken == "" {
tokenBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
if err == nil {
config.BearerToken = string(tokenBytes)
}
}
return false, ctx, config, nil
}
return getEmbedded(ctx)
}
func getExternal(kubeConfig string) (*rest.Config, error) {
return clientcmd.BuildConfigFromFlags("", kubeConfig)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.8

搜索帮助