Skip to content

Commit

Permalink
include namespace with pod serch to resolve issue 2
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Trent <[email protected]>
  • Loading branch information
trent-s committed Mar 12, 2024
1 parent 5ffb938 commit 5e87d39
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions internal/controller/labelgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ func (r *LabelGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request)

case susql.Aggregating:
// Get list of pods matching the label group
podNames, err := r.GetPodNamesMatchingLabels(ctx, labelGroup)
podNames, namespaceNames, err := r.GetPodNamesMatchingLabels(ctx, labelGroup)

if err != nil {
fmt.Printf("ERROR [Reconcile]: Couldn't get pods for the labels provided\n")
return ctrl.Result{}, err
}

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

if err != nil {
fmt.Printf("ERROR [Reconcile]: Querying Prometheus didn't work: %v\n", err)
Expand Down
21 changes: 18 additions & 3 deletions internal/controller/prometheus_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func (r *LabelGroupReconciler) GetMostRecentValue(susqlPrometheusQuery string) (
}
}

func (r *LabelGroupReconciler) GetMetricValuesForPodNames(metricName string, podNames []string) (map[string]float64, error) {
func (r *LabelGroupReconciler) GetMetricValuesForPodNames(metricName string, podNames []string, namespaceNames[]string) (map[string]float64, error) {
if len(podNames) == 0 {
fmt.Printf("ERROR [GetMetricValuesForPodNames]: No pods under observation. Currently len(podNames)=0.\n")
return nil, nil
}

var roundtripper http.RoundTripper = nil
if strings.HasPrefix(r.KeplerPrometheusUrl, "https://") {
rttls := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
Expand All @@ -108,20 +113,30 @@ func (r *LabelGroupReconciler) GetMetricValuesForPodNames(metricName string, pod
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

queryString := fmt.Sprintf("%s{pod_name=~\"%s\",mode=\"dynamic\"}", metricName, strings.Join(podNames, "|"))
/* original query */
/* oldQueryString := 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])
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])
}

results, warnings, err := v1api.Query(ctx, queryString, time.Now(), v1.WithTimeout(0*time.Second))

if err != nil {
if err != nil || results == nil {
fmt.Printf("ERROR [GetMetricValuesForPodNames]: Querying Prometheus didn't work: %v\n", err)
fmt.Printf("metricName: %s\n", metricName)
fmt.Printf("KeplerPrometheusUrl: %s\n", r.KeplerPrometheusUrl)
fmt.Printf("queryString: %s\n", queryString)
return nil, err
}

if len(warnings) > 0 {
fmt.Printf("WARNING [GetMetricValuesForPodNames]: %v\n", warnings)
fmt.Printf("metricName: %s\n", metricName)
fmt.Printf("KeplerPrometheusUrl: %s\n", r.KeplerPrometheusUrl)
fmt.Printf("queryString: %s\n", queryString)
}

metricValues := make(map[string]float64, len(results.(model.Vector)))
Expand Down
11 changes: 5 additions & 6 deletions internal/controller/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,27 @@ package controller
import (
"context"

"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"

susql "github.com/sustainable-computing-io/susql-operator/api/v1"
v1 "k8s.io/api/core/v1"
)

// Functions to get data from the cluster
func (r *LabelGroupReconciler) GetPodNamesMatchingLabels(ctx context.Context, labelGroup *susql.LabelGroup) ([]string, error) {
func (r *LabelGroupReconciler) GetPodNamesMatchingLabels(ctx context.Context, labelGroup *susql.LabelGroup) ([]string, []string, error) {
pods := &v1.PodList{}

if err := r.List(ctx, pods, client.UnsafeDisableDeepCopy, (client.MatchingLabels)(labelGroup.Status.KubernetesLabels)); err != nil {
return nil, err
return nil, nil, err
}

var podNames []string
var namespaceNames []string

for _, pod := range pods.Items {
podNames = append(podNames, pod.Name)
fmt.Printf("%+v\n", pod)
namespaceNames = append(namespaceNames, pod.Namespace)
}

return podNames, nil
return podNames, namespaceNames, nil
}

0 comments on commit 5e87d39

Please sign in to comment.