From 3525a05ce40ae5566ab83ee198b09b596ad1bbc3 Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Mon, 27 Sep 2021 11:37:25 +0200
Subject: [PATCH] minor code changes

---
 .../execution/operator/EventCreator.kt        | 46 +++++++++++--------
 .../execution/operator/TheodoliteOperator.kt  |  5 +-
 .../kotlin/theodolite/util/Configuration.kt   | 15 ++++++
 3 files changed, 44 insertions(+), 22 deletions(-)
 create mode 100644 theodolite/src/main/kotlin/theodolite/util/Configuration.kt

diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt
index 24d221e44..69b21155d 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt
@@ -5,34 +5,40 @@ import io.fabric8.kubernetes.api.model.EventSource
 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.*
+import kotlin.NoSuchElementException
+private val logger = KotlinLogging.logger {}
 
 class EventCreator {
-    val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace("default")
+    val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace(Configuration.NAMESPACE)
 
     fun createEvent(executionName: String, type: String, message: String, reason: String) {
         val uuid = UUID.randomUUID().toString()
-        println("uuid is: " + uuid)
-        val objectRef = buildObjectReference(executionName)
-        val event = EventBuilder()
-            .withNewMetadata()
+        try {
+            val objectRef = buildObjectReference(executionName)
+            val event = EventBuilder()
+                .withNewMetadata()
                 .withName(uuid)
-            .endMetadata()
-            .build()
-
-        event.message = message
-        event.reason = reason
-        event.type = type
-
-        event.firstTimestamp =  Instant.now().toString()
-
-        val source =  EventSource()
-        source.component = "theodolite-operator"
-        event.source = source
-
-        event.involvedObject = objectRef
-        client.v1().events().inNamespace("default").create(event)
+                .endMetadata()
+                .withMessage(message)
+                .withReason(reason)
+                .withType(type)
+                .withFirstTimestamp(Instant.now().toString()) // TODO change datetime format
+                .build()
+
+            val source =  EventSource()
+            source.component = Configuration.COMPONENT_NAME
+            event.source = source
+
+            event.involvedObject = objectRef
+            client.v1().events().inNamespace(Configuration.NAMESPACE).createOrReplace(event)
+        } catch (e: NoSuchElementException) {
+                logger.warn {"Could not create event: type: $type, message: $message, reason: $reason, no corresponding execution found."}
+        }
     }
 
     private fun buildObjectReference(executionName: String): ObjectReference {
diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt
index 6afd8451d..cbce5749f 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt
@@ -11,6 +11,7 @@ import theodolite.model.crd.BenchmarkCRD
 import theodolite.model.crd.BenchmarkExecutionList
 import theodolite.model.crd.ExecutionCRD
 import theodolite.model.crd.KubernetesBenchmarkList
+import theodolite.util.Configuration
 
 
 private const val DEFAULT_NAMESPACE = "default"
@@ -27,7 +28,7 @@ private val logger = KotlinLogging.logger {}
  * **See Also:** [Kubernetes Operator Pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
  */
 class TheodoliteOperator {
-    private val namespace = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE
+    private val namespace = Configuration.NAMESPACE
 
     private val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace(namespace)
     private lateinit var controller: TheodoliteController
@@ -37,7 +38,7 @@ class TheodoliteOperator {
     fun start() {
         LeaderElector(
             client = client,
-            name = "theodolite-operator" // TODO(make leaslock name configurable via env var)
+            name = Configuration.COMPONENT_NAME
         )
             .getLeadership(::startOperator)
     }
diff --git a/theodolite/src/main/kotlin/theodolite/util/Configuration.kt b/theodolite/src/main/kotlin/theodolite/util/Configuration.kt
new file mode 100644
index 000000000..5bc1edd19
--- /dev/null
+++ b/theodolite/src/main/kotlin/theodolite/util/Configuration.kt
@@ -0,0 +1,15 @@
+package theodolite.util
+
+// Defaults
+private const val DEFAULT_NAMESPACE = "default"
+private const val DEFAULT_COMPONENT_NAME = "theodolite-operator"
+
+
+class Configuration(
+) {
+    companion object {
+        val NAMESPACE = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE
+        val COMPONENT_NAME = System.getenv("COMPONENT_NAME") ?: DEFAULT_COMPONENT_NAME
+    }
+
+}
-- 
GitLab