diff --git a/docs/api-reference/patchers.md b/docs/api-reference/patchers.md index 77f937e38f6a0ee4084cb0ad5b5838718eabff10..bea63ccd23decef5654f257221ce0358b4f68e45 100644 --- a/docs/api-reference/patchers.md +++ b/docs/api-reference/patchers.md @@ -53,6 +53,27 @@ Patchers can be seen as functions which take a value as input and modify a Kuber * **resource**: "uc1-kstreams-deployment.yaml" * **example value**: "random-scheduler" +* **LabelPatcher**: Changes the label of a Kubernetes Deployment or StatefulSet. The patched field is: `metadata.labels` + * **type**: "LabelPatcher" + * **resource**: "uc1-kstreams-deployment.yaml" + * **properties**: + * variableName: "app" + * **example value**: "theodolite-sut" + +* **MatchLabelPatcher**: Changes the match labels of a Kubernetes Deployment or StatefulSet. The patched field is: `spec.selector.matchLabels` + * **type**: "MatchLabelPatcher" + * **resource**: "uc1-kstreams-deployment.yaml" + * **properties**: + * variableName: "app" + * **example value**: "theodolite-sut" + +* **TemplateLabelPatcher**: Changes the template labels of a Kubernetes Deployment or StatefulSet. The patched field is: `spec.template.metadata.labels` + * **type**: "MatchLabelPatcher" + * **resource**: "uc1-kstreams-deployment.yaml" + * **properties**: + * variableName: "app" + * **example value**: "theodolite-sut" + * **ImagePatcher**: Changes the image of a Kubernetes resource. **Currently not fully implemented.** * **type**: "ImagePatcher" * **resource**: "uc1-kstreams-deployment.yaml" 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 0000000000000000000000000000000000000000..30ff73b5da3b551119ad085adbc982180e4fc066 --- /dev/null +++ b/theodolite/src/main/kotlin/theodolite/patcher/MatchLabelPatcher.kt @@ -0,0 +1,34 @@ +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 + +/** + * This patcher is able to set the `spec.selector.matchLabels` for a `Deployment` or `StatefulSet` Kubernetes resource. + * + * @property k8sResource The Kubernetes manifests to patch + * @property variableName The matchLabel which should be set + */ +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 88b3e19e999a889cdcb8345ca7c90c37a6e6d275..e92de4dba7de298c9df76600f2c6785f5878103e 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 0000000000000000000000000000000000000000..a524e5c40f90ccf98dc95003cc33dcfceb6f8598 --- /dev/null +++ b/theodolite/src/main/kotlin/theodolite/patcher/TemplateLabelPatcher.kt @@ -0,0 +1,34 @@ +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 + +/** + * This patcher is able to set the field `spec.template.metadata.labels` for a `Deployment` or `StatefulSet` Kubernetes resource. + * + * @property k8sResource The Kubernetes manifests to patch + * @property variableName The label which should be set + */ +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