From dcab3da45f4803b491b6cc9a4217addcb9dc1786 Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Mon, 15 Nov 2021 15:14:01 +0100
Subject: [PATCH] Check if Theodolite is running in operator mode before
 creating K8s events

---
 .../execution/BenchmarkExecutorImpl.kt        | 55 ++++++++++---------
 .../theodolite/execution/ExecutionModes.kt    |  7 +++
 .../main/kotlin/theodolite/execution/Main.kt  | 10 ++--
 .../execution/operator/EventCreator.kt        |  1 -
 .../operator/TheodoliteController.kt          |  3 +-
 .../kotlin/theodolite/util/Configuration.kt   |  3 +
 6 files changed, 47 insertions(+), 32 deletions(-)
 create mode 100644 theodolite/src/main/kotlin/theodolite/execution/ExecutionModes.kt

diff --git a/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt b/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
index 430921275..5c07c7034 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
@@ -6,10 +6,7 @@ import theodolite.benchmark.Benchmark
 import theodolite.benchmark.BenchmarkExecution
 import theodolite.evaluation.AnalysisExecutor
 import theodolite.execution.operator.EventCreator
-import theodolite.util.ConfigurationOverride
-import theodolite.util.LoadDimension
-import theodolite.util.Resource
-import theodolite.util.Results
+import theodolite.util.*
 import java.time.Duration
 import java.time.Instant
 
