英文:
Go - get server address from kubeconfig file
问题
基本上,我的kubeconfig文件包含:
apiVersion: v1
clusters:
- cluster:
server: <OAM IP地址> 这就是我想要的
(...)
我想要获取服务器地址。
之前搜索时,我找到了这个解决方案:
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// 创建clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
panic(err)
}
nodeip := []corev1.NodeAddress{}
for i := 0; i < len(nodes.Items); i++ {
nodeip = nodes.Items[i].Status.Addresses
fmt.Println(nodeip[0].Address)
}
fmt.Println(nodes.Items[0].Status.Addresses)
但它给我返回的是内部IP,而不是OAM服务器IP(它在Kubernetes配置文件中)。
英文:
Basically, my kubeconfig file has:
apiVersion: v1
clusters:
- cluster:
server: <OAM ip address> this is what I want
(...)
I want to get the server address.
Previously searching , I've found this solution:
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
panic(err)
}
nodeip := []corev1.NodeAddress{}
for i := 0; i < len(nodes.Items); i++ {
nodeip = nodes.Items[i].Status.Addresses
fmt.Println(nodeip[0].Address)
}
fmt.Println(nodes.Items[0].Status.Addresses)
But it gives me the Internal IP, not the OAM server IP (which is inside the Kubernetes config file)
答案1
得分: 1
如果你想从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
对象上还有哪些可用字段,一个快速的解决方案是使用%+v
格式说明符打印出config
变量:
fmt.Printf("%+v\n", config)
更多详细信息,请参考参考文档。
英文:
If you want the server address from the kubeconfig
file, just read it from your config
variable:
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)
}
If you're curious what other fields are available on the rest.Config
object, a quick solution is to print out the config
variable using the %+v
format specifier:
fmt.Printf("%+v\n", config)
For more details, look at the reference documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论