Skip to content
Snippets Groups Projects
Commit 67ad9ad5 authored by Benedikt Wetzel's avatar Benedikt Wetzel Committed by Sören Henning
Browse files

add a first trial for resource patcher

parent 8844940c
Branches
Tags
4 merge requests!159Re-implementation of Theodolite with Kotlin/Quarkus,!157Update Graal Image in CI pipeline,!85Introduce new Benchmark class and Patcher,!83WIP: Re-implementation of Theodolite with Kotlin/Quarkus
Showing
with 100 additions and 17 deletions
package theodolite.k8s package theodolite.deprecated
import io.fabric8.kubernetes.api.model.Container import io.fabric8.kubernetes.api.model.Container
import io.fabric8.kubernetes.api.model.EnvVar import io.fabric8.kubernetes.api.model.EnvVar
......
...@@ -6,7 +6,6 @@ import theodolite.benchmark.KubernetesBenchmark ...@@ -6,7 +6,6 @@ import theodolite.benchmark.KubernetesBenchmark
class TheodoliteYamlExecutor { class TheodoliteYamlExecutor {
fun run() { fun run() {
// load the Benchmark context and the benchmark type // load the Benchmark context and the benchmark type
var parser = YamlParser() var parser = YamlParser()
val benchmarkContext = parser.parse("./../../../resources/main/yaml/testContext.yaml", BenchmarkContext::class.java) !! val benchmarkContext = parser.parse("./../../../resources/main/yaml/testContext.yaml", BenchmarkContext::class.java) !!
......
...@@ -18,7 +18,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { ...@@ -18,7 +18,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) {
* @param path of the yaml file * @param path of the yaml file
* @return service from fabric8 * @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() } return loadGenericRessource(path) { x: String -> client.services().load(x).get() }
} }
...@@ -27,7 +27,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { ...@@ -27,7 +27,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) {
* @param path of the yaml file * @param path of the yaml file
* @return service from fabric8 * @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() } return loadGenericRessource(path) { x: String -> client.customResourceDefinitions().load(x).get() }
} }
...@@ -36,7 +36,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { ...@@ -36,7 +36,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) {
* @param path of the yaml file * @param path of the yaml file
* @return Deployment from fabric8 * @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() } return loadGenericRessource(path) { x: String -> client.apps().deployments().load(x).get() }
} }
...@@ -45,7 +45,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { ...@@ -45,7 +45,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) {
* @param path of the yaml file * @param path of the yaml file
* @return ConfigMap from fabric8 * @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() } return loadGenericRessource(path) { x: String -> client.configMaps().load(x).get() }
} }
......
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
...@@ -7,13 +7,13 @@ import java.lang.IllegalArgumentException ...@@ -7,13 +7,13 @@ import java.lang.IllegalArgumentException
class PatcherManager { class PatcherManager {
private fun createK8sPatcher(patcherDefinition: PatcherDefinition, k8sResources: List<Pair<String, KubernetesResource>>): Patcher { 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) { return when(patcherDefinition.type) {
"ReplicaPatcher" -> ReplicaPatcher(k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0]) "ReplicaPatcher" -> ReplicaPatcher(resource)
"EnvVarPatcher" -> EnvVarPatcher(k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0], "EnvVarPatcher" -> EnvVarPatcher(resource, patcherDefinition.container, patcherDefinition.variableName)
patcherDefinition.container, "NodeSelectorPatcher" -> NodeSelectorPatcher(resource, patcherDefinition.variableName)
patcherDefinition.variableName) "ResourceLimitPatcher" -> ResourceLimitPatcher(resource, patcherDefinition.container, patcherDefinition.variableName)
"NodeSelectorPatcher" -> NodeSelectorPatcher(k8sResources.filter { it.first == patcherDefinition.resource }.map { resource -> resource.second }[0], "ResourceRequestPatcher" -> ResourceRequestPatcher(resource, patcherDefinition.container, patcherDefinition.variableName)
patcherDefinition.variableName)
else -> throw IllegalArgumentException("Patcher type ${patcherDefinition.type} not found") else -> throw IllegalArgumentException("Patcher type ${patcherDefinition.type} not found")
} }
} }
......
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
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
...@@ -28,10 +28,6 @@ spec: ...@@ -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" 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 - name: COMMIT_INTERVAL_MS # Set as default for the applications
value: "100" value: "100"
resources:
limits:
memory: 4Gi
cpu: 1000m
- name: prometheus-jmx-exporter - name: prometheus-jmx-exporter
image: "solsson/kafka-prometheus-jmx-exporter@sha256:6f82e2b0464f50da8104acd7363fb9b995001ddff77d248379f8788e78946143" image: "solsson/kafka-prometheus-jmx-exporter@sha256:6f82e2b0464f50da8104acd7363fb9b995001ddff77d248379f8788e78946143"
command: command:
......
...@@ -28,3 +28,15 @@ configOverrides: ...@@ -28,3 +28,15 @@ configOverrides:
resource: "aggregation-deployment.yaml" resource: "aggregation-deployment.yaml"
variableName: "env" variableName: "env"
value: "prod" 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment