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

Minor code quality improvements

parent 79bbcb9c
No related branches found
No related tags found
No related merge requests found
Pipeline #7093 passed
...@@ -18,22 +18,22 @@ class LeaderElector( ...@@ -18,22 +18,22 @@ class LeaderElector(
) { ) {
// TODO(what is the name of the lock? .withName() or LeaseLock(..,name..) ?) // TODO(what is the name of the lock? .withName() or LeaseLock(..,name..) ?)
fun getLeadership(leader: KFunction0<Unit>) { fun getLeadership(leader: () -> Unit) {
val lockIdentity: String = UUID.randomUUID().toString() val lockIdentity: String = UUID.randomUUID().toString()
DefaultKubernetesClient().use { kc -> DefaultKubernetesClient().use { kc ->
kc.leaderElector() kc.leaderElector()
.withConfig( .withConfig(
LeaderElectionConfigBuilder() LeaderElectionConfigBuilder()
.withName("Theodolite") .withName("Theodolite")
.withLeaseDuration(Duration.ofSeconds(15L)) .withLeaseDuration(Duration.ofSeconds(15))
.withLock(LeaseLock(client.namespace, name, lockIdentity)) .withLock(LeaseLock(client.namespace, name, lockIdentity))
.withRenewDeadline(Duration.ofSeconds(10L)) .withRenewDeadline(Duration.ofSeconds(10))
.withRetryPeriod(Duration.ofSeconds(2L)) .withRetryPeriod(Duration.ofSeconds(2))
.withLeaderCallbacks(LeaderCallbacks( .withLeaderCallbacks(LeaderCallbacks(
{ Thread { leader() }.start() }, { Thread { leader() }.start() },
{ logger.info { "STOPPED LEADERSHIP" } } { logger.info { "Stop being the leading operator." } }
) { newLeader: String? -> ) { newLeader: String? ->
logger.info { "New leader elected $newLeader" } logger.info { "New leader elected: $newLeader" }
}) })
.build() .build()
) )
......
...@@ -41,15 +41,14 @@ class TheodoliteOperator { ...@@ -41,15 +41,14 @@ class TheodoliteOperator {
LeaderElector( LeaderElector(
client = client, client = client,
name = Configuration.COMPONENT_NAME name = Configuration.COMPONENT_NAME
) ).getLeadership(::startOperator)
.getLeadership(::startOperator)
} }
/** /**
* Start the operator. * Start the operator.
*/ */
private fun startOperator() { private fun startOperator() {
logger.info { "Using $namespace as namespace." } logger.info { "Becoming the leading operator. Use namespace '$namespace'." }
client.use { client.use {
KubernetesDeserializer.registerCustomKind( KubernetesDeserializer.registerCustomKind(
"$GROUP/$API_VERSION", "$GROUP/$API_VERSION",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment