How can I find a Pod's Controller (Deployment/DaemonSet) using the Kubernetes go-client library?

5/10/2021

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)
}
-- AngryPanda
kubernetes
kubernetes-go-client

2 Answers

5/10/2021

You can typically see what manages the Pod in the ownerReference:-field of the Metadata-part.

Example:

  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: my-app-6b94f5f96
    uid: 46493c9e-f264-49b8-9f52-d1d919aedbf2
-- Jonas
Source: StackOverflow

5/12/2021

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)
	}
}
-- AngryPanda
Source: StackOverflow