使用以下代码,我可以获取集群中运行的所有 Pod。如何使用 Kubernetes go-client 库找到 Pod 控制器(部署/DaemonSet)?
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()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the kubeClient
kubeClient, err := kubernetes.NewForConfig(config)
metricsClient, err := metricsv.NewForConfig(config)
if err != nil {
panic(err.Error())
}
pods, err := kubeClient.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, pod := range pods.Items {
fmt.Println(pod.Name)
// how can I get the Pod controller? (Deployment/DaemonSet)
// e.g. fmt.Println(pod.Controller.Name)
}