使用 client-go API 列出由部署控制器管理的 pods 不工作
Using client-go API to list pods managed by a deployment controller not working
我在 Go 中使用本机 client-go API 来获取由给定名称空间(“默认”)下的部署类型控制器管理的 Pods 的列表,但是返回的列表不包含 Pods
的列表
labelSelector := labels.Set(obj.Spec.Selector.MatchLabels)
其中 obj
是来自 https://pkg.go.dev/k8s.io/api/apps/v1?tab=doc#Deployment
的 *appsv1.Deployment
类型
podsList, err := getPodList(string(labelSelector.AsSelector().String()), kubeClient, res.Namespace)
函数定义为
func getPodList( labelSelector string, client kubernetes.Interface, ns string ) (*corev1.PodList, error) {
options := metav1.ListOptions{
LabelSelector: labelSelector,
}
podList, err := client.CoreV1().Pods(ns).List(options)
return podList, err
}
返回的类型 - https://pkg.go.dev/k8s.io/api/core/v1?tab=doc#PodList 应包含 Items []Pod
切片,这在我返回的信息中不可用。
在 Go 代码中使用以下包
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
似乎我一直都能正常工作,但只是错误地处理了 CoreV1().Pods(ns).List(options)
的 return 类型。它是 return 指向 PodList
- https://pkg.go.dev/k8s.io/api/core/v1?tab=doc#PodList
片段的指针
这是我编写的对后代有用的最小代码
package main
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
)
func main() {
kubeconfig := filepath.Join(
os.Getenv("HOME"), ".kube", "config",
)
// Initialize kubernetes-client
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("Error building kubeconfig: %v\n", err)
os.Exit(1)
}
// create new client with the given config
// https://pkg.go.dev/k8s.io/client-go/kubernetes?tab=doc#NewForConfig
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Printf("Error building kubernetes clientset: %v\n", err)
os.Exit(2)
}
// use the app's label selector name. Remember this should match with
// the deployment selector's matchLabels. Replace <APPNAME> with the
// name of your choice
options := metav1.ListOptions{
LabelSelector: "app=<APPNAME>",
}
// get the pod list
// https://pkg.go.dev/k8s.io/client-go@v11.0.0+incompatible/kubernetes/typed/core/v1?tab=doc#PodInterface
podList, _ := kubeClient.CoreV1().Pods("default").List(options)
// List() returns a pointer to slice, derefernce it, before iterating
for _, podInfo := range (*podList).Items {
fmt.Printf("pods-name=%v\n", podInfo.Name)
fmt.Printf("pods-status=%v\n", podInfo.Status.Phase)
fmt.Printf("pods-condition=%v\n", podInfo.Status.Conditions)
}
}
我在 Go 中使用本机 client-go API 来获取由给定名称空间(“默认”)下的部署类型控制器管理的 Pods 的列表,但是返回的列表不包含 Pods
的列表labelSelector := labels.Set(obj.Spec.Selector.MatchLabels)
其中 obj
是来自 https://pkg.go.dev/k8s.io/api/apps/v1?tab=doc#Deployment
*appsv1.Deployment
类型
podsList, err := getPodList(string(labelSelector.AsSelector().String()), kubeClient, res.Namespace)
函数定义为
func getPodList( labelSelector string, client kubernetes.Interface, ns string ) (*corev1.PodList, error) {
options := metav1.ListOptions{
LabelSelector: labelSelector,
}
podList, err := client.CoreV1().Pods(ns).List(options)
return podList, err
}
返回的类型 - https://pkg.go.dev/k8s.io/api/core/v1?tab=doc#PodList 应包含 Items []Pod
切片,这在我返回的信息中不可用。
在 Go 代码中使用以下包
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
似乎我一直都能正常工作,但只是错误地处理了 CoreV1().Pods(ns).List(options)
的 return 类型。它是 return 指向 PodList
- https://pkg.go.dev/k8s.io/api/core/v1?tab=doc#PodList
这是我编写的对后代有用的最小代码
package main
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
)
func main() {
kubeconfig := filepath.Join(
os.Getenv("HOME"), ".kube", "config",
)
// Initialize kubernetes-client
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("Error building kubeconfig: %v\n", err)
os.Exit(1)
}
// create new client with the given config
// https://pkg.go.dev/k8s.io/client-go/kubernetes?tab=doc#NewForConfig
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Printf("Error building kubernetes clientset: %v\n", err)
os.Exit(2)
}
// use the app's label selector name. Remember this should match with
// the deployment selector's matchLabels. Replace <APPNAME> with the
// name of your choice
options := metav1.ListOptions{
LabelSelector: "app=<APPNAME>",
}
// get the pod list
// https://pkg.go.dev/k8s.io/client-go@v11.0.0+incompatible/kubernetes/typed/core/v1?tab=doc#PodInterface
podList, _ := kubeClient.CoreV1().Pods("default").List(options)
// List() returns a pointer to slice, derefernce it, before iterating
for _, podInfo := range (*podList).Items {
fmt.Printf("pods-name=%v\n", podInfo.Name)
fmt.Printf("pods-status=%v\n", podInfo.Status.Phase)
fmt.Printf("pods-condition=%v\n", podInfo.Status.Conditions)
}
}