Skip to content

Commit 7122072

Browse files
committed
Properly stop watchers during itest
1 parent b50bc8e commit 7122072

9 files changed

Lines changed: 57 additions & 12 deletions

File tree

src/main/kotlin/eu/openanalytics/shinyproxyoperator/Operator.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ class Operator(client: NamespacedKubernetesClient? = null,
159159
}
160160

161161
Timer().schedule(5000, 5000) {
162-
val num = (getOperatorInstance().client as DefaultKubernetesClient).httpClient.connectionPool().connectionCount()
163-
val max = (getOperatorInstance().client as DefaultKubernetesClient).configuration.maxConcurrentRequests
162+
val num = (client as DefaultKubernetesClient).httpClient.connectionPool().connectionCount()
163+
val max = client.configuration.maxConcurrentRequests
164164
logger.warn { "Current number of connections: $num of $max" }
165165
}
166166
}
@@ -210,6 +210,15 @@ class Operator(client: NamespacedKubernetesClient? = null,
210210
shinyProxyController.run(resourceRetriever, shinyProxyLister)
211211
}
212212

213+
fun stop() {
214+
shinyProxyListener.stop()
215+
replicaSetListener.stop()
216+
serviceListener.stop()
217+
configMapListener.stop()
218+
ingressController.stop()
219+
podRetriever.stop()
220+
}
221+
213222
companion object {
214223
private var _operatorInstance: Operator? = null
215224

@@ -239,6 +248,7 @@ class Operator(client: NamespacedKubernetesClient? = null,
239248
return res
240249
}
241250

251+
242252
}
243253

