From 039321dc11bfe970a10d5de1d222259ddbe7df30 Mon Sep 17 00:00:00 2001
From: Simon Ehrenstein <stu200776@mail.uni-kiel.de>
Date: Fri, 19 Mar 2021 11:15:19 +0000
Subject: [PATCH] Allow using the random scheduler in benchmarks

---
 .../infrastructure/random-scheduler/schedule.sh      | 11 +++++++++--
 .../main/kotlin/theodolite/patcher/PatcherFactory.kt |  1 +
 .../theodolite/patcher/SchedulerNamePatcher.kt       | 12 ++++++++++++
 .../src/main/resources/yaml/BenchmarkExecution.yaml  |  6 +++++-
 4 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/SchedulerNamePatcher.kt

diff --git a/execution/infrastructure/random-scheduler/schedule.sh b/execution/infrastructure/random-scheduler/schedule.sh
index e2e10c0ab..06745354d 100755
--- a/execution/infrastructure/random-scheduler/schedule.sh
+++ b/execution/infrastructure/random-scheduler/schedule.sh
@@ -8,11 +8,18 @@ while true;
 do
     for PODNAME in $(kubectl get pods -n $TARGET_NAMESPACE -o json | jq '.items[] | select(.spec.schedulerName == "random-scheduler") | select(.spec.nodeName == null) | .metadata.name' | tr -d '"');
     do
-        NODES=($(kubectl get nodes -o json | jq '.items[].metadata.name' | tr -d '"'))
+        NODE_SELECTOR=$(kubectl get pod $PODNAME -n $TARGET_NAMESPACE -o json | jq -S 'if .spec.nodeSelector != null then .spec.nodeSelector else {} end')
+        NODES=($(kubectl get nodes -o json | jq --argjson nodeSelector "$NODE_SELECTOR" '.items[] | select(.metadata.labels | contains($nodeSelector)) | .metadata.name' | tr -d '"'))
         NUMNODES=${#NODES[@]}
+        if [ $NUMNODES -eq 0 ]; then
+            echo "No nodes found matching the node selector: $NODE_SELECTOR from pod $PODNAME"
+            echo "Pod $PODNAME cannot be scheduled."
+            continue;
+        fi
+        echo "Found $NUM_NODES suitable nodes for pod $PODNAME"
         CHOSEN=${NODES[$[$RANDOM % $NUMNODES]]}
         curl --header "Content-Type:application/json" --request POST --data '{"apiVersion":"v1", "kind": "Binding", "metadata": {"name": "'$PODNAME'"}, "target": {"apiVersion": "v1", "kind": "Node", "name": "'$CHOSEN'"}}' localhost:8080/api/v1/namespaces/$TARGET_NAMESPACE/pods/$PODNAME/binding/
         echo "Assigned $PODNAME to $CHOSEN"
     done
     sleep 1
-done
\ No newline at end of file
+done
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
index 1a3e56bfe..dd391c599 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
@@ -22,6 +22,7 @@ class PatcherFactory {
                 patcherDefinition.container,
                 patcherDefinition.variableName
             )
+            "SchedulerNamePatcher" -> SchedulerNamePatcher(resource)
             else -> throw IllegalArgumentException("Patcher type ${patcherDefinition.type} not found")
         }
     }
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/SchedulerNamePatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/SchedulerNamePatcher.kt
new file mode 100644
index 000000000..88d9a978f
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/SchedulerNamePatcher.kt
@@ -0,0 +1,12 @@
+package theodolite.patcher
+
+import io.fabric8.kubernetes.api.model.KubernetesResource
+import io.fabric8.kubernetes.api.model.apps.Deployment
+
+class SchedulerNamePatcher(private val k8sResource: KubernetesResource): Patcher {
+    override fun <String> patch(value: String) {
+        if (k8sResource is Deployment) {
+            k8sResource.spec.template.spec.schedulerName = value as kotlin.String;
+        }
+    }
+}
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml b/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml
index a91d12362..94c671008 100644
--- a/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml
+++ b/theodolite-quarkus/src/main/resources/yaml/BenchmarkExecution.yaml
@@ -43,4 +43,8 @@ configOverrides:
       resource: "uc1-kstreams-deployment.yaml"
       container: "uc-application"
       variableName: "memory"
-    value: "2Gi"
\ No newline at end of file
+    value: "2Gi"
+  - patcher:
+      type: "SchedulerNamePatcher"
+      resource: "uc1-kstreams-deployment.yaml"
+    value: "random-scheduler"
\ No newline at end of file
-- 
GitLab