From 897f7da329646b35e78f2597c0db943feee2d35c Mon Sep 17 00:00:00 2001 From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de> Date: Thu, 25 Feb 2021 20:08:33 +0100 Subject: [PATCH] add a first trial for resource patcher --- .../{k8s => deprecated}/DeploymentManager.kt | 2 +- .../execution/TheodoliteYamlExecutor.kt | 1 - .../theodolite/k8s/K8sResourceLoader.kt | 8 ++--- .../kotlin/theodolite/patcher/ImagePatcher.kt | 21 ++++++++++++ .../theodolite/patcher/PatcherManager.kt | 12 +++---- .../patcher/ResourceLimitPatcher.kt | 34 +++++++++++++++++++ .../patcher/ResourceRequestPatcher.kt | 21 ++++++++++++ .../yaml/aggregation-deployment.yaml | 4 --- .../src/main/resources/yaml/testContext.yaml | 14 +++++++- 9 files changed, 100 insertions(+), 17 deletions(-) rename theodolite-quarkus/src/main/kotlin/theodolite/{k8s => deprecated}/DeploymentManager.kt (98%) create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceLimitPatcher.kt create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceRequestPatcher.kt diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/DeploymentManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/deprecated/DeploymentManager.kt similarity index 98% rename from theodolite-quarkus/src/main/kotlin/theodolite/k8s/DeploymentManager.kt rename to theodolite-quarkus/src/main/kotlin/theodolite/deprecated/DeploymentManager.kt index af59c5236..20980a428 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/DeploymentManager.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/deprecated/DeploymentManager.kt @@ -1,4 +1,4 @@ -package theodolite.k8s +package theodolite.deprecated import io.fabric8.kubernetes.api.model.Container import io.fabric8.kubernetes.api.model.EnvVar diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt index 4984f9bfe..80eb2eee1 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt @@ -6,7 +6,6 @@ import theodolite.benchmark.KubernetesBenchmark class TheodoliteYamlExecutor { fun run() { - // load the Benchmark context and the benchmark type var parser = YamlParser() val benchmarkContext = parser.parse("./../../../resources/main/yaml/testContext.yaml", BenchmarkContext::class.java) !! diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt index e326ad3fe..6be09ce26 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt @@ -18,7 +18,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { * @param path of the yaml file * @return service from fabric8 */ - fun loadService(path: String): Service { + private fun loadService(path: String): Service { return loadGenericRessource(path) { x: String -> client.services().load(x).get() } } @@ -27,7 +27,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { * @param path of the yaml file * @return service from fabric8 */ - fun loadServiceMonitor(path: String): CustomResourceDefinition { + private fun loadServiceMonitor(path: String): CustomResourceDefinition { return loadGenericRessource(path) { x: String -> client.customResourceDefinitions().load(x).get() } } @@ -36,7 +36,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { * @param path of the yaml file * @return Deployment from fabric8 */ - fun loadDeployment(path: String): Deployment { + private fun loadDeployment(path: String): Deployment { return loadGenericRessource(path) { x: String -> client.apps().deployments().load(x).get() } } @@ -45,7 +45,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { * @param path of the yaml file * @return ConfigMap from fabric8 */ - fun loadConfigmap(path: String): ConfigMap { + private fun loadConfigmap(path: String): ConfigMap { return loadGenericRessource(path) { x: String -> client.configMaps().load(x).get() } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt new file mode 100644 index 000000000..3a7f39cce --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt @@ -0,0 +1,21 @@ +package theodolite.patcher + +import io.fabric8.kubernetes.api.model.KubernetesResource +import io.fabric8.kubernetes.api.model.Quantity +import io.fabric8.kubernetes.api.model.apps.Deployment +import io.fabric8.kubernetes.api.model.apps.StatefulSet + +class ImagePatcher(private val k8sResource: KubernetesResource, private val container: String, private val variableName: String): AbstractPatcher(k8sResource, container, variableName) { + + override fun <String> patch(value: String) { + if (k8sResource is Deployment) { + k8sResource.spec.template.spec.containers.filter { it.name == container }.forEach { + it.image = value as kotlin.String + } + } else if (k8sResource is StatefulSet) { + k8sResource.spec.template.spec.containers.filter { it.name == container }.forEach { + it.image = value as kotlin.String + } + } + } +} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt index b3a63e2ad..9fbbf98fe 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt @@ -7,13 +7,13 @@ import java.lang.IllegalArgumentException class PatcherManager { private fun createK8sPatcher(patcherDefinition: PatcherDefinition, k8sResources: List<Pair<String, KubernetesResource>>): Patcher { + val resource = k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0] return when(patcherDefinition.type) { - "ReplicaPatcher" -> ReplicaPatcher(k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0]) - "EnvVarPatcher" -> EnvVarPatcher(k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0], - patcherDefinition.container, - patcherDefinition.variableName) - "NodeSelectorPatcher" -> NodeSelectorPatcher(k8sResources.filter { it.first == patcherDefinition.resource }.map { resource -> resource.second }[0], - patcherDefinition.variableName) + "ReplicaPatcher" -> ReplicaPatcher(resource) + "EnvVarPatcher" -> EnvVarPatcher(resource, patcherDefinition.container, patcherDefinition.variableName) + "NodeSelectorPatcher" -> NodeSelectorPatcher(resource, patcherDefinition.variableName) + "ResourceLimitPatcher" -> ResourceLimitPatcher(resource, patcherDefinition.container, patcherDefinition.variableName) + "ResourceRequestPatcher" -> ResourceRequestPatcher(resource, patcherDefinition.container, patcherDefinition.variableName) else -> throw IllegalArgumentException("Patcher type ${patcherDefinition.type} not found") } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceLimitPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceLimitPatcher.kt new file mode 100644 index 000000000..3ccc42a76 --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceLimitPatcher.kt @@ -0,0 +1,34 @@ +package theodolite.patcher + +import io.fabric8.kubernetes.api.model.KubernetesResource +import io.fabric8.kubernetes.api.model.Quantity +import io.fabric8.kubernetes.api.model.ResourceRequirements +import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder +import io.fabric8.kubernetes.api.model.apps.Deployment +import io.fabric8.kubernetes.api.model.apps.StatefulSet +import java.lang.IllegalStateException + +class ResourceLimitPatcher(private val k8sResource: KubernetesResource, private val container: String, private val variableName: String): AbstractPatcher(k8sResource, container, variableName) { + + override fun <String> patch(value: String) { + + if (k8sResource is Deployment) { + k8sResource.spec.template.spec.containers.filter { it.name == container }.forEach { + try { + if (it.resources.limits.isNullOrEmpty()) { + it.resources.limits = mapOf(variableName to Quantity(value as kotlin.String)) + } else { + val values = it.resources.limits + println(values) + values[variableName] = Quantity(value as kotlin.String) + it.resources.limits = values + } + } catch (e: IllegalStateException) { + val resource = ResourceRequirements() + resource.limits = mapOf(variableName to Quantity(value as kotlin.String)) + it.resources = resource + } + } + } + } +} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceRequestPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceRequestPatcher.kt new file mode 100644 index 000000000..249a5011c --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ResourceRequestPatcher.kt @@ -0,0 +1,21 @@ +package theodolite.patcher + +import io.fabric8.kubernetes.api.model.KubernetesResource +import io.fabric8.kubernetes.api.model.Quantity +import io.fabric8.kubernetes.api.model.apps.Deployment +import io.fabric8.kubernetes.api.model.apps.StatefulSet + +class ResourceRequestPatcher(private val k8sResource: KubernetesResource, private val container: String, private val variableName: String): AbstractPatcher(k8sResource, container, variableName) { + + override fun <String> patch(value: String) { + if (k8sResource is Deployment) { + k8sResource.spec.template.spec.containers.filter { it.name == container }.forEach { + it.resources.requests.replace(variableName,Quantity(value as kotlin.String)) + } + } else if (k8sResource is StatefulSet) { + k8sResource.spec.template.spec.containers.filter { it.name == container }.forEach { + it.resources.requests.replace(variableName, Quantity(value as kotlin.String)) + } + } + } +} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/resources/yaml/aggregation-deployment.yaml b/theodolite-quarkus/src/main/resources/yaml/aggregation-deployment.yaml index 3963fa7f1..79691e15f 100644 --- a/theodolite-quarkus/src/main/resources/yaml/aggregation-deployment.yaml +++ b/theodolite-quarkus/src/main/resources/yaml/aggregation-deployment.yaml @@ -28,10 +28,6 @@ spec: value: "-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=5555" - name: COMMIT_INTERVAL_MS # Set as default for the applications value: "100" - resources: - limits: - memory: 4Gi - cpu: 1000m - name: prometheus-jmx-exporter image: "solsson/kafka-prometheus-jmx-exporter@sha256:6f82e2b0464f50da8104acd7363fb9b995001ddff77d248379f8788e78946143" command: diff --git a/theodolite-quarkus/src/main/resources/yaml/testContext.yaml b/theodolite-quarkus/src/main/resources/yaml/testContext.yaml index 4b71fde46..dffbe430e 100644 --- a/theodolite-quarkus/src/main/resources/yaml/testContext.yaml +++ b/theodolite-quarkus/src/main/resources/yaml/testContext.yaml @@ -27,4 +27,16 @@ configOverrides: type: "NodeSelectorPatcher" resource: "aggregation-deployment.yaml" variableName: "env" - value: "prod" \ No newline at end of file + value: "prod" + - patcher: + type: "ResourceLimitPatcher" + resource: "aggregation-deployment.yaml" + container: "uc-application" + variableName: "cpu" + value: "50m" + - patcher: + type: "ResourceLimitPatcher" + resource: "aggregation-deployment.yaml" + container: "uc-application" + variableName: "memory" + value: "2Gi" \ No newline at end of file -- GitLab