From 1ae4932e587f7f2fc4b528b5af5f5e7a4478428f Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Tue, 15 Feb 2022 12:41:18 +0100
Subject: [PATCH] Add addional patcher to make it possible to set matchlabels

---
 .../theodolite/patcher/MatchLabelPatcher.kt   | 28 +++++++++++++++++++
 .../theodolite/patcher/PatcherFactory.kt      |  8 ++++++
 .../patcher/TemplateLabelPatcher.kt           | 28 +++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 theodolite/src/main/kotlin/theodolite/patcher/MatchLabelPatcher.kt
 create mode 100644 theodolite/src/main/kotlin/theodolite/patcher/TemplateLabelPatcher.kt

diff --git a/theodolite/src/main/kotlin/theodolite/patcher/MatchLabelPatcher.kt b/theodolite/src/main/kotlin/theodolite/patcher/MatchLabelPatcher.kt
new file mode 100644
index 000000000..1ef6b915a
--- /dev/null
+++ b/theodolite/src/main/kotlin/theodolite/patcher/MatchLabelPatcher.kt
@@ -0,0 +1,28 @@
+package theodolite.patcher
+
+import io.fabric8.kubernetes.api.model.KubernetesResource
+import io.fabric8.kubernetes.api.model.apps.Deployment
+import io.fabric8.kubernetes.api.model.apps.StatefulSet
+
+class MatchLabelPatcher(private val k8sResource: KubernetesResource, val variableName: String) :
+    AbstractPatcher(k8sResource) {
+
+    override fun <String> patch(labelValue: String) {
+        if (labelValue is kotlin.String) {
+            when (k8sResource) {
+                is Deployment -> {
+                    if (k8sResource.spec.selector.matchLabels == null) {
+                        k8sResource.spec.selector.matchLabels = mutableMapOf()
+                    }
+                    k8sResource.spec.selector.matchLabels[this.variableName] = labelValue
+                }
+                is StatefulSet -> {
+                    if (k8sResource.spec.selector.matchLabels == null) {
+                        k8sResource.spec.selector.matchLabels = mutableMapOf()
+                    }
+                    k8sResource.spec.selector.matchLabels[this.variableName] = labelValue
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt b/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
index 88b3e19e9..e92de4dba 100644
--- a/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
+++ b/theodolite/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
@@ -79,6 +79,14 @@ class PatcherFactory {
                     k8sResource = resource,
                     variableName = patcherDefinition.properties["variableName"]!!
                 )
+                "MatchLabelPatcher" -> MatchLabelPatcher(
+                    k8sResource = resource,
+                    variableName = patcherDefinition.properties["variableName"]!!
+                )
+                "TemplateLabelPatcher" -> TemplateLabelPatcher(
+                    k8sResource = resource,
+                    variableName = patcherDefinition.properties["variableName"]!!
+                )
                 "ImagePatcher" -> ImagePatcher(
                     k8sResource = resource,
                     container = patcherDefinition.properties["container"]!!
diff --git a/theodolite/src/main/kotlin/theodolite/patcher/TemplateLabelPatcher.kt b/theodolite/src/main/kotlin/theodolite/patcher/TemplateLabelPatcher.kt
new file mode 100644
index 000000000..aa1fc8f17
--- /dev/null
+++ b/theodolite/src/main/kotlin/theodolite/patcher/TemplateLabelPatcher.kt
@@ -0,0 +1,28 @@
+package theodolite.patcher
+
+import io.fabric8.kubernetes.api.model.KubernetesResource
+import io.fabric8.kubernetes.api.model.apps.Deployment
+import io.fabric8.kubernetes.api.model.apps.StatefulSet
+
+class TemplateLabelPatcher(private val k8sResource: KubernetesResource, val variableName: String) :
+    AbstractPatcher(k8sResource) {
+
+    override fun <String> patch(labelValue: String) {
+        if (labelValue is kotlin.String) {
+            when (k8sResource) {
+                is Deployment -> {
+                    if (k8sResource.spec.template.metadata.labels == null) {
+                        k8sResource.spec.template.metadata.labels = mutableMapOf()
+                    }
+                    k8sResource.spec.template.metadata.labels[this.variableName] = labelValue
+                }
+                is StatefulSet -> {
+                    if (k8sResource.spec.template.metadata.labels == null) {
+                        k8sResource.spec.template.metadata.labels = mutableMapOf()
+                    }
+                    k8sResource.spec.template.metadata.labels[this.variableName] = labelValue
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab