From b601bd5485595b50b08ec9629f6b40f9d3c5cb6c Mon Sep 17 00:00:00 2001 From: Mario-Leander Reimer Date: Sat, 30 Apr 2016 21:33:04 +0200 Subject: [PATCH] Improved handling of Pod events and synced square button color. Closes #1. --- .../de/qaware/cloud/nativ/k8s/ClusterNode.kt | 8 ++++- .../qaware/cloud/nativ/k8s/ClusterNodeGrid.kt | 33 +++++++++++-------- .../cloud/nativ/k8s/KubepadApplication.kt | 6 ++-- .../cloud/nativ/k8s/KubernetesDeployments.kt | 5 +++ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNode.kt b/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNode.kt index e0e85c5..480c7d2 100644 --- a/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNode.kt +++ b/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNode.kt @@ -38,9 +38,15 @@ data class ClusterNode(val row: Int, val column: Int, return this } + fun update(p: Phase) { + if (active.get()) { + phase = p + } + } + fun deactivate(): ClusterNode { active.compareAndSet(true, false) - phase = Phase.Unknown + phase = Phase.Terminated return this } diff --git a/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNodeGrid.kt b/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNodeGrid.kt index 3959980..177fb9c 100644 --- a/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNodeGrid.kt +++ b/src/main/kotlin/de/qaware/cloud/nativ/k8s/ClusterNodeGrid.kt @@ -30,6 +30,7 @@ import org.slf4j.Logger import java.util.concurrent.ExecutorService import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit +import javax.annotation.PreDestroy import javax.enterprise.context.ApplicationScoped import javax.enterprise.event.Event import javax.enterprise.event.Observes @@ -86,19 +87,27 @@ open class ClusterNodeGrid @Inject constructor(@Named("default") } } + @PreDestroy + open fun shutdown() { + deployments.clear() + grid.forEach { row -> + row.forEach { it.deactivate() } + } + } + private fun watch(row: Int, labels: MutableMap) { scheduler.scheduleWithFixedDelay({ if (deployments.deployments()[row] == null ) { throw IllegalStateException("No deployment at $row to watch.") } - val nodes = grid[row] - val active = nodes.filter { it.active.get() } + pods.all(labels).forEachIndexed { j, pod -> + val nodes = grid[row] + val active = nodes.filter { it.active.get() } - pods.all(labels).reversed().forEachIndexed { j, pod -> val node = active.elementAtOrElse(j) { nodes[next(row)].activate() } if (node.active.get()) { - node.phase = phase(pod) + node.update(phase(pod)) updateClusterNode(row, node) } } @@ -131,10 +140,8 @@ open class ClusterNodeGrid @Inject constructor(@Named("default") val node = grid[event.row][event.column] val running = grid[event.row].count { it.active.get() } - executor.execute({ - updateClusterNode(event.row, node.activate()) - deployments.scale(event.row, running + 1) - }) + updateClusterNode(event.row, node.activate()) + executor.execute { deployments.scale(event.row, running + 1) } } /** @@ -148,12 +155,10 @@ open class ClusterNodeGrid @Inject constructor(@Named("default") val node = grid[event.row][event.column] val running = grid[event.row].count { it.active.get() } - executor.execute({ - deployments.scale(event.row, running - 1) - stopping(event.row, node) - node.deactivate() - stopped(event.row, node) - }) + stopping(event.row, node) + node.deactivate() + executor.execute { deployments.scale(event.row, running - 1) } + stopped(event.row, node) } /** diff --git a/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubepadApplication.kt b/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubepadApplication.kt index d3af158..67d81f5 100644 --- a/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubepadApplication.kt +++ b/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubepadApplication.kt @@ -45,9 +45,6 @@ fun main(args: Array) { val cdiContainer = getCdiContainer() cdiContainer.boot(); - // ensure we shutdown nicely on exit - Runtime.getRuntime().addShutdownHook(Thread() { cdiContainer.shutdown() }) - // and initialize the CDI container control val contextControl = cdiContainer.contextControl; contextControl.startContext(Dependent::class.java) @@ -71,4 +68,7 @@ fun main(args: Array) { } else { logger.warning("No Leap Motion support.") } + + // ensure we shutdown nicely on exit + Runtime.getRuntime().addShutdownHook(Thread() { cdiContainer.shutdown() }) } diff --git a/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubernetesDeployments.kt b/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubernetesDeployments.kt index 5ad7d1e..a68613c 100644 --- a/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubernetesDeployments.kt +++ b/src/main/kotlin/de/qaware/cloud/nativ/k8s/KubernetesDeployments.kt @@ -62,6 +62,11 @@ open class KubernetesDeployments @Inject constructor(private val client: Kuberne operation.watch(this) } + open fun clear() { + deployments.clear() + names.clear() + } + open fun deployments(): List = deployments.toList() open operator fun get(row: Int): Deployment? = deployments[row]