在列出节点时,go-client出现未定义错误。

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

Undefined error in go-client when listing the nodes

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习golang,并尝试使用client-go列出我的minikube集群中的节点。但是我遇到了以下问题:

> nodeList.Items未定义(类型*无效类型没有字段或方法Items)compilerMissingFieldOrMethod

以下是我用于此问题的代码片段:

package main

import (
	"context"
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	rules := clientcmd.NewDefaultClientConfigLoadingRules()
	kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})
	config, err := kubeconfig.ClientConfig()
	if err != nil {
		panic(err)
	}
	clientset := kubernetes.NewForConfigOrDie(config)

	nodeList, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})

	if err != nil {
		panic(err)
	}

	for _, node := range nodeList.Items {
		fmt.Printf("%s\n", node.Name)
	}
}

有人可以帮我看看问题出在哪里吗?

谢谢!

英文:

I am new to golang and I am trying to list the nodes in my minikube cluster with the client-go. And I encounter the following issue:

>nodeList.Items undefined (type *invalid type has no field or method Items)compilerMissingFieldOrMethod

And here's my code snippet for this:

package main

import (
	"context"
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	rules := clientcmd.NewDefaultClientConfigLoadingRules()
	kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})
	config, err := kubeconfig.ClientConfig()
	if err != nil {
		panic(err)
	}
	clientset := kubernetes.NewForConfigOrDie(config)

	nodeList, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})

	if err != nil {
		panic(err)
	}

	for _, node := range nodeList.Items {
		fmt.Printf("%s\n", node.Name)
	}
}

Can someone help me what is the problem here?

Thank you!

答案1

得分: 1

看起来你的clientset没有从你的kubeconfig文件中创建。我建议你按照以下方式创建clientset。我在这里使用了集群外配置来创建clientset。你也可以使用InclusterConfig来创建它。

package main

import (
	"context"
	"flag"
	"fmt"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/util/homedir"
	"path/filepath"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	// 解析.kubeconfig文件
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) kubeconfig文件的绝对路径")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "kubeconfig文件的绝对路径")
	}
	flag.Parse()

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

	// 创建clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err)
	}

	nodeList, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})

	if err != nil {
		panic(err)
	}

	for _, node := range nodeList.Items {
		fmt.Printf("%s\n", node.Name)
	}
}
英文:

It looks like the clientset is not created from your kubeconfig. I would suggest you to create the clientset in the following way. I have used out of cluster config here to create the clientset. You can also create it using InclusterConfig.

package main

import (
	"context"
	"flag"
	"fmt"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/util/homedir"
	"path/filepath"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	// parse the .kubeconfig file
	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)
	}

	// create the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err)
	}

	nodeList, err := clientset.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})

	if err != nil {
		panic(err)
	}

	for _, node := range nodeList.Items {
		fmt.Printf("%s\n", node.Name)
	}
}

huangapple
  • 本文由 发表于 2022年11月17日 17:14:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/74472827.html
匿名

发表评论

匿名网友

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

确定