From 4da0a5909d668dd688897adc2028eecd7b83273a Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Mon, 25 Jan 2021 02:41:04 +0100
Subject: [PATCH] Logger + ServiceManager + ConfigMaps + Refactoring

---
 theodolite-quarkus/YAML/jmx-configmap.yaml    |  10 ++
 .../kotlin/theodolite/DeploymentManager.kt    | 112 +++++++++---------
 .../src/main/kotlin/theodolite/Main.kt        |   4 +
 .../main/kotlin/theodolite/ServiceManager.kt  |  12 ++
 .../src/main/kotlin/theodolite/YamlLoader.kt  |  35 +++---
 5 files changed, 96 insertions(+), 77 deletions(-)
 create mode 100644 theodolite-quarkus/YAML/jmx-configmap.yaml
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/ServiceManager.kt

diff --git a/theodolite-quarkus/YAML/jmx-configmap.yaml b/theodolite-quarkus/YAML/jmx-configmap.yaml
new file mode 100644
index 000000000..78496a86b
--- /dev/null
+++ b/theodolite-quarkus/YAML/jmx-configmap.yaml
@@ -0,0 +1,10 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: aggregation-jmx-configmap
+data:
+  jmx-kafka-prometheus.yml: |+
+    jmxUrl: service:jmx:rmi:///jndi/rmi://localhost:5555/jmxrmi
+    lowercaseOutputName: true
+    lowercaseOutputLabelNames: true
+    ssl: false
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/DeploymentManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/DeploymentManager.kt
index c5cd75bd2..5c8e4cfff 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/DeploymentManager.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/DeploymentManager.kt
@@ -1,14 +1,13 @@
 package theodolite
 
-import com.fasterxml.jackson.annotation.JsonProperty
 import io.fabric8.kubernetes.api.model.*
 import io.fabric8.kubernetes.api.model.apps.Deployment
 import io.fabric8.kubernetes.client.DefaultKubernetesClient
-import io.fabric8.kubernetes.client.dsl.RollableScalableResource
-import java.io.File
+import mu.KotlinLogging
 import java.io.InputStream
 import java.nio.file.Paths
 
+private val logger = KotlinLogging.logger {}
 
 class DeploymentManager {
     val MEMORYLIMIT = "memory"
@@ -20,95 +19,94 @@ class DeploymentManager {
     val service = "aggregation-service.yaml"
     val workloadFile = "workloadGenerator.yaml"
     val usecase = "aggregation-deployment.yaml"
+    val configMap = "jmx-configmap.yaml"
     val inputStream: InputStream = path.byteInputStream()
     val client = DefaultKubernetesClient().inNamespace("default")
 
-    val core  = client.configMaps()
 
-    //val deployment = client.apps().deployments().load(absolute + path)
-
-    val dp: Service = client.services().load(path + service).get();
+    val dp: Service = client.services().load(path + service).get()
 
     val workload: Deployment = client.apps().deployments().load(path + workloadFile).get()
     val use: Deployment = client.apps().deployments().load(path + usecase).get()
 
-    // TODO CHANGE CONFIGURATION ENVIROMENT VARIABLES (DONE)
-    // TODO ASSEMBLE GOOD SEPERATION
-    // TODO ADD SERVICES
-    // TODO ADD CONFIG MAP
-    // TODO DELETE STUFF
-    // TODO MAKE YAML LOADING CATCH EXEPTION
 
-    fun printFile(){
+    val loader = YamlLoader(client)
+    val config: ConfigMap? = loader.loadConfigmap(path + configMap)    // TODO ASSEMBLE GOOD SEPERATION
 
-//
+    // TODO REFACTOR EVErythiNG
+    fun printFile() {
 //        println(workload)
 //        changeEnviromentsInstances(workload,"5000")
 //        println(workload)
 
-        println(use)
-        changeRessourceLimits(use, MEMORYLIMIT,"5Gi")
-        println(use)
-    //        println(path)
+//        logger.debug(use.toString())
+//        changeRessourceLimits(use, MEMORYLIMIT, "5Gi")
+//        logger.debug(use.toString())
+
+        logger.info(workload.toString())
+        val testMap = mapOf<String, String>("NUM_SENSORS" to "5000")
+        val vars =
+            workload.spec.template.spec.containers.filter { it.name == "workload-generator" }.forEach { it: Container ->
+                setContainerEnv(it, testMap)
+            }
+
+        logger.info(workload.toString())
+
+        // logger.debug(config.toString())
+
+        //        println(path)
 //        val f : File = File(path+theodoliteDeploment);
 //        val fileAsString : String = String(f.readBytes())
 //        println(fileAsString.replace("theodolite","spesb"))
     }
 
     /**
-     * TODO REFACTOR
+     * Sets the ContainerEvironmentVariables, creates new if variable don t exist.
+     * @param container - The Container
+     * @param map - Map of k=Name,v =Value of EnviromentVariables
      */
-    fun setContainerEnv (container: Container, map: Map<String,String>){
-        map.forEach{ k,v ->
-
-            container.env
-
-            container.env.filter { x -> x.name == k }.forEach {
-                it.value = v
+    fun setContainerEnv(container: Container, map: Map<String, String>) {
+        map.forEach { k, v ->
+            // filter for mathing name and set value
+            val x = container.env.filter { envvar -> envvar.name == k }
+
+            if (x.isEmpty()) {
+                val newVar = EnvVar(k, v, EnvVarSource())
+                container.env.add(newVar)
+            } else {
+                x.forEach {
+                    it.value = v
+                }
             }
-            var c = container.env.filter { x -> x.name == k }
-
-            if(c.isEmpty()){
-                val y = EnvVar(k,v, EnvVarSource())
-                c = listOf(y)
-            }
-
         }
-
     }
 
-
-    // SERVICE
-    fun changeServiceName (service: Service,newName : String){
-
-        service.metadata.apply {
-            name = newName
-        }
-    }
-
-    // WORKLOAD GEN
-    // TODO REFACTOR
-    fun changeEnviromentsInstances (dep: Deployment,num: String) {
-
-        dep.spec.template.spec.containers.get(0).env.filter {
-            it.name == "NUM_SENSORS"
-        }.forEach { x ->
-            x.value = num
-        }
+    /**
+     * Set the enviroment Variable for a Container
+     */
+    fun setWorkloadEnv(workloadDeployment: Deployment, containerName: String, map: Map<String, String>) {
+        workloadDeployment.spec.template.spec.containers.filter { it.name == containerName }
+            .forEach { it: Container ->
+                setContainerEnv(it, map)
+            }
     }
 
-    // APPLICATION
+    /**
+     *  Change the RessourceLimit of the SUT
+     */
     fun changeRessourceLimits(dep: Deployment, ressource: String, limit: String) {
-
         val vars = dep.spec.template.spec.containers.filter { it.name == "uc-application" }.forEach {
             it.resources.limits.replace(ressource, Quantity(limit))
         }
     }
 
+    /**
+     * Change the image of the SUT and the Worklaodgenerators
+     */
     fun changeImage(dep: Deployment, image: String) {
 
         dep.spec.template.spec.containers.filter { it.name == "uc-application" }.forEach {
-            it.image = image }
+            it.image = image
+        }
     }
-
 }
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/Main.kt b/theodolite-quarkus/src/main/kotlin/theodolite/Main.kt
index 3311d15d7..57031509d 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/Main.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/Main.kt
@@ -1,6 +1,8 @@
 package theodolite
+
 import io.quarkus.runtime.annotations.QuarkusMain
 import mu.KotlinLogging
+
 private val logger = KotlinLogging.logger {}
 
 @QuarkusMain
@@ -9,6 +11,8 @@ object Main {
     fun main(args: Array<String>) {
         logger.info("Application started")
 
+        val x = DeploymentManager()
+        x.printFile()
         //Quarkus.run()
     }
 }
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/ServiceManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/ServiceManager.kt
new file mode 100644
index 000000000..83d8c023d
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/ServiceManager.kt
@@ -0,0 +1,12 @@
+package theodolite
+
+import io.fabric8.kubernetes.api.model.Service
+
+class ServiceManager {
+    fun changeServiceName(service: Service, newName: String) {
+
+        service.metadata.apply {
+            name = newName
+        }
+    }
+}
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/YamlLoader.kt b/theodolite-quarkus/src/main/kotlin/theodolite/YamlLoader.kt
index 6bf0c03ae..2c338f664 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/YamlLoader.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/YamlLoader.kt
@@ -1,50 +1,45 @@
 package theodolite
 
+import io.fabric8.kubernetes.api.model.ConfigMap
 import io.fabric8.kubernetes.api.model.Service
 import io.fabric8.kubernetes.api.model.apps.Deployment
 import io.fabric8.kubernetes.client.NamespacedKubernetesClient
 import mu.KotlinLogging
+
 private val logger = KotlinLogging.logger {}
 
 class YamlLoader(client: NamespacedKubernetesClient) {
     lateinit var client: NamespacedKubernetesClient
 
-    init{
-         this.client = client
+    init {
+        this.client = client
     }
 
-
     fun loadService(path: String): Service? {
 
-        var service: Service? = null
-
-        try {
-            service = client.services().load(path).get()
-        } catch (e: Exception) {
-            logger.info("You potentially  misspeled the path: $path")
-            logger.info("$e")
-        }
-
+        val service = loadGenericRessource(path, { x: String -> client.services().load(x).get() })
         return service
     }
 
     fun loadDeployment(path: String): Deployment? {
-        val deployment = loadGenericRessource(path,{x: String-> client.apps().deployments().load(x).get()})
+        val deployment = loadGenericRessource(path, { x: String -> client.apps().deployments().load(x).get() })
         return deployment
     }
 
-    fun <T> loadGenericRessource(path: String, f: (String) -> T): T?{
-        var service: T? = null
+    fun loadConfigmap(path: String): ConfigMap? {
+        val configMap = loadGenericRessource(path, { x: String -> client.configMaps().load(x).get() })
+        return configMap
+    }
+
+    fun <T> loadGenericRessource(path: String, f: (String) -> T): T? {
+        var resource: T? = null
 
         try {
-            service = f(path)
+            resource = f(path)
         } catch (e: Exception) {
             logger.info("You potentially  misspelled the path: $path")
             logger.info("$e")
         }
-
-        return service
-    }
-
+        return resource
     }
 }
-- 
GitLab