244254
enum class Mode {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ import eu.openanalytics.shinyproxyoperator.components.LabelFactory
2424
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
2525
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance
2626
import io.fabric8.kubernetes.api.model.Pod
27-
import io.fabric8.kubernetes.api.model.PodList
2827
import io.fabric8.kubernetes.client.NamespacedKubernetesClient
29-
import io.fabric8.kubernetes.client.dsl.base.OperationContext
3028
import io.fabric8.kubernetes.client.informers.SharedIndexInformer
3129
import mu.KotlinLogging
3230

@@ -89,4 +87,8 @@ class PodRetriever(private val client: NamespacedKubernetesClient) {
8987
return informers.keys
9088
}
9189

90+
fun stop() {
91+
informers.forEach { it.value.stop() }
92+
}
93+
9294
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import io.fabric8.kubernetes.api.model.OwnerReference
2929
import io.fabric8.kubernetes.client.dsl.MixedOperation
3030
import io.fabric8.kubernetes.client.dsl.Resource
3131
import io.fabric8.kubernetes.client.informers.ResourceEventHandler
32+
import io.fabric8.kubernetes.client.informers.SharedIndexInformer
3233
import io.fabric8.kubernetes.client.informers.cache.Indexer
3334
import io.fabric8.kubernetes.client.informers.cache.Lister
3435
import kotlinx.coroutines.channels.SendChannel
@@ -42,8 +43,10 @@ class ResourceListener<T : HasMetadata, L : KubernetesResourceList<T>, R : Resou
4243

4344
private val logger = KotlinLogging.logger {}
4445

46+
private var informer: SharedIndexInformer<T>? = null
47+
4548
fun start(shinyProxyLister: Lister<ShinyProxy>): Indexer<T>? {
46-
val informer = resourceClient.inform(object : ResourceEventHandler<T> {
49+
val i = resourceClient.inform(object : ResourceEventHandler<T> {
4750
override fun onAdd(resource: T) {
4851
logger.trace { "${resource.kind}::OnAdd ${resource.metadata.name}" }
4952
runBlocking { enqueueResource(shinyProxyLister, "Add", resource) }
@@ -59,7 +62,8 @@ class ResourceListener<T : HasMetadata, L : KubernetesResourceList<T>, R : Resou
5962
runBlocking { enqueueResource(shinyProxyLister, "Delete", resource) }
6063
}
6164
})
62-
return informer.indexer
65+
informer = i
66+
return i.indexer
6367
}
6468

6569
private suspend fun enqueueResource(shinyProxyLister: Lister<ShinyProxy>, trigger: String, resource: T) {
@@ -95,4 +99,9 @@ class ResourceListener<T : HasMetadata, L : KubernetesResourceList<T>, R : Resou
9599
return null
96100
}
97101

102+
fun stop() {
103+
informer?.stop()
104+
informer = null
105+
}
106+
98107
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import eu.openanalytics.shinyproxyoperator.ShinyProxyClient
2424
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
2525
import eu.openanalytics.shinyproxyoperator.isInManagedNamespace
2626
import io.fabric8.kubernetes.client.informers.ResourceEventHandler
27+
import io.fabric8.kubernetes.client.informers.SharedIndexInformer
2728
import io.fabric8.kubernetes.client.informers.cache.Indexer
2829
import kotlinx.coroutines.channels.SendChannel
2930
import kotlinx.coroutines.runBlocking
@@ -33,8 +34,10 @@ class ShinyProxyListener(private val channel: SendChannel<ShinyProxyEvent>, priv
3334

3435
private val logger = KotlinLogging.logger {}
3536

37+
private var informer: SharedIndexInformer<ShinyProxy>? = null
38+
3639
fun start(): Indexer<ShinyProxy> {
37-
val informer = shinyProxyClient.inform(object : ResourceEventHandler<ShinyProxy> {
40+
val i = shinyProxyClient.inform(object : ResourceEventHandler<ShinyProxy> {
3841
override fun onAdd(shinyProxy: ShinyProxy) {
3942
if (!isInManagedNamespace(shinyProxy)) return
4043
logger.debug { "${shinyProxy.logPrefix()} [Event/Add]" }
@@ -69,7 +72,13 @@ class ShinyProxyListener(private val channel: SendChannel<ShinyProxyEvent>, priv
6972
runBlocking { channel.send(ShinyProxyEvent(ShinyProxyEventType.DELETE, shinyProxy, null)) }
7073
}
7174
}, 10 * 60 * 1000.toLong())
72-
return informer.indexer!!
75+
informer = i
76+
return i.indexer!!
77+
}
78+
79+
fun stop() {
80+
informer?.stop()
81+
informer = null
7382
}
7483

7584

src/main/kotlin/eu/openanalytics/shinyproxyoperator/ingres/IIngressController.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ interface IIngressController {
2929
fun reconcile(resourceRetriever: ResourceRetriever, shinyProxy: ShinyProxy)
3030

3131
fun onRemoveInstance(resourceRetriever: ResourceRetriever, shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance)
32+
33+
fun stop()
3234
}

src/main/kotlin/eu/openanalytics/shinyproxyoperator/ingress/skipper/IngressController.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class IngressController(
7777
}
7878
}
7979

80+
override fun stop() {
81+
ingressListener.stop()
82+
}
83+
8084
private fun reconcileSingleInstance(resourceRetriever: ResourceRetriever, shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance) {
8185
val ingresses = resourceRetriever.getIngressByLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance), shinyProxy.metadata.namespace)
8286

src/main/kotlin/eu/openanalytics/shinyproxyoperator/ingress/skipper/IngressListener.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ class IngressListener(private val channel: SendChannel<ShinyProxyEvent>,
4646
private val kubernetesClient: KubernetesClient,
4747
private val ingressClient: MixedOperation<Ingress, IngressList, Resource<Ingress>>) {
4848

49+
private var informer: SharedIndexInformer<Ingress>? = null
4950
private val logger = KotlinLogging.logger {}
5051

5152
fun start(shinyProxyLister: Lister<ShinyProxy>): Indexer<Ingress> {
52-
val informer = ingressClient.inform(object : ResourceEventHandler<Ingress> {
53+
val i = ingressClient.inform(object : ResourceEventHandler<Ingress> {
5354
override fun onAdd(resource: Ingress) {
5455
logger.trace { "${resource.kind}::OnAdd ${resource.metadata.name}" }
5556
runBlocking { enqueueResource(shinyProxyLister, "Add", resource) }
@@ -65,7 +66,13 @@ class IngressListener(private val channel: SendChannel<ShinyProxyEvent>,
6566
runBlocking { enqueueResource(shinyProxyLister, "Delete", resource) }
6667
}
6768
})
68-
return informer.indexer
69+
informer = i
70+
return i.indexer
71+
}
72+
73+
fun stop() {
74+
informer?.stop()
75+
informer = null
6976
}
7077

7178
private suspend fun enqueueResource(shinyProxyLister: Lister<ShinyProxy>, trigger: String, resource: Ingress) {
@@ -96,7 +103,6 @@ class IngressListener(private val channel: SendChannel<ShinyProxyEvent>,
96103
channel.send(ShinyProxyEvent(ShinyProxyEventType.RECONCILE, shinyProxy, shinyProxyInstance))
97104
}
98105

99-
100106
private fun getShinyProxyOwnerRefByKind(resource: HasMetadata, kind: String): OwnerReference? {
101107
val ownerReferences = resource.metadata.ownerReferences
102108
for (ownerReference in ownerReferences) {
@@ -109,4 +115,5 @@ class IngressListener(private val channel: SendChannel<ShinyProxyEvent>,
109115
}
110116

111117

118+
112119
}

src/test/kotlin/eu/openanalytics/shinyproxyoperator/MainIntegrationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class MainIntegrationTest : IntegrationTestBase() {
160160

161161
// 3. wait until instance is created
162162
spTestInstance.waitForOneReconcile()
163-
logger.info { "Fuly created instance." }
163+
logger.info { "Fully created instance." }
164164

165165
// 4. assert correctness
166166
spTestInstance.assertInstanceIsCorrect()

src/test/kotlin/eu/openanalytics/shinyproxyoperator/helpers/IntegrationTestBase.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ abstract class IntegrationTestBase {
114114
// 6. delete namespace
115115
deleteNamespaces()
116116
}
117+
Operator.getOperatorInstance().stop()
118+
stableClient.httpClient.connectionPool().evictAll()
117119
}
118120

119121
}

0 commit comments

Comments
 (0)