With the following code, I'm able to fetch all the Pods running in a cluster. How can I find the Pod Controller (Deployment/DaemonSet) using the Kubernetes go-client library?
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)
}
By following @Jonas suggestion I was able to get Pod's manager. Here's a fully working sample:
package main
import (
"context"
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
)
func main() {
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)
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 {
if len(pod.OwnerReferences) == 0 {
fmt.Printf("Pod %s has no owner", pod.Name)
continue
}
var ownerName, ownerKind string
switch pod.OwnerReferences[0].Kind {
case "ReplicaSet":
replica, repErr := kubeClient.AppsV1().ReplicaSets(pod.Namespace).Get(context.TODO(), pod.OwnerReferences[0].Name, metav1.GetOptions{})
if repErr != nil {
panic(repErr.Error())
}
ownerName = replica.OwnerReferences[0].Name
ownerKind = "Deployment"
case "DaemonSet", "StatefulSet":
ownerName = pod.OwnerReferences[0].Name
ownerKind = pod.OwnerReferences[0].Kind
default:
fmt.Printf("Could not find resource manager for type %s\n", pod.OwnerReferences[0].Kind)
continue
}
fmt.Printf("POD %s is managed by %s %s\n", pod.Name, ownerName, ownerKind)
}
}