From ee03ab95512a8135ec030b9e2dcff42b51419c2a Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Tue, 31 Aug 2021 21:35:48 +0200
Subject: [PATCH] Fix bug when stopping executions

---
 .../execution/operator/AbstractStateHandler.kt  | 17 ++++++++++++-----
 .../execution/operator/ExecutionEventHandler.kt |  2 +-
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/AbstractStateHandler.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/AbstractStateHandler.kt
index 9d7436526..0b5d6040b 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/operator/AbstractStateHandler.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/operator/AbstractStateHandler.kt
@@ -4,10 +4,13 @@ import io.fabric8.kubernetes.api.model.HasMetadata
 import io.fabric8.kubernetes.api.model.KubernetesResourceList
 import io.fabric8.kubernetes.api.model.Namespaced
 import io.fabric8.kubernetes.client.CustomResource
+import io.fabric8.kubernetes.client.KubernetesClientException
 import io.fabric8.kubernetes.client.NamespacedKubernetesClient
 import io.fabric8.kubernetes.client.dsl.MixedOperation
 import io.fabric8.kubernetes.client.dsl.Resource
+import mu.KotlinLogging
 import java.lang.Thread.sleep
+private val logger = KotlinLogging.logger {}
 
 abstract class AbstractStateHandler<T, L, D>(
     private val client: NamespacedKubernetesClient,
@@ -20,11 +23,15 @@ abstract class AbstractStateHandler<T, L, D>(
 
     @Synchronized
     override fun setState(resourceName: String, f: (T) -> T?) {
-        this.crdClient
-            .list().items
-            .filter { it.metadata.name == resourceName }
-            .map { customResource -> f(customResource) }
-            .forEach { this.crdClient.updateStatus(it) }
+        try {
+            this.crdClient
+                .list().items
+                .filter { it.metadata.name == resourceName }
+                .map { customResource -> f(customResource) }
+                .forEach { this.crdClient.updateStatus(it) }
+        } catch (e: KubernetesClientException) {
+            logger.warn { "Status cannot be set for resource $resourceName" }
+        }
     }
 
     @Synchronized
diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/ExecutionEventHandler.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/ExecutionEventHandler.kt
index 62c1ddd4e..1209195ee 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/operator/ExecutionEventHandler.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/operator/ExecutionEventHandler.kt
@@ -80,7 +80,7 @@ class ExecutionHandler(
     override fun onDelete(execution: ExecutionCRD, b: Boolean) {
         logger.info { "Delete execution ${execution.metadata.name}" }
         if (execution.status.executionState == States.RUNNING.value
-            && this.controller.isExecutionRunning(execution.spec.name)
+            && this.controller.isExecutionRunning(execution.metadata.name)
         ) {
             this.controller.stop()
         }
-- 
GitLab