diff --git a/internal/controller/labelgroup_controller.go b/internal/controller/labelgroup_controller.go index a2d25f0..a202217 100644 --- a/internal/controller/labelgroup_controller.go +++ b/internal/controller/labelgroup_controller.go @@ -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.") diff --git a/internal/controller/prometheus_manager.go b/internal/controller/prometheus_manager.go index 55ae59a..f1d0d5b 100644 --- a/internal/controller/prometheus_manager.go +++ b/internal/controller/prometheus_manager.go @@ -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}} @@ -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)) diff --git a/internal/controller/resource_manager.go b/internal/controller/resource_manager.go index 8c6338c..1769f03 100644 --- a/internal/controller/resource_manager.go +++ b/internal/controller/resource_manager.go @@ -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, @@ -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