From 5e8cc5eb55d83ff71474e75ac5ca0cb88b815d79 Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Mon, 16 May 2022 09:34:18 +0200
Subject: [PATCH] change rolloutMode to waitForResourcesEnabled flag

---
 docs/api-reference/crds.md                          |  9 ++++-----
 theodolite/crd/crd-benchmark.yaml                   | 10 ++++------
 .../theodolite/benchmark/KubernetesBenchmark.kt     |  7 ++++---
 .../benchmark/KubernetesBenchmarkDeployment.kt      |  2 +-
 .../kotlin/theodolite/benchmark/RolloutManager.kt   | 13 +++++++------
 .../main/kotlin/theodolite/benchmark/RolloutMode.kt | 10 ----------
 .../execution/operator/BenchmarkCRDummy.kt          |  3 +--
 .../k8s-resource-files/test-benchmark.yaml          |  2 +-
 8 files changed, 22 insertions(+), 34 deletions(-)
 delete mode 100644 theodolite/src/main/kotlin/theodolite/benchmark/RolloutMode.kt

diff --git a/docs/api-reference/crds.md b/docs/api-reference/crds.md
index 3dd872522..2214691f2 100644
--- a/docs/api-reference/crds.md
+++ b/docs/api-reference/crds.md
@@ -138,13 +138,12 @@ Resource Types:
         </td>
         <td>false</td>
       </tr><tr>
-        <td><b>rolloutMode</b></td>
-        <td>enum</td>
+        <td><b>waitForResourcesEnabled</b></td>
+        <td>boolean</td>
         <td>
+          If true, Theodolite waits to create the resource for the SUT until the infrastructure resources are ready, and analogously, Theodolite waits to create the load-gen resource until the resources of the SUT are ready.<br/>
           <br/>
-          <br/>
-            <i>Enum</i>: no-waiting, default<br/>
-            <i>Default</i>: default<br/>
+            <i>Default</i>: false<br/>
         </td>
         <td>false</td>
       </tr><tr>
diff --git a/theodolite/crd/crd-benchmark.yaml b/theodolite/crd/crd-benchmark.yaml
index 9a728b22d..d2418ee00 100644
--- a/theodolite/crd/crd-benchmark.yaml
+++ b/theodolite/crd/crd-benchmark.yaml
@@ -26,12 +26,10 @@ spec:
                 description: This field exists only for technical reasons and should not be set by the user. The value of the field will be overwritten.
                 type: string
                 default: ""
-              rolloutMode:
-                  type: string
-                  default: "default"
-                  enum:
-                  - "no-waiting"
-                  - "default"
+              waitForResourcesEnabled:
+                description: If true, Theodolite waits to create the resource for the SUT until the infrastructure resources are ready, and analogously, Theodolite waits to create the load-gen resource until the resources of the SUT are ready.
+                type: boolean
+                default: false
               infrastructure:
                 description: (Optional) A list of file names that reference Kubernetes resources that are deployed on the cluster to create the required infrastructure.
                 type: object
diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt
index 0abe82432..6e8048547 100644
--- a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt
+++ b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt
@@ -10,6 +10,7 @@ import mu.KotlinLogging
 import theodolite.k8s.K8sManager
 import theodolite.patcher.PatcherFactory
 import theodolite.util.*
+import kotlin.properties.Delegates
 
 
 private val logger = KotlinLogging.logger {}
