From 06e829625bb266ff3668c5f4fcda5f2505d54f0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <soeren.henning@email.uni-kiel.de>
Date: Thu, 16 Jun 2022 12:50:47 +0200
Subject: [PATCH] Minor code quality improvements

---
 .../patcher/ConfigOverrideModifier.kt         | 17 ++++------
 .../DataVolumeLoadGeneratorReplicaPatcher.kt  |  5 ++-
 .../kubernetes/patcher/PatchHandler.kt        |  4 +--
 .../patcher/ResourceLimitPatcher.kt           | 32 +++++++++----------
 .../patcher/ResourceRequestPatcher.kt         |  2 +-
 .../patcher/TemplateLabelPatcher.kt           |  1 -
 6 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ConfigOverrideModifier.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ConfigOverrideModifier.kt
index c45808b5a..8945d9b64 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ConfigOverrideModifier.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ConfigOverrideModifier.kt
@@ -21,16 +21,13 @@ class ConfigOverrideModifier(val execution: BenchmarkExecution, val resources: L
         labelValue: String,
         labelName: String
     ) {
-        val additionalConfigOverrides = mutableListOf<ConfigurationOverride>()
-        resources.forEach {
-            run {
-                val configurationOverride = ConfigurationOverride()
-                configurationOverride.patcher = PatcherDefinition()
-                configurationOverride.patcher.type = "LabelPatcher"
-                configurationOverride.patcher.properties = mutableMapOf("variableName" to labelName)
-                configurationOverride.patcher.resource = it
-                configurationOverride.value = labelValue
-                additionalConfigOverrides.add(configurationOverride)
+        val additionalConfigOverrides = resources.map {
+            ConfigurationOverride().apply {
+                this.patcher = PatcherDefinition()
+                this.patcher.type = "LabelPatcher"
+                this.patcher.properties = mapOf("variableName" to labelName)
+                this.patcher.resource = it
+                this.value = labelValue
             }
         }
         execution.configOverrides.addAll(additionalConfigOverrides)
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt
index d884c9ee1..2979d0626 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt
@@ -24,15 +24,14 @@ class DataVolumeLoadGeneratorReplicaPatcher(
         return resources.flatMap { patchSingeResource(it, value)}
     }
 
-    fun patchSingeResource(k8sResource: HasMetadata, value: String): List<HasMetadata> {
-        var resource = k8sResource
+    private fun patchSingeResource(k8sResource: HasMetadata, value: String): List<HasMetadata> {
         // calculate number of load generator instances and load per instance
         val load = Integer.parseInt(value)
         val loadGenInstances = (load + maxVolume - 1) / maxVolume
         val loadPerInstance = load / loadGenInstances
 
         // Patch instance values and load value of generators
-        val resourceList = ReplicaPatcher().patch(listOf(resource), loadGenInstances.toString())
+        val resourceList = ReplicaPatcher().patch(listOf(k8sResource), loadGenInstances.toString())
         return EnvVarPatcher(this.container, this.variableName).patch(resourceList, loadPerInstance.toString())
     }
 }
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatchHandler.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatchHandler.kt
index 6873d9ec8..92b1e52ba 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatchHandler.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatchHandler.kt
@@ -3,13 +3,13 @@ package rocks.theodolite.kubernetes.patcher
 import io.fabric8.kubernetes.api.model.HasMetadata
 class PatchHandler {
     companion object {
-        private fun getResourcesToPatch(resources: MutableMap<String, List<HasMetadata>>, patcherDefinition: PatcherDefinition): List<HasMetadata> {
+        private fun getResourcesToPatch(resources: Map<String, List<HasMetadata>>, patcherDefinition: PatcherDefinition): List<HasMetadata> {
             return resources[patcherDefinition.resource]
                 ?: throw InvalidPatcherConfigurationException("Could not find resource ${patcherDefinition.resource}")
 
         }
         fun patchResource(
-            resources: MutableMap<String, List<HasMetadata>>,
+            resources: Map<String, List<HasMetadata>>,
             patcherDefinition: PatcherDefinition,
             value: String,
         ): List<HasMetadata> {
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt
index 7450e2edf..c8064c605 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt
@@ -29,7 +29,7 @@ class ResourceLimitPatcher(
                 }
             }
             else -> {
-                throw InvalidPatcherConfigurationException("ResourceLimitPatcher is not applicable for $resource")
+                throw InvalidPatcherConfigurationException("ResourceLimitPatcher is not applicable for $resource.")
             }
         }
         return resource
@@ -37,21 +37,21 @@ class ResourceLimitPatcher(
 
 
         private fun setLimits(container: Container, value: String) {
-        when {
-            container.resources == null -> {
-                val resource = ResourceRequirements()
-                resource.limits = mapOf(limitedResource to Quantity(value))
-                container.resources = resource
-            }
-            container.resources.limits.isEmpty() -> {
-                container.resources.limits = mapOf(limitedResource to Quantity(value))
-            }
-            else -> {
-                val values = mutableMapOf<String, Quantity>()
-                container.resources.limits.forEach { entry -> values[entry.key] = entry.value }
-                values[limitedResource] = Quantity(value)
-                container.resources.limits = values
-            }
+            when {
+                container.resources == null -> {
+                    val resource = ResourceRequirements()
+                    resource.limits = mapOf(limitedResource to Quantity(value))
+                    container.resources = resource
+                }
+                container.resources.limits.isEmpty() -> {
+                    container.resources.limits = mapOf(limitedResource to Quantity(value))
+                }
+                else -> {
+                    val values = mutableMapOf<String, Quantity>()
+                    container.resources.limits.forEach { entry -> values[entry.key] = entry.value }
+                    values[limitedResource] = Quantity(value)
+                    container.resources.limits = values
+                }
         }
     }
 }
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt
index bdcdc9527..7d9cd8b74 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt
@@ -29,7 +29,7 @@ class ResourceRequestPatcher(
                 }
             }
             else -> {
-                throw InvalidPatcherConfigurationException("ResourceRequestPatcher is not applicable for $resource")
+                throw InvalidPatcherConfigurationException("ResourceRequestPatcher is not applicable for $resource.")
             }
         }
         return resource
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/TemplateLabelPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/TemplateLabelPatcher.kt
index 534809cf8..e1c1bc3a1 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/TemplateLabelPatcher.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/TemplateLabelPatcher.kt
@@ -13,7 +13,6 @@ class TemplateLabelPatcher(
     val variableName: String) :
     AbstractPatcher() {
 
-
     override fun patchSingleResource(resource: HasMetadata, value: String): HasMetadata {
         when (resource) {
             is Deployment -> {
-- 
GitLab