From fdbf21e03a9a84d7c093bab25df6212aae573235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Skocze=C5=84?= Date: Mon, 30 Sep 2024 13:32:12 +0000 Subject: [PATCH] Allow to filter pods using labels while collecting metrics in scheduler_perf --- .../scheduler_perf/scheduler_perf.go | 16 ++++++++++------ test/integration/scheduler_perf/util.go | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/test/integration/scheduler_perf/scheduler_perf.go b/test/integration/scheduler_perf/scheduler_perf.go index a90d40ca729..3911c9c5619 100644 --- a/test/integration/scheduler_perf/scheduler_perf.go +++ b/test/integration/scheduler_perf/scheduler_perf.go @@ -871,6 +871,10 @@ type startCollectingMetricsOp struct { Name string // Namespaces for which the scheduling throughput metric is calculated. Namespaces []string + // Labels used to filter the pods for which the scheduling throughput metric is collected. + // If empty, it will collect the metric for all pods in the selected namespaces. + // Optional. + LabelSelector map[string]string } func (scm *startCollectingMetricsOp) isValid(_ bool) error { @@ -1227,12 +1231,12 @@ func checkEmptyInFlightEvents() error { return nil } -func startCollectingMetrics(tCtx ktesting.TContext, collectorWG *sync.WaitGroup, podInformer coreinformers.PodInformer, mcc *metricsCollectorConfig, throughputErrorMargin float64, opIndex int, name string, namespaces []string) (ktesting.TContext, []testDataCollector) { +func startCollectingMetrics(tCtx ktesting.TContext, collectorWG *sync.WaitGroup, podInformer coreinformers.PodInformer, mcc *metricsCollectorConfig, throughputErrorMargin float64, opIndex int, name string, namespaces []string, labelSelector map[string]string) (ktesting.TContext, []testDataCollector) { collectorCtx := ktesting.WithCancel(tCtx) workloadName := tCtx.Name() // The first part is the same for each workload, therefore we can strip it. workloadName = workloadName[strings.Index(name, "/")+1:] - collectors := getTestDataCollectors(podInformer, fmt.Sprintf("%s/%s", workloadName, name), namespaces, mcc, throughputErrorMargin) + collectors := getTestDataCollectors(podInformer, fmt.Sprintf("%s/%s", workloadName, name), namespaces, labelSelector, mcc, throughputErrorMargin) for _, collector := range collectors { // Need loop-local variable for function below. collector := collector @@ -1373,7 +1377,7 @@ func runWorkload(tCtx ktesting.TContext, tc *testCase, w *workload, informerFact if collectorCtx != nil { tCtx.Fatalf("op %d: Metrics collection is overlapping. Probably second collector was started before stopping a previous one", opIndex) } - collectorCtx, collectors = startCollectingMetrics(tCtx, &collectorWG, podInformer, tc.MetricsCollectorConfig, throughputErrorMargin, opIndex, namespace, []string{namespace}) + collectorCtx, collectors = startCollectingMetrics(tCtx, &collectorWG, podInformer, tc.MetricsCollectorConfig, throughputErrorMargin, opIndex, namespace, []string{namespace}, nil) defer collectorCtx.Cancel("cleaning up") } if err := createPodsRapidly(tCtx, namespace, concreteOp); err != nil { @@ -1584,7 +1588,7 @@ func runWorkload(tCtx ktesting.TContext, tc *testCase, w *workload, informerFact if collectorCtx != nil { tCtx.Fatalf("op %d: Metrics collection is overlapping. Probably second collector was started before stopping a previous one", opIndex) } - collectorCtx, collectors = startCollectingMetrics(tCtx, &collectorWG, podInformer, tc.MetricsCollectorConfig, throughputErrorMargin, opIndex, concreteOp.Name, concreteOp.Namespaces) + collectorCtx, collectors = startCollectingMetrics(tCtx, &collectorWG, podInformer, tc.MetricsCollectorConfig, throughputErrorMargin, opIndex, concreteOp.Name, concreteOp.Namespaces, concreteOp.LabelSelector) defer collectorCtx.Cancel("cleaning up") case *stopCollectingMetricsOp: @@ -1633,12 +1637,12 @@ type testDataCollector interface { collect() []DataItem } -func getTestDataCollectors(podInformer coreinformers.PodInformer, name string, namespaces []string, mcc *metricsCollectorConfig, throughputErrorMargin float64) []testDataCollector { +func getTestDataCollectors(podInformer coreinformers.PodInformer, name string, namespaces []string, labelSelector map[string]string, mcc *metricsCollectorConfig, throughputErrorMargin float64) []testDataCollector { if mcc == nil { mcc = &defaultMetricsCollectorConfig } return []testDataCollector{ - newThroughputCollector(podInformer, map[string]string{"Name": name}, namespaces, throughputErrorMargin), + newThroughputCollector(podInformer, map[string]string{"Name": name}, labelSelector, namespaces, throughputErrorMargin), newMetricsCollector(mcc, map[string]string{"Name": name}), } } diff --git a/test/integration/scheduler_perf/util.go b/test/integration/scheduler_perf/util.go index 48d4a2f9452..551fd287d71 100644 --- a/test/integration/scheduler_perf/util.go +++ b/test/integration/scheduler_perf/util.go @@ -405,7 +405,8 @@ func collectHistogramVec(metric string, labels map[string]string, lvMap map[stri type throughputCollector struct { podInformer coreinformers.PodInformer schedulingThroughputs []float64 - labels map[string]string + labelSelector map[string]string + resultLabels map[string]string namespaces sets.Set[string] errorMargin float64 @@ -413,12 +414,13 @@ type throughputCollector struct { start time.Time } -func newThroughputCollector(podInformer coreinformers.PodInformer, labels map[string]string, namespaces []string, errorMargin float64) *throughputCollector { +func newThroughputCollector(podInformer coreinformers.PodInformer, resultLabels map[string]string, labelSelector map[string]string, namespaces []string, errorMargin float64) *throughputCollector { return &throughputCollector{ - podInformer: podInformer, - labels: labels, - namespaces: sets.New(namespaces...), - errorMargin: errorMargin, + podInformer: podInformer, + labelSelector: labelSelector, + resultLabels: resultLabels, + namespaces: sets.New(namespaces...), + errorMargin: errorMargin, } } @@ -451,7 +453,7 @@ func (tc *throughputCollector) run(tCtx ktesting.TContext) { return } - if !tc.namespaces.Has(newPod.Namespace) { + if !tc.namespaces.Has(newPod.Namespace) || !labelsMatch(newPod.Labels, tc.labelSelector) { return } @@ -577,7 +579,7 @@ func (tc *throughputCollector) run(tCtx ktesting.TContext) { func (tc *throughputCollector) collect() []DataItem { throughputSummary := DataItem{ - Labels: tc.labels, + Labels: tc.resultLabels, progress: tc.progress, start: tc.start, }