@@ -37,7 +38,7 @@ private var DEFAULT_THEODOLITE_APP_RESOURCES = "./benchmark-resources"
 @RegisterForReflection
 class KubernetesBenchmark : KubernetesResource, Benchmark {
     lateinit var name: String
-    lateinit var rolloutMode: RolloutMode
+    var waitForResourcesEnabled = false
     lateinit var resourceTypes: List<TypeName>
     lateinit var loadTypes: List<TypeName>
     var kafkaConfig: KafkaConfig? = null
@@ -65,7 +66,7 @@ class KubernetesBenchmark : KubernetesResource, Benchmark {
 
     override fun setupInfrastructure() {
         this.infrastructure.beforeActions.forEach { it.exec(client = client) }
-        RolloutManager(rolloutMode, this.client)
+        RolloutManager(waitForResourcesEnabled, this.client)
             .rollout(loadResources(this.infrastructure.resources).map { it.second })
     }
 
@@ -129,7 +130,7 @@ class KubernetesBenchmark : KubernetesResource, Benchmark {
             kafkaConfig = if (kafkaConfig != null) mapOf("bootstrap.servers" to kafkaConfig.bootstrapServer) else mapOf(),
             topics = kafkaConfig?.topics ?: listOf(),
             client = this.client,
-            rolloutMode = rolloutMode
+            rolloutMode = waitForResourcesEnabled
 
         )
     }
diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt
index d6ba65b37..1d7b22233 100644
--- a/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt
+++ b/theodolite/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt
@@ -28,7 +28,7 @@ class KubernetesBenchmarkDeployment(
     private val sutAfterActions: List<Action>,
     private val loadGenBeforeActions: List<Action>,
     private val loadGenAfterActions: List<Action>,
-    private val rolloutMode: RolloutMode,
+    private val rolloutMode: Boolean,
     val appResources: List<HasMetadata>,
     val loadGenResources: List<HasMetadata>,
     private val loadGenerationDelay: Long,
diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/RolloutManager.kt b/theodolite/src/main/kotlin/theodolite/benchmark/RolloutManager.kt
index 203ced651..f282fb279 100644
--- a/theodolite/src/main/kotlin/theodolite/benchmark/RolloutManager.kt
+++ b/theodolite/src/main/kotlin/theodolite/benchmark/RolloutManager.kt
@@ -1,23 +1,25 @@
 package theodolite.benchmark
 
 import io.fabric8.kubernetes.api.model.HasMetadata
+import io.fabric8.kubernetes.api.model.Pod
 import io.fabric8.kubernetes.api.model.apps.DaemonSet
 import io.fabric8.kubernetes.api.model.apps.Deployment
 import io.fabric8.kubernetes.api.model.apps.ReplicaSet
 import io.fabric8.kubernetes.api.model.apps.StatefulSet
+import io.fabric8.kubernetes.api.model.batch.v1.Job
 import io.fabric8.kubernetes.client.NamespacedKubernetesClient
 import theodolite.k8s.K8sManager
 
 private var SLEEP_TIME_MS = 500L
 
 
-class RolloutManager(private val rolloutMode: RolloutMode, private val client: NamespacedKubernetesClient) {
+class RolloutManager(private val blockUntilResourcesReady: Boolean, private val client: NamespacedKubernetesClient) {
 
     fun rollout(resources: List<HasMetadata>) {
         resources
             .forEach { K8sManager(client).deploy(it) }
 
-        if (rolloutMode == RolloutMode.DEFAULT) {
+        if (blockUntilResourcesReady) {
             resources
                 .forEach {
                     when (it) {
@@ -25,16 +27,15 @@ class RolloutManager(private val rolloutMode: RolloutMode, private val client: N
                         is StatefulSet -> waitFor { client.apps().statefulSets().withName(it.metadata.name).isReady }
                         is DaemonSet -> waitFor { client.apps().daemonSets().withName(it.metadata.name).isReady }
                         is ReplicaSet -> waitFor { client.apps().replicaSets().withName(it.metadata.name).isReady }
+                        is Job -> waitFor { client.batch().v1().cronjobs().withName(it.metadata.name).isReady }
                     }
                 }
         }
     }
 
-
-    private fun waitFor(f: () -> Boolean) {
-        while (!f()) {
+    private fun waitFor(isResourceReady: () -> Boolean) {
+        while (!isResourceReady()) {
             Thread.sleep(SLEEP_TIME_MS)
-            println("wait for resource")
         }
     }
 
diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/RolloutMode.kt b/theodolite/src/main/kotlin/theodolite/benchmark/RolloutMode.kt
deleted file mode 100644
index 052847c66..000000000
--- a/theodolite/src/main/kotlin/theodolite/benchmark/RolloutMode.kt
+++ /dev/null
@@ -1,10 +0,0 @@
-package theodolite.benchmark
-
-import com.fasterxml.jackson.annotation.JsonValue
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize
-
-@JsonDeserialize
-enum class RolloutMode(@JsonValue val value: String) {
-    NONE("no-waiting"),
-    DEFAULT("default")
-}
\ No newline at end of file
diff --git a/theodolite/src/test/kotlin/theodolite/execution/operator/BenchmarkCRDummy.kt b/theodolite/src/test/kotlin/theodolite/execution/operator/BenchmarkCRDummy.kt
index bad9773d3..d68414291 100644
--- a/theodolite/src/test/kotlin/theodolite/execution/operator/BenchmarkCRDummy.kt
+++ b/theodolite/src/test/kotlin/theodolite/execution/operator/BenchmarkCRDummy.kt
@@ -2,7 +2,6 @@ package theodolite.execution.operator
 
 import theodolite.benchmark.KubernetesBenchmark
 import theodolite.benchmark.Resources
-import theodolite.benchmark.RolloutMode
 import theodolite.model.crd.BenchmarkCRD
 import theodolite.util.KafkaConfig
 
@@ -26,7 +25,7 @@ class BenchmarkCRDummy(name: String) {
         benchmarkCR.metadata.name = name
         benchmarkCR.kind = "Benchmark"
         benchmarkCR.apiVersion = "v1"
-        benchmark.rolloutMode = RolloutMode.DEFAULT
+        benchmark.waitForResourcesEnabled = false
 
         benchmark.infrastructure = Resources()
         benchmark.sut = Resources()
diff --git a/theodolite/src/test/resources/k8s-resource-files/test-benchmark.yaml b/theodolite/src/test/resources/k8s-resource-files/test-benchmark.yaml
index e45ecb222..102a6a249 100644
--- a/theodolite/src/test/resources/k8s-resource-files/test-benchmark.yaml
+++ b/theodolite/src/test/resources/k8s-resource-files/test-benchmark.yaml
@@ -3,7 +3,7 @@ kind: benchmark
 metadata:
   name: example-benchmark
 spec:
-  rolloutMode: "default"
+  waitForResourcesEnabled: false
   sut:
     resources:
       - configMap:
-- 
GitLab