Skip to content
Snippets Groups Projects
Commit 3525a05c authored by Benedikt Wetzel's avatar Benedikt Wetzel
Browse files

minor code changes

parent b0d57fe2
No related branches found
No related tags found
1 merge request!173Introduce events in order to make it possible to see simple log message with...
...@@ -5,34 +5,40 @@ import io.fabric8.kubernetes.api.model.EventSource ...@@ -5,34 +5,40 @@ import io.fabric8.kubernetes.api.model.EventSource
import io.fabric8.kubernetes.api.model.ObjectReference import io.fabric8.kubernetes.api.model.ObjectReference
import io.fabric8.kubernetes.client.DefaultKubernetesClient import io.fabric8.kubernetes.client.DefaultKubernetesClient
import io.fabric8.kubernetes.client.NamespacedKubernetesClient import io.fabric8.kubernetes.client.NamespacedKubernetesClient
import mu.KotlinLogging
import theodolite.util.Config
import theodolite.util.Configuration
import java.time.Instant import java.time.Instant
import java.util.* import java.util.*
import kotlin.NoSuchElementException
private val logger = KotlinLogging.logger {}
class EventCreator { 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) { fun createEvent(executionName: String, type: String, message: String, reason: String) {
val uuid = UUID.randomUUID().toString() val uuid = UUID.randomUUID().toString()
println("uuid is: " + uuid) try {
val objectRef = buildObjectReference(executionName) val objectRef = buildObjectReference(executionName)
val event = EventBuilder() val event = EventBuilder()
.withNewMetadata() .withNewMetadata()
.withName(uuid) .withName(uuid)
.endMetadata() .endMetadata()
.withMessage(message)
.withReason(reason)
.withType(type)
.withFirstTimestamp(Instant.now().toString()) // TODO change datetime format
.build() .build()
event.message = message
event.reason = reason
event.type = type
event.firstTimestamp = Instant.now().toString()
val source = EventSource() val source = EventSource()
source.component = "theodolite-operator" source.component = Configuration.COMPONENT_NAME
event.source = source event.source = source
event.involvedObject = objectRef event.involvedObject = objectRef
client.v1().events().inNamespace("default").create(event) 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 { private fun buildObjectReference(executionName: String): ObjectReference {
......
...@@ -11,6 +11,7 @@ import theodolite.model.crd.BenchmarkCRD ...@@ -11,6 +11,7 @@ import theodolite.model.crd.BenchmarkCRD
import theodolite.model.crd.BenchmarkExecutionList import theodolite.model.crd.BenchmarkExecutionList
import theodolite.model.crd.ExecutionCRD import theodolite.model.crd.ExecutionCRD
import theodolite.model.crd.KubernetesBenchmarkList import theodolite.model.crd.KubernetesBenchmarkList
import theodolite.util.Configuration
private const val DEFAULT_NAMESPACE = "default" private const val DEFAULT_NAMESPACE = "default"
...@@ -27,7 +28,7 @@ private val logger = KotlinLogging.logger {} ...@@ -27,7 +28,7 @@ private val logger = KotlinLogging.logger {}
* **See Also:** [Kubernetes Operator Pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) * **See Also:** [Kubernetes Operator Pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)
*/ */
class TheodoliteOperator { class TheodoliteOperator {
private val namespace = System.getenv("NAMESPACE") ?: DEFAULT_NAMESPACE private val namespace = Configuration.NAMESPACE
private val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace(namespace) private val client: NamespacedKubernetesClient = DefaultKubernetesClient().inNamespace(namespace)
private lateinit var controller: TheodoliteController private lateinit var controller: TheodoliteController
...@@ -37,7 +38,7 @@ class TheodoliteOperator { ...@@ -37,7 +38,7 @@ class TheodoliteOperator {
fun start() { fun start() {
LeaderElector( LeaderElector(
client = client, client = client,
name = "theodolite-operator" // TODO(make leaslock name configurable via env var) name = Configuration.COMPONENT_NAME
) )
.getLeadership(::startOperator) .getLeadership(::startOperator)
} }
......
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
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment