Skip to content

Commit 5ca0c24

Browse files
committed
Check for obsolete SP instances and remove them
1 parent 059601c commit 5ca0c24

File tree

12 files changed

+161
-78
lines changed

12 files changed

+161
-78
lines changed

pom.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<maven.compiler.source>11</maven.compiler.source>
1313
<maven.compiler.release>11</maven.compiler.release>
1414
<maven.compiler.target>11</maven.compiler.target>
15-
<version.fabric8.client>4.10.3</version.fabric8.client>
15+
<version.fabric8.client>4.2.2</version.fabric8.client>
1616
<kotlin.version>1.4.0</kotlin.version>
1717
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
1818
</properties>
@@ -52,6 +52,31 @@
5252
<artifactId>kotlin-logging</artifactId>
5353
<version>1.7.8</version>
5454
</dependency>
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-core</artifactId>
58+
<version>2.11.2</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.fasterxml.jackson.core</groupId>
62+
<artifactId>jackson-annotations</artifactId>
63+
<version>2.11.2</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.fasterxml.jackson.core</groupId>
67+
<artifactId>jackson-databind</artifactId>
68+
<version>2.11.2</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.fasterxml.jackson.dataformat</groupId>
72+
<artifactId>jackson-dataformat-yaml</artifactId>
73+
<version>2.11.2</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.fasterxml.jackson.module</groupId>
77+
<artifactId>jackson-module-jaxb-annotations</artifactId>
78+
<version>2.11.2</version>
79+
</dependency>
5580
</dependencies>
5681

5782
<build>

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package eu.openanalytics.shinyproxyoperator.components
22

3-
import com.fasterxml.jackson.databind.ObjectMapper
4-
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
5-
import eu.openanalytics.shinyproxyoperator.controller.ShinyProxyController
63
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
4+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance
75
import io.fabric8.kubernetes.api.model.ConfigMap
86
import io.fabric8.kubernetes.api.model.ConfigMapBuilder
97
import io.fabric8.kubernetes.client.KubernetesClient
@@ -14,11 +12,15 @@ class ConfigMapFactory(private val kubeClient: KubernetesClient) {
1412

1513
private val logger = KotlinLogging.logger {}
1614

17-
fun create(shinyProxy: ShinyProxy): ConfigMap {
15+
fun create(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance): ConfigMap {
16+
if (shinyProxy.calculateHashOfCurrentSpec() != shinyProxyInstance.hashOfSpec) {
17+
TODO("Cannot re-create ConfigMap for old instance")
18+
}
19+
1820
val configMapDefinition: ConfigMap = ConfigMapBuilder()
1921
.withNewMetadata()
2022
.withName(ResourceNameFactory.createNameForConfigMap(shinyProxy))
21-
.withLabels(LabelFactory.labelsForCurrentShinyProxyInstance(shinyProxy))
23+
.withLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))
2224
.addNewOwnerReference()
2325
.withController(true)
2426
.withKind("ShinyProxy")
@@ -32,6 +34,7 @@ class ConfigMapFactory(private val kubeClient: KubernetesClient) {
3234

3335
val createdConfigMap = kubeClient.configMaps().inNamespace(shinyProxy.metadata.namespace).create(configMapDefinition)
3436
logger.debug { "Created ConfigMap with name ${createdConfigMap.metadata.name}" }
37+
3538
return kubeClient.resource(createdConfigMap).fromServer().get()
3639
}
3740

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ object LabelFactory {
2727

2828
const val APP_LABEL = "app"
2929
const val APP_LABEL_VALUE = "shinyproxy"
30-
const val NAME_LABEL = "sp-name"
31-
const val INSTANCE_LABEL = "sp-instance"
30+
const val NAME_LABEL = "openanalytics.eu/sp-resource-name"
31+
const val INSTANCE_LABEL = "openanalytics.eu/sp-instance"
32+
const val PROXIED_APP = "openanalytics.eu/containerproxy-proxied-app"
3233

3334
}

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

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

33
import eu.openanalytics.shinyproxyoperator.controller.ShinyProxyController
44
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
5+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance
56
import eu.openanalytics.shinyproxyoperator.retry
67
import io.fabric8.kubernetes.api.model.ConfigMap
78
import io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder
@@ -18,12 +19,12 @@ class ReplicaSetFactory(private val kubeClient: KubernetesClient ) {
1819

1920
private val logger = KotlinLogging.logger {}
2021

21-
suspend fun create(shinyProxy: ShinyProxy): ReplicaSet {
22+
suspend fun create(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance): ReplicaSet {
2223
val replicaSetDefinition: ReplicaSet = ReplicaSetBuilder()
2324
.withNewMetadata()
2425
.withName(ResourceNameFactory.createNameForReplicaSet(shinyProxy))
2526
.withNamespace(shinyProxy.metadata.namespace)
26-
.withLabels(LabelFactory.labelsForCurrentShinyProxyInstance(shinyProxy))
27+
.withLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))
2728
.addNewOwnerReference()
2829
.withController(true)
2930
.withKind("ShinyProxy")
@@ -35,13 +36,13 @@ class ReplicaSetFactory(private val kubeClient: KubernetesClient ) {
3536
.withNewSpec()
3637
.withReplicas(1)
3738
.withNewSelector()
38-
.withMatchLabels(LabelFactory.labelsForCurrentShinyProxyInstance(shinyProxy))
39+
.withMatchLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))
3940
.endSelector()
4041
.withNewTemplate()
4142
.withNewMetadata()
4243
.withGenerateName(ResourceNameFactory.createNameForPod(shinyProxy))
4344
.withNamespace(shinyProxy.metadata.namespace)
44-
.withLabels(LabelFactory.labelsForCurrentShinyProxyInstance(shinyProxy))
45+
.withLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))
4546
.endMetadata()
4647
.withNewSpec()
4748
.addNewContainer()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eu.openanalytics.shinyproxyoperator.components
22

