37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cluster.go 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-05-09 11:16 . Fix windows build
// +build !windows
package cluster
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/pkg/errors"
)
const (
rancherCredentialsFolder = "/cattle-credentials"
urlFilename = "url"
tokenFilename = "token"
kubernetesServiceHostKey = "KUBERNETES_SERVICE_HOST"
kubernetesServicePortKey = "KUBERNETES_SERVICE_PORT"
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
)
func TokenAndURL() (string, string, error) {
url, err := readKey(urlFilename)
if err != nil {
return "", "", err
}
token, err := readKey(tokenFilename)
return token, url, err
}
func Params() (map[string]interface{}, error) {
caData, err := ioutil.ReadFile(rootCAFile)
if err != nil {
return nil, errors.Wrapf(err, "reading %s", rootCAFile)
}
token, err := ioutil.ReadFile(tokenFile)
if err != nil {
return nil, errors.Wrapf(err, "reading %s", tokenFile)
}
kubernetesServiceHost, err := getenv(kubernetesServiceHostKey)
if err != nil {
return nil, err
}
kubernetesServicePort, err := getenv(kubernetesServicePortKey)
if err != nil {
return nil, err
}
return map[string]interface{}{
"cluster": map[string]interface{}{
"address": fmt.Sprintf("%s:%s", kubernetesServiceHost, kubernetesServicePort),
"token": strings.TrimSpace(string(token)),
"caCert": base64.StdEncoding.EncodeToString(caData),
},
}, nil
}
func getenv(env string) (string, error) {
value := os.Getenv(env)
if value == "" {
return "", fmt.Errorf("%s is empty", env)
}
return value, nil
}
func readKey(key string) (string, error) {
bytes, err := ioutil.ReadFile(path.Join(rancherCredentialsFolder, key))
if err != nil {
return "", err
}
return string(bytes), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.7-rc1

搜索帮助