From 6d944a0854f7521bd99875669d16eff9f823c8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Henning?= <soeren.henning@email.uni-kiel.de> Date: Thu, 1 Dec 2022 16:51:19 +0100 Subject: [PATCH] Internal refactoring of patcher factory --- .../patcher/AbstractResourcePatcher.kt | 10 +-- .../kubernetes/patcher/PatcherFactory.kt | 78 ++++++++----------- .../patcher/ResourceLimitPatcher.kt | 2 +- .../patcher/ResourceRequestPatcher.kt | 3 +- 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/AbstractResourcePatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/AbstractResourcePatcher.kt index daa873f17..adb5c1468 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/AbstractResourcePatcher.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/AbstractResourcePatcher.kt @@ -28,12 +28,12 @@ abstract class AbstractResourcePatcher( when (resource) { is Deployment -> { resource.spec.template.spec.containers.filter { it.name == container }.forEach { - setLimits(it, value) + setValues(it, value) } } is StatefulSet -> { resource.spec.template.spec.containers.filter { it.name == container }.forEach { - setLimits(it, value) + setValues(it, value) } } else -> { @@ -43,7 +43,7 @@ abstract class AbstractResourcePatcher( return resource } - private fun setLimits(container: Container, value: String) { + private fun setValues(container: Container, value: String) { val quantity = if (this.format != null || this.factor != null) { val amountAsInt = value.toIntOrNull()?.times(this.factor ?: 1) if (amountAsInt == null) { @@ -56,8 +56,8 @@ abstract class AbstractResourcePatcher( Quantity(value) } - setLimits(container, quantity) + setValues(container, quantity) } - abstract fun setLimits(container: Container, quantity: Quantity) + abstract fun setValues(container: Container, quantity: Quantity) } \ No newline at end of file diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatcherFactory.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatcherFactory.kt index 69a2528bb..26e76ccfa 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatcherFactory.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/PatcherFactory.kt @@ -12,86 +12,76 @@ class PatcherFactory { * Create patcher based on the given [PatcherDefinition] and * the list of KubernetesResources. * - * @param patcherDefinition The [PatcherDefinition] for which are - * [Patcher] should be created. - * @param k8sResources List of all available Kubernetes resources. - * This is a list of pairs<String, KubernetesResource>: - * The frist corresponds to the filename where the resource is defined. - * The second corresponds to the concrete [KubernetesResource] that should be patched. + * @param patcher The [PatcherDefinition] for which are [Patcher] should be created. * @return The created [Patcher]. * @throws IllegalArgumentException if no patcher can be created. */ - fun createPatcher( - patcherDefinition: PatcherDefinition, - ): Patcher { - - return try { - when (patcherDefinition.type) { + fun createPatcher(patcher: PatcherDefinition): Patcher { + return when (patcher.type) { "ReplicaPatcher" -> ReplicaPatcher( ) "NumNestedGroupsLoadGeneratorReplicaPatcher" -> NumNestedGroupsLoadGeneratorReplicaPatcher( - loadGenMaxRecords = patcherDefinition.properties["loadGenMaxRecords"]!!, - numSensors = patcherDefinition.properties["numSensors"]!! + loadGenMaxRecords = patcher.properties["loadGenMaxRecords"] ?: throwInvalid(patcher), + numSensors = patcher.properties["numSensors"] ?: throwInvalid(patcher) ) "NumSensorsLoadGeneratorReplicaPatcher" -> NumSensorsLoadGeneratorReplicaPatcher( - loadGenMaxRecords = patcherDefinition.properties["loadGenMaxRecords"]!! + loadGenMaxRecords = patcher.properties["loadGenMaxRecords"] ?: throwInvalid(patcher) ) "DataVolumeLoadGeneratorReplicaPatcher" -> DataVolumeLoadGeneratorReplicaPatcher( - maxVolume = patcherDefinition.properties["maxVolume"]!!.toInt(), - container = patcherDefinition.properties["container"]!!, - variableName = patcherDefinition.properties["variableName"]!! + maxVolume = (patcher.properties["maxVolume"] ?: throwInvalid(patcher)).toInt(), + container = patcher.properties["container"] ?: throwInvalid(patcher), + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "EnvVarPatcher" -> EnvVarPatcher( - container = patcherDefinition.properties["container"]!!, - variableName = patcherDefinition.properties["variableName"]!! + container = patcher.properties["container"] ?: throwInvalid(patcher), + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "NodeSelectorPatcher" -> NodeSelectorPatcher( - variableName = patcherDefinition.properties["variableName"]!! + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "ResourceLimitPatcher" -> ResourceLimitPatcher( - container = patcherDefinition.properties["container"]!!, - limitedResource = patcherDefinition.properties["limitedResource"]!!, - format = patcherDefinition.properties["format"], - factor = patcherDefinition.properties["factor"]?.toInt() + container = patcher.properties["container"] ?: throwInvalid(patcher), + limitedResource = patcher.properties["limitedResource"] ?: throwInvalid(patcher), + format = patcher.properties["format"], + factor = patcher.properties["factor"]?.toInt() ) "ResourceRequestPatcher" -> ResourceRequestPatcher( - container = patcherDefinition.properties["container"]!!, - requestedResource = patcherDefinition.properties["requestedResource"]!!, - format = patcherDefinition.properties["format"], - factor = patcherDefinition.properties["factor"]?.toInt() + container = patcher.properties["container"] ?: throwInvalid(patcher), + requestedResource = patcher.properties["requestedResource"] ?: throwInvalid(patcher), + format = patcher.properties["format"], + factor = patcher.properties["factor"]?.toInt() ) "SchedulerNamePatcher" -> SchedulerNamePatcher() "LabelPatcher" -> LabelPatcher( - variableName = patcherDefinition.properties["variableName"]!! + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "MatchLabelPatcher" -> MatchLabelPatcher( - variableName = patcherDefinition.properties["variableName"]!! + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "TemplateLabelPatcher" -> TemplateLabelPatcher( - variableName = patcherDefinition.properties["variableName"]!! + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "ImagePatcher" -> ImagePatcher( - container = patcherDefinition.properties["container"]!! + container = patcher.properties["container"] ?: throwInvalid(patcher) ) "ConfigMapYamlPatcher" -> ConfigMapYamlPatcher( - fileName = patcherDefinition.properties["fileName"]!!, - variableName = patcherDefinition.properties["variableName"]!! + fileName = patcher.properties["fileName"] ?: throwInvalid(patcher), + variableName = patcher.properties["variableName"] ?: throwInvalid(patcher) ) "NamePatcher" -> NamePatcher() "ServiceSelectorPatcher" -> ServiceSelectorPatcher( - variableName = patcherDefinition.properties["label"]!! + variableName = patcher.properties["label"] ?: throwInvalid(patcher) ) "VolumesConfigMapPatcher" -> VolumesConfigMapPatcher( - volumeName = patcherDefinition.properties["volumeName"]!! + volumeName = patcher.properties["volumeName"] ?: throwInvalid(patcher) ) - else -> throw InvalidPatcherConfigurationException("Patcher type ${patcherDefinition.type} not found.") + else -> throw InvalidPatcherConfigurationException("Patcher type ${patcher.type} not found.") } - } catch (e: NullPointerException) { - throw InvalidPatcherConfigurationException( - "Could not create patcher with type ${patcherDefinition.type}" + - " Probably a required patcher argument was not specified.", e - ) - } + } + + private fun throwInvalid(patcher: PatcherDefinition): String { + throw InvalidPatcherConfigurationException("Could not create patcher with type ${patcher.type}. Probably a required patcher argument was not specified.") } } + } diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt index c1ae22f00..b2085ed80 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceLimitPatcher.kt @@ -24,7 +24,7 @@ class ResourceLimitPatcher( format = format, factor = factor ) { - override fun setLimits(container: Container, quantity: Quantity) { + override fun setValues(container: Container, quantity: Quantity) { when { container.resources == null -> { val resource = ResourceRequirements() diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt index a6b037dde..3441ef2a9 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/ResourceRequestPatcher.kt @@ -4,7 +4,6 @@ import io.fabric8.kubernetes.api.model.Container import io.fabric8.kubernetes.api.model.Quantity import io.fabric8.kubernetes.api.model.ResourceRequirements - /** * The Resource request [Patcher] sets resource requests for Deployments and StatefulSets. * @@ -25,7 +24,7 @@ class ResourceRequestPatcher( factor = factor ) { - override fun setLimits(container: Container, quantity: Quantity) { + override fun setValues(container: Container, quantity: Quantity) { when { container.resources == null -> { val resource = ResourceRequirements() -- GitLab