From 969d28b12b7402e760c6acaa65f2e6886bb3e261 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 25 Jan 2023 20:38:04 +0100 Subject: [PATCH] scheduler_perf: refactor common code --- .../scheduler_perf/scheduler_perf_test.go | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/test/integration/scheduler_perf/scheduler_perf_test.go b/test/integration/scheduler_perf/scheduler_perf_test.go index e560f5a8a03..6f0f39bf6dc 100644 --- a/test/integration/scheduler_perf/scheduler_perf_test.go +++ b/test/integration/scheduler_perf/scheduler_perf_test.go @@ -758,6 +758,7 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [ // numPodsScheduledPerNamespace has all namespaces created in workload and the number of pods they (will) have. // All namespaces listed in numPodsScheduledPerNamespace will be cleaned up. numPodsScheduledPerNamespace := make(map[string]int) + b.Cleanup(func() { for namespace := range numPodsScheduledPerNamespace { if err := client.CoreV1().Namespaces().Delete(ctx, namespace, metav1.DeleteOptions{}); err != nil { @@ -816,15 +817,7 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [ if concreteOp.Namespace != nil { namespace = *concreteOp.Namespace } - if _, ok := numPodsScheduledPerNamespace[namespace]; !ok { - // The namespace has not created yet. - // So, creat that and register it to numPodsScheduledPerNamespace. - _, err := client.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, metav1.CreateOptions{}) - if err != nil { - b.Fatalf("failed to create namespace for Pod: %v", namespace) - } - numPodsScheduledPerNamespace[namespace] = 0 - } + createNamespaceIfNotPresent(ctx, b, client, namespace, &numPodsScheduledPerNamespace) if concreteOp.PodTemplatePath == nil { concreteOp.PodTemplatePath = tc.DefaultPodTemplatePath } @@ -1020,6 +1013,18 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [ return dataItems } +func createNamespaceIfNotPresent(ctx context.Context, b *testing.B, client clientset.Interface, namespace string, podsPerNamespace *map[string]int) { + if _, ok := (*podsPerNamespace)[namespace]; !ok { + // The namespace has not created yet. + // So, create that and register it. + _, err := client.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, metav1.CreateOptions{}) + if err != nil { + b.Fatalf("failed to create namespace for Pod: %v", namespace) + } + (*podsPerNamespace)[namespace] = 0 + } +} + type testDataCollector interface { run(ctx context.Context) collect() []DataItem