From 14a594a1b22724cdde454cf94e81a90788aa3598 Mon Sep 17 00:00:00 2001 From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de> Date: Wed, 17 Feb 2021 20:40:50 +0100 Subject: [PATCH] create first prototypical patcher --- .../benchmark/BenchmarkYamlParser.kt | 2 +- .../benchmark/KubernetesBenchmark.kt | 45 +++++++++++++------ .../theodolite/patcher/AbstractPatcher.kt | 6 +++ .../main/kotlin/theodolite/patcher/Patcher.kt | 5 +++ .../theodolite/patcher/ReplicaPatcher.kt | 11 +++++ .../theodolite/{benchmark => util}/Parser.kt | 2 +- .../theodolite/util/PatcherDefinition.kt | 8 ++++ .../main/kotlin/theodolite/util/TypeName.kt | 6 +++ .../resources/yaml/testBenchmarkType.yaml | 7 ++- 9 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt rename theodolite-quarkus/src/main/kotlin/theodolite/{benchmark => util}/Parser.kt (72%) create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkYamlParser.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkYamlParser.kt index 723898d76..51e0f1aa3 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkYamlParser.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkYamlParser.kt @@ -1,8 +1,8 @@ package theodolite.benchmark import org.yaml.snakeyaml.Yaml -import org.yaml.snakeyaml.constructor.BaseConstructor import org.yaml.snakeyaml.constructor.Constructor +import theodolite.util.Parser import java.io.File import java.io.FileInputStream import java.io.InputStream diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt index 09e820e51..be6327830 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt @@ -1,43 +1,60 @@ package theodolite.benchmark import io.fabric8.kubernetes.api.model.KubernetesResource +import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.client.DefaultKubernetesClient import org.yaml.snakeyaml.Yaml import theodolite.k8s.YamlLoader +import theodolite.patcher.AbstractPatcher +import theodolite.patcher.Patcher +import theodolite.patcher.ReplicaPatcher import theodolite.util.LoadDimension +import theodolite.util.PatcherDefinition import theodolite.util.Resource +import theodolite.util.TypeName import java.io.File import java.io.FileInputStream import java.io.InputStream +import java.lang.IllegalArgumentException class KubernetesBenchmark(): Benchmark { lateinit var name: String lateinit var appResource: List<String> lateinit var loadGenResource: List<String> + lateinit var resourceTypes: List<TypeName> - private fun loadKubernetesResources(): List<KubernetesResource?> { + private fun loadKubernetesResources(resources: List<String>): List<Pair<String, KubernetesResource?>> { val basePath = "./../../../resources/main/yaml/" var parser = theodolite.benchmark.BenchmarkYamlParser() val loader = YamlLoader(DefaultKubernetesClient().inNamespace("default")) - return this.appResource - .map { resource -> "$basePath/$resource" } - .map { resourcePath -> + return resources + .map { resource -> + val resourcePath = "$basePath/$resource" val kind = parser.parse(resourcePath, HashMap<String, String>()::class.java) !! - kind["kind"]?.let { loader.loadK8sResource(it, resourcePath) } - } + val k8sResource = kind["kind"]?.let { loader.loadK8sResource(it, resourcePath) } + Pair<String, KubernetesResource?>(resource, k8sResource) + } } - private fun patchKubernetesResources() { - + private fun createK8sPatcher(patcherDefinition: PatcherDefinition, k8sResources: List<Pair<String, KubernetesResource>>): Patcher<Int> { + return when(patcherDefinition.type) { + "ReplicaPatcher" -> ReplicaPatcher(k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0]) + "EnvVarPatcher" -> TODO("create env var patcher") + else -> throw IllegalArgumentException("Patcher type ${patcherDefinition.type} not fount") + } + TODO("Use reflection to load patchers") } override fun buildDeployment(load: LoadDimension, res: Resource, override: Map<String, String>): BenchmarkDeployment { - // TODO("") - val resources = loadKubernetesResources() - resources.forEach {x -> println(x.toString())} - // Return KubernetesBenchmarkDeployment with individual parametrisation - return KubernetesBenchmarkDeployment(emptyList(), hashMapOf<String, Any>(), "", emptyList() ) + // TODO("set node selector") + val resources = loadKubernetesResources(this.appResource + this.loadGenResource) + val patchers = this.resourceTypes.map { patcherDef -> createK8sPatcher(patcherDef.patchers[0], + resources as List<Pair<String, KubernetesResource>> + ) } + // exemplary only for replica patcher + patchers.forEach{ patcher -> patcher.patch(res.get()) } + return KubernetesBenchmarkDeployment(emptyList(), hashMapOf<String, Any>(), "", emptyList()) } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt new file mode 100644 index 000000000..32819549b --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt @@ -0,0 +1,6 @@ +package theodolite.patcher + +import io.fabric8.kubernetes.api.model.KubernetesResource + +abstract class AbstractPatcher<T>(k8sResource: KubernetesResource): Patcher<T> { +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt new file mode 100644 index 000000000..0c1fa5b8a --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt @@ -0,0 +1,5 @@ +package theodolite.patcher + +interface Patcher<T> { + fun patch(value: T) +} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt new file mode 100644 index 000000000..1d9dfb24e --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt @@ -0,0 +1,11 @@ +package theodolite.patcher + +import io.fabric8.kubernetes.api.model.KubernetesResource +import io.fabric8.kubernetes.api.model.apps.Deployment + +class ReplicaPatcher(private val k8sResource: KubernetesResource): AbstractPatcher<Int>(k8sResource){ + override fun patch(replicas: Int) { + if (k8sResource is Deployment) + this.k8sResource.spec.replicas = replicas + } +} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Parser.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt similarity index 72% rename from theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Parser.kt rename to theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt index 93bf4a795..6e0803e6d 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Parser.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt @@ -1,4 +1,4 @@ -package theodolite.benchmark +package theodolite.util interface Parser { fun <T> parse(path: String, E:Class<T>): T? //Yaml diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt new file mode 100644 index 000000000..e1bbc64bd --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt @@ -0,0 +1,8 @@ +package theodolite.util + +class PatcherDefinition() { + lateinit var type: String + lateinit var resource: String + lateinit var container: String + lateinit var variableName: String +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt new file mode 100644 index 000000000..f4a65bb58 --- /dev/null +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt @@ -0,0 +1,6 @@ +package theodolite.util + +class TypeName() { + lateinit var typeName: String + lateinit var patchers: List<PatcherDefinition> +} \ No newline at end of file diff --git a/theodolite-quarkus/src/main/resources/yaml/testBenchmarkType.yaml b/theodolite-quarkus/src/main/resources/yaml/testBenchmarkType.yaml index 8b34de6d5..02c35964e 100644 --- a/theodolite-quarkus/src/main/resources/yaml/testBenchmarkType.yaml +++ b/theodolite-quarkus/src/main/resources/yaml/testBenchmarkType.yaml @@ -3,4 +3,9 @@ appResource: - "aggregation-deployment.yaml" - "aggregation-service.yaml" loadGenResource: - - "workloadGenerator.yaml" \ No newline at end of file + - "workloadGenerator.yaml" +resourceTypes: + - typeName: "Instances" + patchers: + - type: "ReplicaPatcher" + resource: "aggregation-deployment.yaml" \ No newline at end of file -- GitLab