代码拉取完成,页面将自动刷新
// +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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。