diff --git a/internal/controller/labelgroup_controller.go b/internal/controller/labelgroup_controller.go index 6938868..260eba0 100644 --- a/internal/controller/labelgroup_controller.go +++ b/internal/controller/labelgroup_controller.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "strconv" + "strings" "time" "github.com/go-logr/logr" @@ -179,6 +180,17 @@ func (r *LabelGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] podNames: %s", podNames)) // trace r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] namespaceNames: %s", namespaceNames)) // trace + podsInNamespaces, err := r.filterPodsInNamespace(ctx, labelGroup.Namespace, labelGroup.Status.KubernetesLabels) + + // r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] podNames: %s", podsInNamespaces)) // trace + + var printPodNames []string + for _, pod := range podsInNamespaces { + printPodNames = append(printPodNames, pod.Name) + } + + r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] podNames: %s", strings.Join(printPodNames, ", "))) + if err != nil || len(podNames) == 0 || len(namespaceNames) == 0 { r.Logger.V(0).Error(err, "[Reconcile] Couldn't get pods for the labels provided.") r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] labelGroup: %#v", labelGroup)) // trace diff --git a/internal/controller/resource_manager.go b/internal/controller/resource_manager.go index 67c7ad3..8c6338c 100644 --- a/internal/controller/resource_manager.go +++ b/internal/controller/resource_manager.go @@ -24,8 +24,25 @@ import ( susqlv1 "github.com/sustainable-computing-io/susql-operator/api/v1" v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/labels" ) +func (r *LabelGroupReconciler) filterPodsInNamespace(ctx context.Context, namespace string, labelSelector map[string]string) ([]v1.Pod, error) { + // Initialize list options with label selector + listOptions := &client.ListOptions{ + Namespace: namespace, + LabelSelector: labels.SelectorFromSet(labels.Set(labelSelector)), + } + + // List pods in the specified namespace with label selector applied + var podList v1.PodList + if err := r.Client.List(ctx, &podList, listOptions); err != nil { + return nil, err + } + + return podList.Items, nil +} + // Functions to get data from the cluster func (r *LabelGroupReconciler) GetPodNamesMatchingLabels(ctx context.Context, labelGroup *susqlv1.LabelGroup) ([]string, []string, error) { pods := &v1.PodList{}