scheduler_perf: refactor common code

This commit is contained in:
Patrick Ohly 2023-01-25 20:38:04 +01:00
parent 5f23f83587
commit 969d28b12b

View File

@ -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