Skip to content

Commit 26ee073

Browse files
committed
Fix orphaned resources during operator update
1 parent f567992 commit 26ee073

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/main/kotlin/eu/openanalytics/shinyproxyoperator/components/LabelFactory.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ object LabelFactory {
2727

2828
fun labelsForShinyProxyInstance(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance): Map<String, String> {
2929
val hashOfSpec = shinyProxyInstance.hashOfSpec
30-
return mapOf(
30+
val labels = hashMapOf(
3131
APP_LABEL to APP_LABEL_VALUE,
3232
REALM_ID_LABEL to shinyProxy.realmId,
3333
INSTANCE_LABEL to hashOfSpec,
34-
REVISION_LABEL to shinyProxyInstance.revision.toString()
3534
)
35+
if (shinyProxyInstance.revision != null) {
36+
// only match on revision label, if a revision is set, ensure backwards compatibility
37+
labels[REVISION_LABEL] = shinyProxyInstance.revision.toString()
38+
}
39+
return labels
3640
}
3741

3842
fun labelsForShinyProxy(shinyProxy: ShinyProxy): Map<String, String> {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
135135
val revision = if (existingInstance != null) {
136136
logger.info { "${shinyProxy.logPrefix(existingInstance)} Trying to create new instance which already exists and is not the latest instance. Therefore this instance will become the latest again" }
137137
// reconcile will take care of making this the latest instance again
138-
existingInstance.revision + 1
138+
(existingInstance.revision ?: 0) + 1
139139
} else {
140140
0
141141
}
@@ -286,7 +286,7 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
286286
val instancesToCheck = shinyProxy.status.instances.toList()
287287
for (shinyProxyInstance in instancesToCheck) {
288288
val latestRevision = shinyProxy.status.getInstanceByHash(shinyProxyInstance.hashOfSpec)?.revision ?: 0
289-
if (shinyProxyInstance.isLatestInstance || (shinyProxyInstance.hashOfSpec == shinyProxy.hashOfCurrentSpec && shinyProxyInstance.revision >= latestRevision)) {
289+
if (shinyProxyInstance.isLatestInstance || (shinyProxyInstance.hashOfSpec == shinyProxy.hashOfCurrentSpec && shinyProxyInstance.revision != null && shinyProxyInstance.revision >= latestRevision)) {
290290
// shinyProxyInstance is either the latest or the soon to be latest instance
291291
continue
292292
}
@@ -321,9 +321,6 @@ class ShinyProxyController(private val channel: Channel<ShinyProxyEvent>,
321321
delay(30_000)
322322
logger.info { "${shinyProxy.logPrefix(shinyProxyInstance)} DeleteSingleShinyProxyInstance [Step 3/3]: Delete resources" }
323323

324-
for (service in resourceRetriever.getServiceByLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance), shinyProxy.metadata.namespace)) {
325-
kubernetesClient.resource(service).delete()
326-
}
327324
for (replicaSet in resourceRetriever.getReplicaSetByLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance), shinyProxy.metadata.namespace)) {
328325
kubernetesClient.resource(replicaSet).delete()
329326
}

src/main/kotlin/eu/openanalytics/shinyproxyoperator/crd/ShinyProxyInstance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
*/
2121
package eu.openanalytics.shinyproxyoperator.crd
2222

23-
data class ShinyProxyInstance(val hashOfSpec: String, var isLatestInstance: Boolean, val revision: Int = 0)
23+
data class ShinyProxyInstance(val hashOfSpec: String, var isLatestInstance: Boolean, val revision: Int? = null)

src/main/kotlin/eu/openanalytics/shinyproxyoperator/crd/ShinyProxyStatus.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import io.fabric8.kubernetes.api.model.KubernetesResource
2929
data class ShinyProxyStatus(val instances: ArrayList<ShinyProxyInstance> = arrayListOf()) : KubernetesResource {
3030

3131
fun getInstanceByHash(hash: String): ShinyProxyInstance? {
32-
return instances.filter { it.hashOfSpec == hash }.maxByOrNull { it.revision }
32+
return instances.filter { it.hashOfSpec == hash }.maxByOrNull { it.revision ?: 0 }
3333
}
3434

3535
fun latestInstance(): ShinyProxyInstance? {

0 commit comments

Comments
 (0)