List and find deployment created #3158
-
Hi guys, I'm creating the controller to list all all pods that are execution in the cluster but now I need get the management of pod if is deployment, statefulset, daemontset. How can I get these resources/dependencies? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @Tomelin, To determine the kind of workload (e.g., Deployment, StatefulSet, or DaemonSet) you might can do by inspecting the Following some example: package controllers
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (r *YourReconciler) GetPodController(pod *corev1.Pod) (string, error) {
for _, ownerReference := range pod.OwnerReferences {
if ownerReference.Kind == "Deployment" {
return "Deployment", nil
} else if ownerReference.Kind == "StatefulSet" {
return "StatefulSet", nil
} else if ownerReference.Kind == "DaemonSet" {
return "DaemonSet", nil
}
}
return "", nil
}
func (r *YourReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var pod corev1.Pod
if err := r.Get(ctx, types.NamespacedName{Name: req.Name, Namespace: req.Namespace}, &pod); err != nil {
// handle err
}
controllerType, err := r.GetPodController(&pod)
if err != nil {
// handle err
}
if controllerType != "" {
// Now you know the type of controller managing this pod and can act accordingly
}
// Rest of your reconcile logic
} This example fetches the Pod and checks its owner references to determine its controller type. You can then use this information for further processing or create conditions in your controller. I hope that helps you out. |
Beta Was this translation helpful? Give feedback.
Hi @Tomelin,
To determine the kind of workload (e.g., Deployment, StatefulSet, or DaemonSet) you might can do by inspecting the
ownerReferences
field in the Pod's metadata. TheownerReferences
field will typically provide information about the parent object that "owns" this resource.Following some example: