From f87f1a1ac152aa8197cc813c24855b00ebe573b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Vonheiden?= <bjoern.vonheiden@hotmail.de>
Date: Tue, 7 Sep 2021 11:08:51 +0200
Subject: [PATCH] use the composition over inheritance patter for
 DataVolumeLoadGeneratorReplicaPatcher

---
 .../DataVolumeLoadGeneratorReplicaPatcher.kt  | 37 +++++++++++--------
 .../theodolite/patcher/PatcherFactory.kt      |  2 +-
 2 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/theodolite/src/main/kotlin/theodolite/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt b/theodolite/src/main/kotlin/theodolite/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt
index f797fc870..bdc107910 100644
--- a/theodolite/src/main/kotlin/theodolite/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt
+++ b/theodolite/src/main/kotlin/theodolite/patcher/DataVolumeLoadGeneratorReplicaPatcher.kt
@@ -1,31 +1,38 @@
 package theodolite.patcher
 
 import io.fabric8.kubernetes.api.model.KubernetesResource
-import io.fabric8.kubernetes.api.model.apps.Deployment
 
 /**
- * The DataVolumeLoadGeneratorReplicaPatcher allows to modify the value of an environment variable
+ * The DataVolumeLoadGeneratorReplicaPatcher takes the total load that should be generated
+ * and computes the number of instances needed for this load based on the `maxVolume`
+ * ((load + maxVolume - 1) / maxVolume) and calculates the load per instance
+ * (loadPerInstance = load / instances).
+ * The number of instances are set for the load generator and the given variable is set to the
+ * load per instance.
  *
  * @property k8sResource Kubernetes resource to be patched.
+ * @property maxVolume per load generator instance
  * @property container Container to be patched.
  * @property variableName Name of the environment variable to be patched.
  */
 class DataVolumeLoadGeneratorReplicaPatcher(
-    private val k8sResource: KubernetesResource,
-    private val maxVolume: String,
+    k8sResource: KubernetesResource,
+    private val maxVolume: Int,
     container: String,
     variableName: String
-) : EnvVarPatcher(k8sResource, container, variableName) {
+) : AbstractPatcher(k8sResource) {
 
-    override fun <String> patch(value: String) {
-        if (k8sResource is Deployment) {
-            if (value is kotlin.String) {
-                val load = value.toInt()
-                val loadGenInstances = (load + maxVolume.toInt() - 1) / maxVolume.toInt()
-                this.k8sResource.spec.replicas = loadGenInstances
-                val loadPerInstance = load / loadGenInstances
-                super.patch(loadPerInstance.toString())
-            }
-        }
+    private val replicaPatcher = ReplicaPatcher(k8sResource)
+    private val envVarPatcher = EnvVarPatcher(k8sResource, container, variableName)
+
+    override fun <T> patch(value: T) {
+        // calculate number of load generator instances and load per instance
+        val load = Integer.parseInt(value.toString())
+        val loadGenInstances = (load + maxVolume - 1) / maxVolume
+        val loadPerInstance = load / loadGenInstances
+
+        // Patch instance values and load value of generators
+        replicaPatcher.patch(loadGenInstances.toString())
+        envVarPatcher.patch(loadPerInstance.toString())
     }
 }
diff --git a/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt b/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
index 01a104cbd..9350cecc4 100644
--- a/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
+++ b/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
@@ -50,7 +50,7 @@ class PatcherFactory {
                 )
                 "DataVolumeLoadGeneratorReplicaPatcher" -> DataVolumeLoadGeneratorReplicaPatcher(
                     k8sResource = resource,
-                    maxVolume = patcherDefinition.properties["maxVolume"]!!,
+                    maxVolume = patcherDefinition.properties["maxVolume"]!!.toInt(),
                     container = patcherDefinition.properties["container"]!!,
                     variableName = patcherDefinition.properties["variableName"]!!
                 )
-- 
GitLab