From a8da7d412c2cd39dc58ef82c6aa2802b9d3e6cdd Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Tue, 23 Feb 2021 09:48:35 +0100
Subject: [PATCH] Enhance the way to configure  overrides

---
 .../kotlin/theodolite/benchmark/Benchmark.kt  |  4 +--
 .../theodolite/benchmark/BenchmarkContext.kt  |  5 ++-
 .../benchmark/KubernetesBenchmark.kt          | 11 ++++---
 .../theodolite/benchmark/TestBenchmark.kt     |  4 +--
 .../theodolite/execution/BenchmarkExecutor.kt |  2 +-
 .../execution/BenchmarkExecutorImpl.kt        |  4 +--
 .../execution/TestBenchmarkExecutorImpl.kt    |  2 +-
 .../theodolite/patcher/PatcherManager.kt      | 32 +++++++++++--------
 .../theodolite/util/ConfigurationOverride.kt  |  6 ++++
 .../util/OverridePatcherDefinition.kt         |  8 -----
 .../src/main/resources/yaml/testContext.yaml  | 20 ++++++++----
 11 files changed, 55 insertions(+), 43 deletions(-)
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt
 delete mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/util/OverridePatcherDefinition.kt

diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt
index 6373262d8..a21d2c747 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt
@@ -1,9 +1,9 @@
 package theodolite.benchmark
 
 import theodolite.util.LoadDimension
-import theodolite.util.OverridePatcherDefinition
+import theodolite.util.ConfigurationOverride
 import theodolite.util.Resource
 
 interface Benchmark {
-    fun buildDeployment(load: LoadDimension, res: Resource, overrides: List<OverridePatcherDefinition>): BenchmarkDeployment
+    fun buildDeployment(load: LoadDimension, res: Resource, configurationOverrides: List<ConfigurationOverride>): BenchmarkDeployment
 }
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkContext.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkContext.kt
index 596f768f7..ef378d98e 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkContext.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkContext.kt
@@ -1,7 +1,6 @@
 package theodolite.benchmark
 
-import theodolite.util.OverridePatcherDefinition
-import theodolite.util.PatcherDefinition
+import theodolite.util.ConfigurationOverride
 import kotlin.properties.Delegates
 
 
@@ -12,7 +11,7 @@ class BenchmarkContext() {
     lateinit var resources: List<Int>
     lateinit var slos: List<Slo>
     lateinit var execution: Execution
-    lateinit var configOverrides: List<OverridePatcherDefinition>
+    lateinit var configOverrides: List<ConfigurationOverride>
 
     class Execution() {
         lateinit var  strategy: String
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt
index fc5515a4a..545d368f5 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt
@@ -32,19 +32,20 @@ class KubernetesBenchmark(): Benchmark {
             }
         }
 
-    override fun buildDeployment(load: LoadDimension, res: Resource, overrides: List<OverridePatcherDefinition>): BenchmarkDeployment {
+    override fun buildDeployment(load: LoadDimension, res: Resource, configurationOverrides: List<ConfigurationOverride>): BenchmarkDeployment {
         // TODO("set node selector")
         val resources = loadKubernetesResources(this.appResource + this.loadGenResource)
         val patcherManager = PatcherManager()
 
+
         // patch res and load
-        patcherManager.applyPatcher(res.getType(), this.resourceTypes, resources, res.get())
-        patcherManager.applyPatcher(load.getType(), this.loadTypes, resources, load.get().toString())
+        patcherManager.createAndApplyPatcher(res.getType(), this.resourceTypes, resources, res.get())
+        patcherManager.createAndApplyPatcher(load.getType(), this.loadTypes, resources, load.get().toString())
 
         // patch overrides
-        overrides.forEach {override ->  patcherManager.applyPatcher(override, resources)}
-        println(zookeeperConfig["server"] !! )
+        configurationOverrides.forEach{ override -> patcherManager.applyPatcher(listOf(override.patcher), resources, override.value)}
 
+        resources.forEach { r -> println(r) }
         return KubernetesBenchmarkDeployment(
             resources.map { r -> r.second },
             kafkaConfig = hashMapOf("bootstrap.servers" to kafkaConfig.bootstrapSever),
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestBenchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestBenchmark.kt
index dc55d61fa..abda15822 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestBenchmark.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/TestBenchmark.kt
@@ -1,7 +1,7 @@
 package theodolite.benchmark
 
 import theodolite.util.LoadDimension
-import theodolite.util.OverridePatcherDefinition
+import theodolite.util.ConfigurationOverride
 import theodolite.util.Resource
 
 class TestBenchmark : Benchmark {
@@ -9,7 +9,7 @@ class TestBenchmark : Benchmark {
     override fun buildDeployment(
         load: LoadDimension,
         res: Resource,
-        override: List<OverridePatcherDefinition>
+        configurationOverride: List<ConfigurationOverride>
     ): BenchmarkDeployment {
         return TestBenchmarkDeployment()
     }
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt
index c78559dd0..6540004e0 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt
@@ -15,7 +15,7 @@ private val logger = KotlinLogging.logger {}
  * @property executionDuration
  * @constructor Create empty Benchmark executor
  */
-abstract class BenchmarkExecutor(val benchmark: Benchmark, val results: Results, val executionDuration: Duration, overrides: List<OverridePatcherDefinition>) {
+abstract class BenchmarkExecutor(val benchmark: Benchmark, val results: Results, val executionDuration: Duration, configurationOverrides: List<ConfigurationOverride>) {
 
     /**
      * Run a experiment for the given parametrization, evaluate the experiment and save the result.
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
index c38dd1c14..ce0295b3f 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
@@ -4,9 +4,9 @@ import theodolite.benchmark.Benchmark
 import theodolite.util.*
 import java.time.Duration
 
-class BenchmarkExecutorImpl(benchmark: Benchmark, results: Results, executionDuration: Duration, val overrides: List<OverridePatcherDefinition>) : BenchmarkExecutor(benchmark, results, executionDuration, overrides) {
+class BenchmarkExecutorImpl(benchmark: Benchmark, results: Results, executionDuration: Duration, val configurationOverrides: List<ConfigurationOverride>) : BenchmarkExecutor(benchmark, results, executionDuration, configurationOverrides) {
     override fun runExperiment(load: LoadDimension, res: Resource): Boolean {
-        val benchmarkDeployment = benchmark.buildDeployment(load, res, this.overrides)
+        val benchmarkDeployment = benchmark.buildDeployment(load, res, this.configurationOverrides)
         benchmarkDeployment.setup()
         this.waitAndLog()
         benchmarkDeployment.teardown()
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TestBenchmarkExecutorImpl.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TestBenchmarkExecutorImpl.kt
index 3b1b6b31d..00afeb18d 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TestBenchmarkExecutorImpl.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TestBenchmarkExecutorImpl.kt
@@ -8,7 +8,7 @@ import java.time.Duration
 
 class TestBenchmarkExecutorImpl(private val mockResults: Array<Array<Boolean>>, benchmark: Benchmark, results: Results):
     BenchmarkExecutor(benchmark, results, executionDuration = Duration.ofSeconds(1),
-        overrides = emptyList()
+        configurationOverrides = emptyList()
     ) {
 
     override fun runExperiment(load: LoadDimension, res: Resource): Boolean {
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt
index b7e35b578..f7875cf39 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt
@@ -1,7 +1,6 @@
 package theodolite.patcher
 
 import io.fabric8.kubernetes.api.model.KubernetesResource
-import theodolite.util.OverridePatcherDefinition
 import theodolite.util.PatcherDefinition
 import theodolite.util.TypeName
 import java.lang.IllegalArgumentException
@@ -23,22 +22,29 @@ class PatcherManager {
             .flatMap { type -> type.patchers}
     }
 
-    fun applyPatcher(type: String, patcherTypes: List<TypeName>, resources: List<Pair<String, KubernetesResource>>, value: Any) {
+    /**
+     * This function first creates a patcher definition and then patches the list of resources based on this patcher definition
+     *
+     * @param type Patcher type, for example "EnvVarPatcher"
+     * @param patcherTypes List of patcher types definitions, for example for resources and threads
+     * @param resources List of K8s resources, a patcher takes the resources that are needed
+     * @param value The value to patch
+     */
+    fun createAndApplyPatcher(type: String, patcherTypes: List<TypeName>, resources: List<Pair<String, KubernetesResource>>, value: Any) {
         this.getPatcherDef(type, patcherTypes)
             .forEach {patcherDef ->
                 createK8sPatcher(patcherDef, resources).patch(value) }
     }
 
+    /**
+     * Patch a resource based on the given patcher definition, a list of resources and a value to patch
+     *
+     * @param patcherDefinition The patcher definition
+     * @param resources List of patcher types definitions, for example for resources and threads
+     * @param value The value to patch
+     */
+    fun applyPatcher(patcherDefinition: List<PatcherDefinition>, resources: List<Pair<String, KubernetesResource>>, value: Any) {
+        patcherDefinition.forEach { def ->  this.createK8sPatcher(def, resources).patch(value) }
 
-   fun applyPatcher(overrides: OverridePatcherDefinition, resources: List<Pair<String, KubernetesResource>>){
-       var pdef = PatcherDefinition()
-       pdef.type = overrides.type
-       pdef.container = overrides.container
-       pdef.resource = overrides.resource
-       overrides.overrides.forEach{ override ->
-           pdef.variableName = override.key
-           this.createK8sPatcher(pdef, resources).patch(override.value)
-       }
-   }
-
+    }
 }
\ No newline at end of file
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt
new file mode 100644
index 000000000..f9099929c
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt
@@ -0,0 +1,6 @@
+package theodolite.util
+
+class ConfigurationOverride() {
+    lateinit var patcher: PatcherDefinition
+    lateinit var value: String
+}
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/OverridePatcherDefinition.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/OverridePatcherDefinition.kt
deleted file mode 100644
index c996fbe90..000000000
--- a/theodolite-quarkus/src/main/kotlin/theodolite/util/OverridePatcherDefinition.kt
+++ /dev/null
@@ -1,8 +0,0 @@
-package theodolite.util
-
-class OverridePatcherDefinition() {
-    lateinit var type: String
-    lateinit var resource: String
-    lateinit var container: String
-    lateinit var overrides: Map<String, String>
-}
diff --git a/theodolite-quarkus/src/main/resources/yaml/testContext.yaml b/theodolite-quarkus/src/main/resources/yaml/testContext.yaml
index 46ba710c8..289d15bbe 100644
--- a/theodolite-quarkus/src/main/resources/yaml/testContext.yaml
+++ b/theodolite-quarkus/src/main/resources/yaml/testContext.yaml
@@ -15,10 +15,18 @@ execution:
   repititions: 1
   restrictions:
     - "LowerBound"
+#configOverrides:
+#  - type: "EnvVarPatcher"
+#    resource: "workloadGenerator.yaml"
+#    container: "workload-generator"
+#    overrides:
+#      overrideTestA: "8888"
+#      overrideTestB: "6666"
 configOverrides:
-  - type: "EnvVarPatcher"
-    resource: "workloadGenerator.yaml"
-    container: "workload-generator"
-    overrides:
-      overrideTestA: "8888"
-      overrideTestB: "6666"
\ No newline at end of file
+  - patcher:
+      type: "EnvVarPatcher"
+      resource: "workloadGenerator.yaml"
+      container: "workload-generator"
+      variableName: "KAFKA_SERVER"
+    value: "localhost:9192"
+
-- 
GitLab