From c76b3e4ba4843a95b2a7b099e423cc3466bb66e9 Mon Sep 17 00:00:00 2001 From: lorenz <stu203404@mail.uni-kiel.de> Date: Tue, 13 Apr 2021 15:29:18 +0200 Subject: [PATCH] Fix ServiceMonitorWrapper.kt, Also fix to correct usage of tread{} on the ShutdownHook --- .../kotlin/theodolite/execution/Shutdown.kt | 2 +- .../execution/TheodoliteYamlExecutor.kt | 8 +++-- .../theodolite/k8s/ServiceMonitorWrapper.kt | 32 +++++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt index 589dee1ac..92aaef292 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/Shutdown.kt @@ -23,4 +23,4 @@ class Shutdown(private val benchmarkExecution: BenchmarkExecution, private val b deployment.teardown() logger.info { "Teardown completed" } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt index eec7796e4..6bddea20c 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt @@ -28,13 +28,15 @@ class TheodoliteYamlExecutor { val benchmark = parser.parse(path = benchmarkPath, E = KubernetesBenchmark::class.java)!! - val shutdown = Shutdown(benchmarkExecution, benchmark) - Runtime.getRuntime().addShutdownHook(thread { shutdown.run()}) + // Add shutdown hook + // Use thread{} with start = false, else the thread will start right away + val shutdown = thread(start = false) { Shutdown(benchmarkExecution, benchmark).run() } + Runtime.getRuntime().addShutdownHook(shutdown) val executor = TheodoliteExecutor(benchmarkExecution, benchmark) executor.run() logger.info { "Theodolite finished" } - Runtime.getRuntime().removeShutdownHook(thread { shutdown.run()}) + Runtime.getRuntime().removeShutdownHook(shutdown) exitProcess(0) } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt index 7b1a0d0b3..4950cee22 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/ServiceMonitorWrapper.kt @@ -1,22 +1,27 @@ package theodolite.k8s -import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.client.CustomResource import io.fabric8.kubernetes.client.NamespacedKubernetesClient import mu.KotlinLogging -import java.lang.Exception private val logger = KotlinLogging.logger {} -class ServiceMonitorWrapper(private val serviceMonitor: Map<String,String>): CustomResource() { +class ServiceMonitorWrapper(private val serviceMonitor: Map<String, String>) : CustomResource() { /** * Deploy a service monitor * * @param client a namespaced Kubernetes client which are used to deploy the CR object. + * + * @throws java.io.IOException if the resource could not be deployed. */ fun deploy(client: NamespacedKubernetesClient) { - val serviceMonitorContext = K8sContextFactory().create(api = "v1", scope = "Namespaced", group = "monitoring.coreos.com", plural = "servicemonitors") + val serviceMonitorContext = K8sContextFactory().create( + api = "v1", + scope = "Namespaced", + group = "monitoring.coreos.com", + plural = "servicemonitors" + ) client.customResource(serviceMonitorContext) .createOrReplace(client.configuration.namespace, this.serviceMonitor as Map<String, Any>) } @@ -27,16 +32,25 @@ class ServiceMonitorWrapper(private val serviceMonitor: Map<String,String>): Cus * @param client a namespaced Kubernetes client which are used to delete the CR object. */ fun delete(client: NamespacedKubernetesClient) { - val serviceMonitorContext = K8sContextFactory().create(api = "v1", scope = "Namespaced", group = "monitoring.coreos.com", plural = "servicemonitors") + val serviceMonitorContext = K8sContextFactory().create( + api = "v1", + scope = "Namespaced", + group = "monitoring.coreos.com", + plural = "servicemonitors" + ) try { - client.customResource(serviceMonitorContext).delete(client.configuration.namespace, this.getServiceMonitorName()) + client.customResource(serviceMonitorContext) + .delete(client.configuration.namespace, this.getServiceMonitorName()) } catch (e: Exception) { logger.warn { "Could not delete service monitor" } } } + /** + * @throws NullPointerException if name or metadata is null + */ private fun getServiceMonitorName(): String { - val smAsMap = this.serviceMonitor as Map<String, String> - return smAsMap["name"] !! + val smAsMap = this.serviceMonitor["metadata"]!! as Map<String, String> + return smAsMap["name"]!! } -} \ No newline at end of file +} -- GitLab