1 回答
TA贡献1784条经验 获得超8个赞
似乎我一直都在工作,但只是CoreV1().Pods(ns).List(options)错误地处理了返回类型。它返回一个指向切片的指针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)
}
}
- 1 回答
- 0 关注
- 153 浏览
添加回答
举报