33
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
4+
import io.fabric8.kubernetes.client.informers.SharedIndexInformer
45
import mu.KotlinLogging
56

67
object ResourceNameFactory {

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

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

33
import eu.openanalytics.shinyproxyoperator.controller.ShinyProxyController
44
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
5+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance
56
import io.fabric8.kubernetes.api.model.IntOrString
67
import io.fabric8.kubernetes.api.model.Service
78
import io.fabric8.kubernetes.api.model.ServiceBuilder
@@ -12,12 +13,12 @@ class ServiceFactory(private val kubeClient: KubernetesClient) {
1213

1314
private val logger = KotlinLogging.logger {}
1415

15-
suspend fun create(shinyProxy: ShinyProxy): Service? {
16+
suspend fun create(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance): Service? {
1617
val serviceDefinition: Service = ServiceBuilder()
1718
.withNewMetadata()
1819
.withName(ResourceNameFactory.createNameForService(shinyProxy))
1920
.withNamespace(shinyProxy.metadata.namespace)
20-
.withLabels(LabelFactory.labelsForCurrentShinyProxyInstance(shinyProxy))
21+
.withLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))
2122
.addNewOwnerReference()
2223
.withController(true)
2324
.withKind("ShinyProxy")
@@ -32,7 +33,7 @@ class ServiceFactory(private val kubeClient: KubernetesClient) {
3233
.withPort(80)
3334
.withTargetPort(IntOrString(8080))
3435
.endPort()
35-
.withSelector(LabelFactory.labelsForCurrentShinyProxyInstance(shinyProxy))
36+
.withSelector(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance))
3637
.endSpec()
3738
.build()
3839

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,74 @@
11
package eu.openanalytics.shinyproxyoperator.controller
22

33
import io.fabric8.kubernetes.api.model.ConfigMap
4+
import io.fabric8.kubernetes.api.model.Pod
45
import io.fabric8.kubernetes.api.model.Service
56
import io.fabric8.kubernetes.api.model.apps.ReplicaSet
67
import io.fabric8.kubernetes.client.informers.cache.Lister
78
import mu.KotlinLogging
8-
import java.util.AbstractMap
99

1010
class ResourceRetriever(private val replicaSetLister: Lister<ReplicaSet>,
1111
private val configMapLister: Lister<ConfigMap>,
12-
private val serviceLister: Lister<Service>) {
12+
private val serviceLister: Lister<Service>,
13+
private val podLister: Lister<Pod>) {
1314

1415
private val logger = KotlinLogging.logger {}
1516

16-
fun getConfigMapByLabel(label: String, shinyProxyName: String): List<ConfigMap> {
17-
val configMaps = arrayListOf<ConfigMap>()
18-
for (configmap in configMapLister.list()) {
19-
if (configmap?.metadata?.labels?.entries?.contains(AbstractMap.SimpleEntry(label, shinyProxyName)) == true) {
20-
configMaps.add(configmap)
21-
logger.debug { "Found ConfigMap ${configmap.metadata.name}" }
22-
}
23-
}
24-
logger.info { "ConfigMapCount: ${configMaps.size}, ${configMaps.map { it.metadata.name }}" }
25-
return configMaps
26-
}
27-
2817
fun getConfigMapByLabels(labels: Map<String, String>): List<ConfigMap> {
2918
val configMaps = arrayListOf<ConfigMap>()
19+
logger.debug { "Looking for configmap with labels: $labels" }
3020
for (configmap in configMapLister.list()) {
21+
logger.debug { "Found ConfigMap ${configmap.metadata.name}" }
3122
if (configmap?.metadata?.labels?.entries?.containsAll(labels.entries) == true) {
3223
configMaps.add(configmap)
33-
logger.debug { "Found ConfigMap ${configmap.metadata.name}" }
24+
} else {
25+
logger.debug { "Not what we are looking for..." }
3426
}
3527
}
3628
logger.info { "ConfigMapCount: ${configMaps.size}, ${configMaps.map { it.metadata.name }}" }
3729
return configMaps
3830
}
3931

40-
fun getReplicaSetByLabel(label: String, shinyProxyName: String): ArrayList<ReplicaSet> {
41-
val replicaSets = arrayListOf<ReplicaSet>()
42-
for (replicaSet in replicaSetLister.list()) {
43-
if (replicaSet?.metadata?.labels?.entries?.contains(AbstractMap.SimpleEntry(label, shinyProxyName)) == true) {
44-
replicaSets.add(replicaSet)
45-
logger.debug { "Found ReplicaSet ${replicaSet.metadata.name} phase => ${replicaSet.status}" }
46-
}
47-
}
48-
logger.info { "ReplicaSetCount: ${replicaSets.size}, ${replicaSets.map { it.metadata.name }}" }
49-
return replicaSets
50-
}
51-
5232
fun getReplicaSetByLabels(labels: Map<String, String>): ArrayList<ReplicaSet> {
5333
val replicaSets = arrayListOf<ReplicaSet>()
34+
logger.debug { "Looking for Repliacas with labels: $labels" }
5435
for (replicaSet in replicaSetLister.list()) {
5536
if (replicaSet?.metadata?.labels?.entries?.containsAll(labels.entries) == true) {
5637
replicaSets.add(replicaSet)
57-
logger.debug { "Found ReplicaSet ${replicaSet.metadata.name} phase => ${replicaSet.status}" }
38+
} else {
39+
logger.debug { "Not what we are looking for..." }
5840
}
5941
}
6042
logger.info { "ReplicaSetCount: ${replicaSets.size}, ${replicaSets.map { it.metadata.name }}" }
6143
return replicaSets
6244
}
6345

64-
fun getServiceByLabel(label: String, shinyProxyName: String): List<Service> {
46+
fun getServiceByLabels(labels: Map<String, String>): List<Service> {
6547
val services = arrayListOf<Service>()
48+
logger.debug { "Looking for Services with labels: $labels" }
6649
for (service in serviceLister.list()) {
67-
if (service?.metadata?.labels?.entries?.contains(AbstractMap.SimpleEntry(label, shinyProxyName)) == true) {
50+
if (service?.metadata?.labels?.entries?.containsAll(labels.entries) == true) {
6851
services.add(service)
69-
logger.debug { "Found ReplicaSet ${service.metadata.name} phase => ${service.status}" }
52+
} else {
53+
logger.debug { "Not what we are looking for..." }
7054
}
7155
}
7256
logger.info { "ServiceCount: ${services.size}, ${services.map { it.metadata.name }}" }
7357
return services
7458
}
7559

76-
fun getServiceByLabels(labels: Map<String, String>): List<Service> {
77-
val services = arrayListOf<Service>()
78-
for (service in serviceLister.list()) {
60+
fun getPodByLabels(labels: Map<String, String>): List<Pod> {
61+
val pods = arrayListOf<Pod>()
62+
logger.debug { "Looking for Pods with labels: $labels" }
63+
for (service in podLister.list()) {
7964
if (service?.metadata?.labels?.entries?.containsAll(labels.entries) == true) {
80-
services.add(service)
81-
logger.debug { "Found ReplicaSet ${service.metadata.name} phase => ${service.status}" }
65+
pods.add(service)
66+
} else {
67+
logger.debug { "Not what we are looking for..." }
8268
}
8369
}
84-
logger.info { "ServiceCount: ${services.size}, ${services.map { it.metadata.name }}" }
85-
return services
70+
logger.info { "PodCount: ${pods.size}, ${pods.map { it.metadata.name }}" }
71+
return pods
8672
}
8773

8874
}

0 commit comments

Comments
 (0)