@@ -40,6 +37,7 @@ class BenchmarkExecutorImpl(
     executionName
 ) {
     private val eventCreator = EventCreator()
+    private val mode = Configuration.EXECUTION_MODE
 
     override fun runExperiment(load: LoadDimension, res: Resource): Boolean {
         var result = false
@@ -82,39 +80,46 @@ class BenchmarkExecutorImpl(
         try {
             benchmarkDeployment.setup()
             this.waitAndLog()
-            eventCreator.createEvent(
-                executionName = executionName,
-                type = "NORMAL",
-                reason = "Start experiment",
-                message = "load: ${load.get()}, resources: ${res.get()}")
+            if (mode == ExecutionModes.OPERATOR.value) {
+                eventCreator.createEvent(
+                    executionName = executionName,
+                    type = "NORMAL",
+                    reason = "Start experiment",
+                    message = "load: ${load.get()}, resources: ${res.get()}")
+            }
         } catch (e: Exception) {
             logger.error { "Error while setup experiment." }
             logger.error { "Error is: $e" }
             this.run.set(false)
 
-            eventCreator.createEvent(
-                executionName = executionName,
-                type = "WARNING",
-                reason = "Start experiment failed",
-                message = "load: ${load.get()}, resources: ${res.get()}")
+            if (mode == ExecutionModes.OPERATOR.value) {
+                eventCreator.createEvent(
+                    executionName = executionName,
+                    type = "WARNING",
+                    reason = "Start experiment failed",
+                    message = "load: ${load.get()}, resources: ${res.get()}")
+            }
         }
         val to = Instant.now()
         try {
             benchmarkDeployment.teardown()
-            eventCreator.createEvent(
-                executionName = executionName,
-                type = "NORMAL",
-                reason = "Stop experiment",
-                message = "Teardown complete")
+            if (mode == ExecutionModes.OPERATOR.value) {
+                eventCreator.createEvent(
+                    executionName = executionName,
+                    type = "NORMAL",
+                    reason = "Stop experiment",
+                    message = "Teardown complete")
+            }
         } catch (e: Exception) {
             logger.warn { "Error while tearing down the benchmark deployment." }
             logger.debug { "Teardown failed, caused by: $e" }
-
-            eventCreator.createEvent(
-                executionName = executionName,
-                type = "WARNING",
-                reason = "Stop experiment failed",
-                message = "Teardown failed: ${e.message}")
+            if (mode == ExecutionModes.OPERATOR.value) {
+                eventCreator.createEvent(
+                    executionName = executionName,
+                    type = "WARNING",
+                    reason = "Stop experiment failed",
+                    message = "Teardown failed: ${e.message}")
+            }
         }
         return Pair(from, to)
     }
diff --git a/theodolite/src/main/kotlin/theodolite/execution/ExecutionModes.kt b/theodolite/src/main/kotlin/theodolite/execution/ExecutionModes.kt
new file mode 100644
index 000000000..bf947be01
--- /dev/null
+++ b/theodolite/src/main/kotlin/theodolite/execution/ExecutionModes.kt
@@ -0,0 +1,7 @@
+package theodolite.execution
+
+enum class ExecutionModes(val value: String) {
+    OPERATOR("operator"),
+    YAML_EXECUTOR("yaml-executor"),
+    STANDALONE("standalone")
+}
\ No newline at end of file
diff --git a/theodolite/src/main/kotlin/theodolite/execution/Main.kt b/theodolite/src/main/kotlin/theodolite/execution/Main.kt
index 7d5fca859..11f696ddd 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/Main.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/Main.kt
@@ -3,6 +3,7 @@ package theodolite.execution
 import io.quarkus.runtime.annotations.QuarkusMain
 import mu.KotlinLogging
 import theodolite.execution.operator.TheodoliteOperator
+import theodolite.util.Configuration
 import kotlin.system.exitProcess
 
 private val logger = KotlinLogging.logger {}
@@ -13,13 +14,12 @@ object Main {
     @JvmStatic
     fun main(args: Array<String>) {
 
-        val mode = System.getenv("MODE") ?: "standalone"
+        val mode = Configuration.EXECUTION_MODE
         logger.info { "Start Theodolite with mode $mode" }
 
-        when (mode) {
-            "standalone" -> TheodoliteStandalone().start()
-            "yaml-executor" -> TheodoliteStandalone().start() // TODO remove (#209)
-            "operator" -> TheodoliteOperator().start()
+        when (mode.toLowerCase()) {
+            ExecutionModes.STANDALONE.value, ExecutionModes.YAML_EXECUTOR.value -> TheodoliteStandalone().start()  // TODO remove standalone (#209)
+            ExecutionModes.OPERATOR.value -> TheodoliteOperator().start()
             else -> {
                 logger.error { "MODE $mode not found" }
                 exitProcess(1)
diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt
index 69b21155d..fab098ebd 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt
@@ -6,7 +6,6 @@ import io.fabric8.kubernetes.api.model.ObjectReference
 import io.fabric8.kubernetes.client.DefaultKubernetesClient
 import io.fabric8.kubernetes.client.NamespacedKubernetesClient
 import mu.KotlinLogging
-import theodolite.util.Config
 import theodolite.util.Configuration
 import java.time.Instant
 import java.util.*
diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteController.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteController.kt
index 98bb4ed2a..131093a43 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteController.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteController.kt
@@ -5,6 +5,7 @@ import io.fabric8.kubernetes.client.dsl.Resource
 import mu.KotlinLogging
 import theodolite.benchmark.BenchmarkExecution
 import theodolite.benchmark.KubernetesBenchmark
+import theodolite.execution.ExecutionModes
 import theodolite.execution.TheodoliteExecutor
 import theodolite.model.crd.*
 import theodolite.patcher.ConfigOverrideModifier
@@ -96,7 +97,7 @@ class TheodoliteController(
                 }
             }
         } catch (e: Exception) {
-            EventCreator().createEvent(
+                EventCreator().createEvent(
                 executionName = execution.name,
                 type = "WARNING",
                 reason = "Execution failed",
diff --git a/theodolite/src/main/kotlin/theodolite/util/Configuration.kt b/theodolite/src/main/kotlin/theodolite/util/Configuration.kt
index 5bc1edd19..dac3b943e 100644
--- a/theodolite/src/main/kotlin/theodolite/util/Configuration.kt
+++ b/theodolite/src/main/kotlin/theodolite/util/Configuration.kt
@@ -1,5 +1,7 @@
 package theodolite.util
 
+import theodolite.execution.ExecutionModes
+
 // Defaults
 private const val DEFAULT_NAMESPACE = "default"
 private const val DEFAULT_COMPONENT_NAME = "theodolite-operator"
@@ -10,6 +12,7 @@ class Configuration(
     companion object {
         val NAMESPACE = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE
         val COMPONENT_NAME = System.getenv("COMPONENT_NAME") ?: DEFAULT_COMPONENT_NAME
+        val EXECUTION_MODE = System.getenv("MODE") ?: ExecutionModes.STANDALONE.value
     }
 
 }
-- 
GitLab