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

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

Go - get server address from kubeconfig file

问题

基本上,我的kubeconfig文件包含:

  1. apiVersion: v1
  2. clusters:
  3. - cluster:
  4. server: <OAM IP地址> 这就是我想要的
  5. (...)

我想要获取服务器地址。
之前搜索时,我找到了这个解决方案:

  1. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  2. if err != nil {
  3. panic(err.Error())
  4. }
  5. // 创建clientset
  6. clientset, err := kubernetes.NewForConfig(config)
  7. if err != nil {
  8. panic(err.Error())
  9. }
  10. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  11. if err != nil {
  12. panic(err)
  13. }
  14. nodeip := []corev1.NodeAddress{}
  15. for i := 0; i < len(nodes.Items); i++ {
  16. nodeip = nodes.Items[i].Status.Addresses
  17. fmt.Println(nodeip[0].Address)
  18. }
  19. fmt.Println(nodes.Items[0].Status.Addresses)

但它给我返回的是内部IP,而不是OAM服务器IP(它在Kubernetes配置文件中)。

英文:

Basically, my kubeconfig file has:

  1. apiVersion: v1
  2. clusters:
  3. - cluster:
  4. server: &lt;OAM ip address&gt; this is what I want
  5. (...)

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

  1. config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
  2. if err != nil {
  3. panic(err.Error())
  4. }
  5. // creates the clientset
  6. clientset, err := kubernetes.NewForConfig(config)
  7. if err != nil {
  8. panic(err.Error())
  9. }
  10. nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
  11. if err != nil {
  12. panic(err)
  13. }
  14. nodeip := []corev1.NodeAddress{}
  15. for i := 0; i &lt; len(nodes.Items); i++ {
  16. nodeip = nodes.Items[i].Status.Addresses
  17. fmt.Println(nodeip[0].Address)
  18. }
  19. 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变量中读取即可:

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "path/filepath"
  6. "k8s.io/client-go/kubernetes"
  7. "k8s.io/client-go/tools/clientcmd"
  8. "k8s.io/client-go/util/homedir"
  9. )
  10. func main() {
  11. var kubeconfig *string
  12. if home := homedir.HomeDir(); home != "" {
  13. kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
  14. } else {
  15. kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
  16. }
  17. flag.Parse()
  18. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  19. if err != nil {
  20. panic(err)
  21. }
  22. fmt.Printf("server: %s\n", config.Host)
  23. }

如果你想知道rest.Config对象上还有哪些可用字段,一个快速的解决方案是使用%+v格式说明符打印出config变量:

  1. fmt.Printf("%+v\n", config)

更多详细信息,请参考参考文档

英文:

If you want the server address from the kubeconfig file, just read it from your config variable:

  1. package main
  2. import (
  3. &quot;flag&quot;
  4. &quot;fmt&quot;
  5. &quot;path/filepath&quot;
  6. &quot;k8s.io/client-go/kubernetes&quot;
  7. &quot;k8s.io/client-go/tools/clientcmd&quot;
  8. &quot;k8s.io/client-go/util/homedir&quot;
  9. )
  10. func main() {
  11. var kubeconfig *string
  12. if home := homedir.HomeDir(); home != &quot;&quot; {
  13. kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
  14. } else {
  15. kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
  16. }
  17. flag.Parse()
  18. config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
  19. if err != nil {
  20. panic(err)
  21. }
  22. fmt.Printf(&quot;server: %s\n&quot;, config.Host)
  23. }

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:

  1. 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:

确定