从kubeconfig文件中获取服务器地址

huangapple go评论88阅读模式
英文:

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: &lt;OAM ip address&gt; this is what I want
(...)

I want to get the server address.
Previously searching , I've found this solution:

config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *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 &lt; 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 (
	&quot;flag&quot;
	&quot;fmt&quot;
	&quot;path/filepath&quot;

	&quot;k8s.io/client-go/kubernetes&quot;
	&quot;k8s.io/client-go/tools/clientcmd&quot;
	&quot;k8s.io/client-go/util/homedir&quot;
)

func main() {
	var kubeconfig *string

	if home := homedir.HomeDir(); home != &quot;&quot; {
		kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
	} else {
		kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
	}

	flag.Parse()

	config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
	if err != nil {
		panic(err)
	}

	fmt.Printf(&quot;server: %s\n&quot;, 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(&quot;%+v\n&quot;, config)

For more details, look at the reference documentation.

huangapple
  • 本文由 发表于 2022年10月26日 03:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/74199122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定