1 回答
TA贡献1946条经验 获得超4个赞
如果您想要文件中的服务器地址,只需从您的变量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)
}
如果您好奇对象上还有哪些其他字段可用,一个快速的解决方案是使用格式说明符rest.Config打印出变量:config%+v
fmt.Printf("%+v\n", config)
有关详细信息,请查看参考文档。
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报