Skip to content
Snippets Groups Projects
Commit 540544de authored by Sören Henning's avatar Sören Henning
Browse files

Add decorating patcher

parent f742ae6b
No related branches found
No related tags found
No related merge requests found
Pipeline #10329 failed
......@@ -6,12 +6,15 @@ import io.fabric8.kubernetes.client.utils.Serialization
/**
* A Patcher is able to modify values of a Kubernetes resource, see [Patcher].
*/
abstract class AbstractStringPatcher : Patcher {
abstract class AbstractStringPatcher(
val prefix: String = "",
val suffix: String = ""
) : Patcher {
final override fun patch(resources: List<HasMetadata>, value: String) : List<HasMetadata> {
return resources
.map { Serialization.clone(it)}
.map { patchSingleResource(it, value) }
.map { patchSingleResource(it, prefix + value) }
}
abstract fun patchSingleResource(resource: HasMetadata, value: String): HasMetadata
......
......@@ -13,7 +13,7 @@ import org.yaml.snakeyaml.Yaml
*/
class ConfigMapYamlPatcher(
private val fileName: String,
private val variableName: String
private val variableName: String,
) : AbstractStringPatcher() {
override fun patchSingleResource(resource: HasMetadata, value: String): HasMetadata {
......
package rocks.theodolite.kubernetes.patcher
import io.fabric8.kubernetes.api.model.HasMetadata
import mu.KotlinLogging
private val logger = KotlinLogging.logger {}
/**
* A decorating [Patcher] to modifies a value by a factor, a prefix and a suffix. All modifications are optional. The
* factor only has an effect if the supplied value is an integer.
*/
class DecoratingPatcher(
private val innerPatcher: Patcher,
private val prefix: String?,
private val suffix: String?,
private val factor: Int?
) : Patcher {
override fun patch(resources: List<HasMetadata>, value: String) : List<HasMetadata> {
return this.innerPatcher.patch(resources, (prefix ?: "") + multiply(value) + (suffix ?: ""))
}
private fun multiply(value: String): String {
if (this.factor == null) {
return value
}
val valueAsInt = value.toIntOrNull()
if (valueAsInt == null) {
logger.warn { "Patcher value cannot be parsed as Int. Ignoring factor." }
return value
}
return valueAsInt.times(factor).toString()
}
}
......@@ -32,9 +32,14 @@ class PatcherFactory {
container = patcher.properties["container"] ?: throwInvalid(patcher),
variableName = patcher.properties["variableName"] ?: throwInvalid(patcher)
)
"EnvVarPatcher" -> EnvVarPatcher(
container = patcher.properties["container"] ?: throwInvalid(patcher),
variableName = patcher.properties["variableName"] ?: throwInvalid(patcher)
"EnvVarPatcher" -> DecoratingPatcher(
EnvVarPatcher(
container = patcher.properties["container"] ?: throwInvalid(patcher),
variableName = patcher.properties["variableName"] ?: throwInvalid(patcher)
),
prefix = patcher.properties["prefix"],
suffix = patcher.properties["suffix"],
factor = patcher.properties["factor"]?.toInt(),
)
"NodeSelectorPatcher" -> NodeSelectorPatcher(
variableName = patcher.properties["variableName"] ?: throwInvalid(patcher)
......@@ -64,9 +69,14 @@ class PatcherFactory {
"ImagePatcher" -> ImagePatcher(
container = patcher.properties["container"] ?: throwInvalid(patcher)
)
"ConfigMapYamlPatcher" -> ConfigMapYamlPatcher(
fileName = patcher.properties["fileName"] ?: throwInvalid(patcher),
variableName = patcher.properties["variableName"] ?: throwInvalid(patcher)
"ConfigMapYamlPatcher" -> DecoratingPatcher(
ConfigMapYamlPatcher(
fileName = patcher.properties["fileName"] ?: throwInvalid(patcher),
variableName = patcher.properties["variableName"] ?: throwInvalid(patcher)
),
prefix = patcher.properties["prefix"],
suffix = patcher.properties["suffix"],
factor = patcher.properties["factor"]?.toInt(),
)
"NamePatcher" -> NamePatcher()
"ServiceSelectorPatcher" -> ServiceSelectorPatcher(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment