diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt index 24d221e4486f5f9c07a63af5b3087ce91480e809..69b21155d939c4e17b4da21f12f9d4e4d7e993f5 100644 --- a/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt +++ b/theodolite/src/main/kotlin/theodolite/execution/operator/EventCreator.kt @@ -5,34 +5,40 @@ import io.fabric8.kubernetes.api.model.EventSource import io.fabric8.kubernetes.api.model.ObjectReference import io.fabric8.kubernetes.client.DefaultKubernetesClient import io.fabric8.kubernetes.client.NamespacedKubernetesClient +import mu.KotlinLogging +import theodolite.util.Config +import theodolite.util.Configuration import java.time.Instant import java.util.* +import kotlin.NoSuchElementException +private val logger = KotlinLogging.logger {} class EventCreator { - val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace("default") + val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace(Configuration.NAMESPACE) fun createEvent(executionName: String, type: String, message: String, reason: String) { val uuid = UUID.randomUUID().toString() - println("uuid is: " + uuid) - val objectRef = buildObjectReference(executionName) - val event = EventBuilder() - .withNewMetadata() + try { + val objectRef = buildObjectReference(executionName) + val event = EventBuilder() + .withNewMetadata() .withName(uuid) - .endMetadata() - .build() - - event.message = message - event.reason = reason - event.type = type - - event.firstTimestamp = Instant.now().toString() - - val source = EventSource() - source.component = "theodolite-operator" - event.source = source - - event.involvedObject = objectRef - client.v1().events().inNamespace("default").create(event) + .endMetadata() + .withMessage(message) + .withReason(reason) + .withType(type) + .withFirstTimestamp(Instant.now().toString()) // TODO change datetime format + .build() + + val source = EventSource() + source.component = Configuration.COMPONENT_NAME + event.source = source + + event.involvedObject = objectRef + client.v1().events().inNamespace(Configuration.NAMESPACE).createOrReplace(event) + } catch (e: NoSuchElementException) { + logger.warn {"Could not create event: type: $type, message: $message, reason: $reason, no corresponding execution found."} + } } private fun buildObjectReference(executionName: String): ObjectReference { diff --git a/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt b/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt index 6afd8451df954f68d199b7d8cba6436e29241e68..cbce5749f39577a5e99bb773eff9ff8a3541ad8b 100644 --- a/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt +++ b/theodolite/src/main/kotlin/theodolite/execution/operator/TheodoliteOperator.kt @@ -11,6 +11,7 @@ import theodolite.model.crd.BenchmarkCRD import theodolite.model.crd.BenchmarkExecutionList import theodolite.model.crd.ExecutionCRD import theodolite.model.crd.KubernetesBenchmarkList +import theodolite.util.Configuration private const val DEFAULT_NAMESPACE = "default" @@ -27,7 +28,7 @@ private val logger = KotlinLogging.logger {} * **See Also:** [Kubernetes Operator Pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) */ class TheodoliteOperator { - private val namespace = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE + private val namespace = Configuration.NAMESPACE private val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace(namespace) private lateinit var controller: TheodoliteController @@ -37,7 +38,7 @@ class TheodoliteOperator { fun start() { LeaderElector( client = client, - name = "theodolite-operator" // TODO(make leaslock name configurable via env var) + name = Configuration.COMPONENT_NAME ) .getLeadership(::startOperator) } diff --git a/theodolite/src/main/kotlin/theodolite/util/Configuration.kt b/theodolite/src/main/kotlin/theodolite/util/Configuration.kt new file mode 100644 index 0000000000000000000000000000000000000000..5bc1edd1954726146ac32dd2139459d60e013d2b --- /dev/null +++ b/theodolite/src/main/kotlin/theodolite/util/Configuration.kt @@ -0,0 +1,15 @@ +package theodolite.util + +// Defaults +private const val DEFAULT_NAMESPACE = "default" +private const val DEFAULT_COMPONENT_NAME = "theodolite-operator" + + +class Configuration( +) { + companion object { + val NAMESPACE = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE + val COMPONENT_NAME = System.getenv("COMPONENT_NAME") ?: DEFAULT_COMPONENT_NAME + } + +}