Skip to content

Commit 059601c

Browse files
committed
Spawn new SP instance when config updates
1 parent 283decb commit 059601c

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

src/main/kotlin/eu/openanalytics/shinyproxyoperator/controller/ResourceListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ResourceListener<T : HasMetadata>(private val channel: SendChannel<ShinyPr
4646
val hashOfInstance = resource.metadata.labels[LabelFactory.INSTANCE_LABEL] ?: TODO("Should not happen")
4747
val shinyProxyInstance = shinyProxy.status.getInstanceByHash(hashOfInstance) ?: TODO("Should not happen")
4848

49-
channel.send(ShinyProxyEvent(ShinyProxyEventType.UPDATE_DEPENDENCY, shinyProxy, shinyProxyInstance))
49+
channel.send(ShinyProxyEvent(ShinyProxyEventType.RECONCILE, shinyProxy, shinyProxyInstance))
5050
}
5151

5252

src/main/kotlin/eu/openanalytics/shinyproxyoperator/controller/ShinyProxyController.kt

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ class ShinyProxyController(private val kubernetesClient: KubernetesClient,
5555

5656
when (event.eventType) {
5757
ShinyProxyEventType.ADD -> {
58-
reconcileSingleShinyProxy(event.shinyProxy, event.shinyProxyInstance)
58+
val newInstance = createNewInstance(event.shinyProxy)
59+
reconcileSingleShinyProxyInstance(event.shinyProxy, newInstance)
5960
}
60-
ShinyProxyEventType.UPDATE -> {
61-
// TODO calculate hash -> reconcile
62-
reconcileSingleShinyProxy(event.shinyProxy, event.shinyProxyInstance)
61+
ShinyProxyEventType.UPDATE_SPEC -> {
62+
val newInstance = createNewInstance(event.shinyProxy)
63+
reconcileSingleShinyProxyInstance(event.shinyProxy, newInstance)
6364
}
6465
ShinyProxyEventType.DELETE -> {
6566
// DELETE is not needed
66-
// deleteSingleShinyProxy(event.shinyProxy)
6767
}
68-
ShinyProxyEventType.UPDATE_DEPENDENCY -> {
69-
reconcileSingleShinyProxy(event.shinyProxy, event.shinyProxyInstance)
68+
ShinyProxyEventType.RECONCILE -> {
69+
reconcileSingleShinyProxyInstance(event.shinyProxy, event.shinyProxyInstance)
7070
}
7171
}
7272

@@ -90,24 +90,37 @@ class ShinyProxyController(private val kubernetesClient: KubernetesClient,
9090
// }
9191
// }
9292

93-
private suspend fun reconcileSingleShinyProxy(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance?) {
94-
logger.info { "ReconcileSingleShinyProxy: ${shinyProxy.metadata.name}" }
93+
private fun createNewInstance(shinyProxy: ShinyProxy): ShinyProxyInstance {
94+
val existingInstance = shinyProxy.status.getInstanceByHash(shinyProxy.calculateHashOfCurrentSpec())
9595

96-
if (shinyProxy.status.instances.isEmpty()) {
97-
val instance = ShinyProxyInstance()
98-
instance.hashOfSpec = shinyProxy.calculateHashOfCurrentSpec()
99-
instance.isLatestInstance = true
100-
shinyProxy.status.instances.add(instance)
96+
if (existingInstance != null && existingInstance.isLatestInstance == true) {
97+
logger.warn { "Trying to create new instance which already exists and is the latest instance" }
98+
return existingInstance
99+
} else if (existingInstance != null && existingInstance.isLatestInstance == false) {
100+
shinyProxy.status.instances.forEach { it.isLatestInstance = false }
101+
existingInstance.isLatestInstance = true
101102
shinyProxyClient.updateStatus(shinyProxy)
102-
return
103+
return existingInstance
103104
}
104105

106+
val newInstance = ShinyProxyInstance()
107+
newInstance.hashOfSpec = shinyProxy.calculateHashOfCurrentSpec()
108+
newInstance.isLatestInstance = true
109+
shinyProxy.status.instances.forEach { it.isLatestInstance = false }
110+
shinyProxy.status.instances.add(newInstance)
111+
shinyProxyClient.updateStatus(shinyProxy)
112+
return newInstance
113+
}
114+
115+
private suspend fun reconcileSingleShinyProxyInstance(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance?) {
116+
logger.info { "ReconcileSingleShinyProxy: ${shinyProxy.metadata.name}" }
117+
105118
if (shinyProxyInstance == null) {
106-
TODO("Should not happend")
119+
TODO("Should not happen")
107120
}
108121

109122
if (shinyProxyInstance.hashOfSpec == null) {
110-
TODO("Should not happend")
123+
TODO("Should not happen")
111124
}
112125

113126
val configMaps = resourceRetriever.getConfigMapByLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))

src/main/kotlin/eu/openanalytics/shinyproxyoperator/controller/ShinyProxyEventType.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package eu.openanalytics.shinyproxyoperator.controller
22

33
enum class ShinyProxyEventType {
44
ADD,
5-
UPDATE,
5+
UPDATE_SPEC,
66
DELETE,
7-
UPDATE_DEPENDENCY
7+
RECONCILE
88
}

src/main/kotlin/eu/openanalytics/shinyproxyoperator/controller/ShinyProxyListener.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ class ShinyProxyListener(private val channel: SendChannel<ShinyProxyEvent>,
2727
println("Old hash: ${shinyProxy.calculateHashOfCurrentSpec()}")
2828
println("New hash: ${newShinyProxy.calculateHashOfCurrentSpec()}")
2929

30-
val shinyProxyInstance = if (shinyProxy.calculateHashOfCurrentSpec() == newShinyProxy.calculateHashOfCurrentSpec()) {
31-
shinyProxy.status.getInstanceByHash(shinyProxy.calculateHashOfCurrentSpec())
30+
if (shinyProxy.calculateHashOfCurrentSpec() == newShinyProxy.calculateHashOfCurrentSpec()) {
31+
val shinyProxyInstance = shinyProxy.status.getInstanceByHash(shinyProxy.calculateHashOfCurrentSpec()) ?: TODO("This should not happen")
32+
runBlocking { channel.send(ShinyProxyEvent(ShinyProxyEventType.RECONCILE, shinyProxy, shinyProxyInstance)) }
3233
} else {
33-
TODO("New SP instance should be created")
34+
runBlocking { channel.send(ShinyProxyEvent(ShinyProxyEventType.UPDATE_SPEC, newShinyProxy, null)) }
3435
}
35-
36-
if (shinyProxyInstance == null) {
37-
TODO("This should not happen")
38-
}
39-
40-
runBlocking { channel.send(ShinyProxyEvent(ShinyProxyEventType.UPDATE, shinyProxy, shinyProxyInstance)) }
4136
}
4237

4338
override fun onDelete(shinyProxy: ShinyProxy, b: Boolean) {

0 commit comments

Comments
 (0)