diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/VolumesConfigMapPatcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/VolumesConfigMapPatcher.kt index 54ac12be7629da792cbb72b2f1d409ba0f4ba93c..a0483a5d4576831848a62dcecb5c987c8ce8656a 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/VolumesConfigMapPatcher.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/patcher/VolumesConfigMapPatcher.kt @@ -7,37 +7,28 @@ import io.fabric8.kubernetes.api.model.apps.StatefulSet class VolumesConfigMapPatcher(private var volumeName: String) : AbstractStringPatcher() { override fun patchSingleResource(resource: HasMetadata, value: String): HasMetadata { - if (resource is Deployment) { - if (resource.spec.template.spec.volumes == null) { - resource.spec.template.spec.volumes = mutableListOf() + val volumeMounts = when(resource) { + is Deployment -> { + if (resource.spec.template.spec.volumes == null) { + resource.spec.template.spec.volumes = mutableListOf() + } + resource.spec.template.spec.volumes } - val volumeMounts = resource.spec.template.spec.volumes - - for (mount in volumeMounts) { - try { - if (mount.configMap.name == volumeName) { - mount.configMap.name = value - } - } catch (_: NullPointerException) { + is StatefulSet -> { + if (resource.spec.template.spec.volumes == null) { + resource.spec.template.spec.volumes = mutableListOf() } + resource.spec.template.spec.volumes } + else -> emptyList() // No volumes to patch } - if (resource is StatefulSet) { - if (resource.spec.template.spec.volumes == null) { - resource.spec.template.spec.volumes = mutableListOf() - } - val volumeMounts = resource.spec.template.spec.volumes - for (mount in volumeMounts) { - try { - if (mount.configMap.name == volumeName) { - mount.configMap.name = value - } - } catch (_: NullPointerException) { - } + for (mount in volumeMounts) { + // Find ConfigMap volume with requested name + if (mount.configMap?.name?.equals(volumeName) == true) { + mount.configMap.name = value } } - return resource } } \ No newline at end of file