Skip to content

Commit

Permalink
get pods from namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: MohammedAbdi <[email protected]>
  • Loading branch information
mamy-CS committed Jun 14, 2024
1 parent e687fb0 commit 2f47e7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/controller/labelgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"strconv"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions internal/controller/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 2f47e7b

Please sign in to comment.