1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果您想要文件中的服務(wù)器地址,只需從您的變量kubeconfig中讀取它:config
package main
import (
"flag"
"fmt"
"path/filepath"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
var kubeconfig *string
if home := homedir.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()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
fmt.Printf("server: %s\n", config.Host)
}
如果您好奇對(duì)象上還有哪些其他字段可用,一個(gè)快速的解決方案是使用格式說明符rest.Config打印出變量:config%+v
fmt.Printf("%+v\n", config)
有關(guān)詳細(xì)信息,請查看參考文檔。
- 1 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報(bào)