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

Support namespace in delete command and add tests

parent 7af98977
No related branches found
No related tags found
1 merge request!295Support namespace in delete command
Pipeline #9160 passed
......@@ -17,21 +17,22 @@ class DeleteCommand {
lateinit var selector: DeleteActionSelector
fun exec(client: NamespacedKubernetesClient) {
logger.info { "Deleting all resources with apiVersion ${selector.apiVersion} and Kind ${selector.kind} matching regular expression ${selector.nameRegex}" }
logger.info { "Deleting all resources with apiVersion '${selector.apiVersion}' and kind '${selector.kind}' in namespace '${selector.namespace}' matching regular expression '${selector.nameRegex}'." }
val regExp = selector.nameRegex.toRegex()
val k8sManager = K8sManager(client)
//val k8sManager = K8sManager(client)
client
.genericKubernetesResources(selector.apiVersion, selector.kind)
.inNamespace(client.namespace)
.inNamespace(selector.namespace ?: client.namespace)
.list()
.items
.filter { regExp.matches(it.metadata.name) }
.forEach {
logger.info { "Deleting resource ${it.metadata.name} of Kind ${it.kind} and api/version ${it.apiVersion}." }
logger.info { "Deleting resource '${it.kind}/${it.metadata.name}'." }
try {
k8sManager.remove(it)
client.resource(it).delete()
// k8sManager.remove(it)
} catch (e: KubernetesClientException) {
logger.error { "An error occured when deleting resource ${it.metadata.name} of Kind ${it.kind} and api/version ${it.apiVersion}. Error: ${e.message}"}
logger.warn(e) { "An error occurred when deleting resource '${it.kind}/${it.metadata.name}'." }
}
}
}
......@@ -40,10 +41,17 @@ class DeleteCommand {
@JsonDeserialize
@RegisterForReflection
class DeleteActionSelector {
@JsonInclude(JsonInclude.Include.NON_NULL)
lateinit var apiVersion: String
@JsonInclude(JsonInclude.Include.NON_NULL)
lateinit var kind: String
@JsonInclude(JsonInclude.Include.NON_NULL)
var namespace: String? = null
@JsonInclude(JsonInclude.Include.NON_NULL)
lateinit var nameRegex: String
}
\ No newline at end of file
package rocks.theodolite.kubernetes
import io.fabric8.kubernetes.api.model.ConfigMapBuilder
import io.fabric8.kubernetes.api.model.NamespaceBuilder
import io.fabric8.kubernetes.client.server.mock.KubernetesServer
import io.quarkus.test.junit.QuarkusTest
import io.quarkus.test.kubernetes.client.KubernetesTestServer
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
@QuarkusTest
@WithKubernetesTestServer
internal class DeleteCommandTest {
@KubernetesTestServer
private lateinit var server: KubernetesServer
@Test
fun testDelete() {
val configMap = ConfigMapBuilder()
.withNewMetadata().withName("test-configmap").endMetadata()
.withData<String, String>(mapOf(
"myfile.txt" to "some content"
)).build()
server.client.configMaps().create(configMap)
assertEquals(1, server.client.configMaps().list().items.size)
val command = DeleteCommand().also {
it.selector = DeleteActionSelector().also {
it.kind = configMap.kind
it.apiVersion = configMap.apiVersion
it.nameRegex = "^test.*"
}
}
command.exec(server.client)
assertEquals(0, server.client.configMaps().list().items.size)
}
@Test
fun testDeleteOtherNamespace() {
server.client.namespaces().create(
NamespaceBuilder().withNewMetadata().withName("other-namespace").endMetadata().build()
)
val configMap = ConfigMapBuilder()
.withNewMetadata()
.withName("test-configmap")
.withNamespace("other-namespace")
.endMetadata()
.withData<String, String>(mapOf(
"myfile.txt" to "some content"
)).build()
server.client.configMaps().inNamespace("other-namespace").create(configMap)
assertEquals(1, server.client.configMaps().inNamespace("other-namespace").list().items.size)
val command = DeleteCommand().also {
it.selector = DeleteActionSelector().also {
it.kind = configMap.kind
it.apiVersion = configMap.apiVersion
it.nameRegex = "^test.*"
it.namespace = "other-namespace"
}
}
// Use other namespace than for ConfigMap to verify that selector namespace is used
command.exec(server.client.inNamespace("client-namespace"))
assertEquals(0, server.client.configMaps().inNamespace("other-namespace").list().items.size)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment