Skip to content

Commit

Permalink
add namespace specification for pods
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 2e9964c commit 63abbee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
21 changes: 11 additions & 10 deletions internal/controller/labelgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,30 @@ func (r *LabelGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request)
case susqlv1.Aggregating:
r.Logger.V(5).Info("[Reconcile] Entered aggregating case.") // trace
// Get list of pods matching the label group
podNames, namespaceNames, err := r.GetPodNamesMatchingLabels(ctx, labelGroup)
r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] podNames: %s", podNames)) // trace
r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] namespaceNames: %s", namespaceNames)) // trace
// podNames, namespaceNames, err := r.GetPodNamesMatchingLabels(ctx, labelGroup)
// 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
// Get list of pods matching the label group and namespace
podsInNamespace, err := r.filterPodsInNamespace(ctx, labelGroup.Namespace, labelGroup.Status.KubernetesLabels)

var printPodNames []string
for _, pod := range podsInNamespaces {
for _, pod := range podsInNamespace {
printPodNames = append(printPodNames, pod.Name)
}

r.Logger.V(5).Info(fmt.Sprintf("[Namespace] podNames: %s", strings.Join(printPodNames, ", ")))
r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] LabelName: %s", labelGroup.Name))
r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] Namespace: %s", labelGroup.Namespace))
r.Logger.V(5).Info(fmt.Sprintf("[Reconcile] podNamesinNamespace: [%s]", strings.Join(printPodNames, ", ")))

if err != nil || len(podNames) == 0 || len(namespaceNames) == 0 {
if err != nil || len(podsInNamespace) == 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
return ctrl.Result{}, err
}

// Aggregate Kepler measurements for these set of pods
metricValues, err := r.GetMetricValuesForPodNames(r.KeplerMetricName, podNames, namespaceNames)
metricValues, err := r.GetMetricValuesForPodNames(r.KeplerMetricName, podsInNamespace, labelGroup.Namespace)

if err != nil {
r.Logger.V(0).Error(err, "[Reconcile] Querying Prometheus didn't work.")
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/prometheus_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (r *LabelGroupReconciler) GetMostRecentValue(susqlPrometheusQuery string) (
}
}

func (r *LabelGroupReconciler) GetMetricValuesForPodNames(metricName string, podNames []string, namespaceNames []string) (map[string]float64, error) {
func (r *LabelGroupReconciler) GetMetricValuesForPodNames(metricName string, podNames []string, namespaceName string) (map[string]float64, error) {
var roundtripper http.RoundTripper = nil
if strings.HasPrefix(r.KeplerPrometheusUrl, "https://") {
rttls := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
Expand All @@ -114,11 +114,11 @@ func (r *LabelGroupReconciler) GetMetricValuesForPodNames(metricName string, pod
// queryString := fmt.Sprintf("%s{pod_name=~\"%s\",mode=\"dynamic\"}", metricName, strings.Join(podNames, "|"))

// new query for issue 2: can improve runtime efficiency...
queryString := fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"dynamic\"})", metricName, podNames[0], namespaceNames[0])
queryString = queryString + "+" + fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"idle\"})", metricName, podNames[0], namespaceNames[0])
queryString := fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"dynamic\"})", metricName, podNames[0], namespaceName)
queryString = queryString + "+" + fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"idle\"})", metricName, podNames[0], namespaceName)
for i := 1; i < len(podNames); i++ {
queryString = queryString + "+" + fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"dynamic\"})", metricName, podNames[i], namespaceNames[i])
queryString = queryString + "+" + fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"idle\"})", metricName, podNames[i], namespaceNames[i])
queryString = queryString + "+" + fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"dynamic\"})", metricName, podNames[i], namespaceName)
queryString = queryString + "+" + fmt.Sprintf("sum(%s{pod_name=\"%s\",container_namespace=\"%s\",mode=\"idle\"})", metricName, podNames[i], namespaceName)
}

results, warnings, err := v1api.Query(ctx, queryString, time.Now(), v1.WithTimeout(0*time.Second))
Expand Down
8 changes: 6 additions & 2 deletions internal/controller/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
)

func (r *LabelGroupReconciler) filterPodsInNamespace(ctx context.Context, namespace string, labelSelector map[string]string) ([]v1.Pod, error) {
func (r *LabelGroupReconciler) filterPodsInNamespace(ctx context.Context, namespace string, labelSelector map[string]string) ([]string, error) {
// Initialize list options with label selector
listOptions := &client.ListOptions{
Namespace: namespace,
Expand All @@ -40,7 +40,11 @@ func (r *LabelGroupReconciler) filterPodsInNamespace(ctx context.Context, namesp
return nil, err
}

return podList.Items, nil
var podNames []string
for _, pod := range podList.Items {
podNames = append(podNames, pod.Name)
}
return podNames, nil
}

// Functions to get data from the cluster
Expand Down

0 comments on commit 63abbee

Please sign in to comment.