1. 程式人生 > >使用 client-go 控制原生及拓展的 Kubernetes API

使用 client-go 控制原生及拓展的 Kubernetes API

I have more then 5 kubernetes clusters.
Below is my code,

func ListAllNodes(configPath string) {
config, err := clientcmd.BuildConfigFromFlags(, configPath)
clientSet, err := kubernetes.NewForConfig(config)
nodes,err := clientSet.CoreV1().Nodes().List(metav1.ListOptions{})
fmt.Println(err)
fmt.Println( len(nodes.Items))
}
ListAllNodes(/home/cluster1/config)
ListAllNodes(/home/cluster2/config)
ListAllNodes(/home/cluster3/config)
ListAllNodes(/home/cluster4/config)
ListAllNodes(/home/cluster5/config)

Here is output:

2
5
Forbidden: /api/v1/nodes (get nodes) UnexpectedServerResponse Forbidden
Forbidden: /api/v1/nodes (get nodes) UnexpectedServerResponse Forbidden
Forbidden: /api/v1/nodes (get nodes) UnexpectedServerResponse Forbidden

If I change the cluster order, still work for first two clusters. The rest cluster doesn’t work.

func ListAllNodes(configPath string) {
config, err := clientcmd.BuildConfigFromFlags(, configPath)
clientSet, err := kubernetes.NewForConfig(config)
nodes,err := clientSet.CoreV1().Nodes().List(metav1.ListOptions{})
fmt.Println(err)
fmt.Println( len(nodes.Items))
}
ListAllNodes(/home/cluster5/config)
ListAllNodes(/home/cluster4/config)
ListAllNodes(/home/cluster1/config)
ListAllNodes(/home/cluster2/config)
ListAllNodes(/home/cluster3/config)

Here is output:

6
2
Forbidden: /api/v1/nodes (get nodes) UnexpectedServerResponse Forbidden
Forbidden: /api/v1/nodes (get nodes) UnexpectedServerResponse Forbidden
Forbidden: /api/v1/nodes (get nodes) UnexpectedServerResponse Forbidden

1年前 (2017-06-17)回覆