Skip to content

Commit 5785dbd

Browse files
committed
Refactor
1 parent c66f464 commit 5785dbd

15 files changed

Lines changed: 310 additions & 358 deletions

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

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package eu.openanalytics.shinyproxyoperator.components
2+
3+
import eu.openanalytics.shinyproxyoperator.controller.ShinyProxyController
4+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
5+
import io.fabric8.kubernetes.api.model.ConfigMap
6+
import io.fabric8.kubernetes.api.model.ConfigMapBuilder
7+
import io.fabric8.kubernetes.client.KubernetesClient
8+
import mu.KotlinLogging
9+
10+
class ConfigMapFactory(private val kubeClient: KubernetesClient) {
11+
12+
private val logger = KotlinLogging.logger {}
13+
14+
fun create(shinyProxy: ShinyProxy): ConfigMap {
15+
val configMapDefinition: ConfigMap = ConfigMapBuilder()
16+
.withNewMetadata()
17+
.withGenerateName(shinyProxy.metadata.name.toString() + "-configmap-")
18+
.withLabels(mapOf(ShinyProxyController.APP_LABEL to shinyProxy.metadata.name))
19+
.addNewOwnerReference()
20+
.withController(true)
21+
.withKind("ShinyProxy")
22+
.withApiVersion("openanalytics.eu/v1alpha1")
23+
.withName(shinyProxy.metadata.name)
24+
.withNewUid(shinyProxy.metadata.uid)
25+
.endOwnerReference()
26+
.endMetadata()
27+
.addToData("application-in.yml", shinyProxy.spec.applicationYaml)
28+
.build()
29+
30+
val createdConfigMap = kubeClient.configMaps().inNamespace(shinyProxy.metadata.namespace).create(configMapDefinition)
31+
logger.debug { "Created ConfigMap with name ${createdConfigMap.metadata.name}" }
32+
return kubeClient.resource(createdConfigMap).fromServer().get()
33+
}
34+
35+
}

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

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package eu.openanalytics.shinyproxyoperator.components
2+
3+
import eu.openanalytics.shinyproxyoperator.controller.ShinyProxyController
4+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
5+
import eu.openanalytics.shinyproxyoperator.retry
6+
import io.fabric8.kubernetes.api.model.ConfigMap
7+
import io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder
8+
import io.fabric8.kubernetes.api.model.VolumeBuilder
9+
import io.fabric8.kubernetes.api.model.VolumeMountBuilder
10+
import io.fabric8.kubernetes.api.model.apps.ReplicaSet
11+
import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder
12+
import io.fabric8.kubernetes.client.KubernetesClient
13+
import io.fabric8.kubernetes.client.internal.readiness.Readiness
14+
import mu.KotlinLogging
15+
import java.util.*
16+
17+
class ReplicaSetFactory(private val kubeClient: KubernetesClient ) {
18+
19+
private val logger = KotlinLogging.logger {}
20+
21+
suspend fun create(shinyProxy: ShinyProxy,configMap: ConfigMap): ReplicaSet {
22+
val replicaSetDefinition: ReplicaSet = ReplicaSetBuilder()
23+
.withNewMetadata()
24+
.withGenerateName(shinyProxy.metadata.name.toString() + "-replicaset-")
25+
.withNamespace(shinyProxy.metadata.namespace)
26+
.withLabels(mapOf(ShinyProxyController.APP_LABEL to shinyProxy.metadata.name))
27+
.addNewOwnerReference()
28+
.withController(true)
29+
.withKind("ShinyProxy")
30+
.withApiVersion("openanalytics.eu/v1alpha1")
31+
.withName(shinyProxy.metadata.name)
32+
.withNewUid(shinyProxy.metadata.uid)
33+
.endOwnerReference()
34+
.endMetadata()
35+
.withNewSpec()
36+
.withReplicas(1)
37+
.withNewSelector()
38+
.withMatchLabels(mapOf(ShinyProxyController.APP_LABEL to shinyProxy.metadata.name))
39+
.endSelector()
40+
.withNewTemplate()
41+
.withNewMetadata()
42+
.withGenerateName(shinyProxy.metadata.name.toString() + "-pod-")
43+
.withNamespace(shinyProxy.metadata.namespace)
44+
.withLabels(Collections.singletonMap(ShinyProxyController.APP_LABEL, shinyProxy.metadata.name))
45+
.endMetadata()
46+
.withNewSpec()
47+
.addNewContainer()
48+
.withName("shinyproxy")
49+
.withImage("localhost:5000/shinyproxy-dev:latest")
50+
.withVolumeMounts(VolumeMountBuilder()
51+
.withName("config-volume")
52+
.withMountPath("/etc/shinyproxy/application-in.yml")
53+
.withSubPath("application-in.yml")
54+
.build())
55+
.endContainer()
56+
.withVolumes(VolumeBuilder()
57+
.withName("config-volume")
58+
.withConfigMap(ConfigMapVolumeSourceBuilder()
59+
.withName(configMap.metadata.name)
60+
.build())
61+
.build())
62+
.endSpec()
63+
.endTemplate()
64+
.endSpec()
65+
.build()
66+
67+
val createdReplicaSet = kubeClient.apps().replicaSets().inNamespace(shinyProxy.metadata.namespace).create(replicaSetDefinition)
68+
if (retry(60, 1000) { Readiness.isReady(kubeClient.resource(createdReplicaSet).fromServer().get()) }) {
69+
logger.debug { "Created ReplicaSet with name ${createdReplicaSet.metadata.name}" }
70+
return kubeClient.resource(createdReplicaSet).fromServer().get()
71+
} else {
72+
throw RuntimeException("Could not create ReplicaSet ${createdReplicaSet.metadata.name}")
73+
}
74+
}
75+
76+
}

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

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package eu.openanalytics.shinyproxyoperator.components
2+
3+
import eu.openanalytics.shinyproxyoperator.controller.ShinyProxyController
4+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
5+
import io.fabric8.kubernetes.api.model.IntOrString
6+
import io.fabric8.kubernetes.api.model.Service
7+
import io.fabric8.kubernetes.api.model.ServiceBuilder
8+
import io.fabric8.kubernetes.client.KubernetesClient
9+
import mu.KotlinLogging
10+
11+
class ServiceFactory(private val kubeClient: KubernetesClient) {
12+
13+
private val logger = KotlinLogging.logger {}
14+
15+
suspend fun create(shinyProxy: ShinyProxy): Service? {
16+
val serviceDefinition: Service = ServiceBuilder()
17+
.withNewMetadata()
18+
.withGenerateName(shinyProxy.metadata.name.toString() + "-service-")
19+
.withNamespace(shinyProxy.metadata.namespace)
20+
.withLabels(mapOf(ShinyProxyController.APP_LABEL to shinyProxy.metadata.name))
21+
.addNewOwnerReference()
22+
.withController(true)
23+
.withKind("ShinyProxy")
24+
.withApiVersion("openanalytics.eu/v1alpha1")
25+
.withName(shinyProxy.metadata.name)
26+
.withNewUid(shinyProxy.metadata.uid)
27+
.endOwnerReference()
28+
.endMetadata()
29+
.withNewSpec()
30+
.withType("NodePort")
31+
.addNewPort()
32+
.withPort(80)
33+
.withTargetPort(IntOrString(8080))
34+
.endPort()
35+
.withSelector(mapOf(ShinyProxyController.APP_LABEL to shinyProxy.metadata.name))
36+
.endSpec()
37+
.build()
38+
39+
val createdService = kubeClient.services().inNamespace(shinyProxy.metadata.namespace).create(serviceDefinition)
40+
logger.debug { "Created Service with name ${createdService.metadata.name}" }
41+
42+
return kubeClient.resource(createdService).fromServer().get()
43+
}
44+
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package eu.openanalytics.shinyproxyoperator.controller
2+
3+
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy
4+
import io.fabric8.kubernetes.api.model.HasMetadata
5+
import io.fabric8.kubernetes.api.model.OwnerReference
6+
import io.fabric8.kubernetes.client.informers.ResourceEventHandler
7+
import io.fabric8.kubernetes.client.informers.SharedIndexInformer
8+
import io.fabric8.kubernetes.client.informers.cache.Cache
9+
import io.fabric8.kubernetes.client.informers.cache.Lister
10+
import kotlinx.coroutines.channels.SendChannel
11+
import kotlinx.coroutines.runBlocking
12+
import mu.KotlinLogging
13+
14+
class ResourceListener<T : HasMetadata>(private val channel: SendChannel<String>,
15+
informer: SharedIndexInformer<T>,
16+
private val shinyProxyLister: Lister<ShinyProxy>) {
17+
18+
private val logger = KotlinLogging.logger {}
19+
20+
init {
21+
informer.addEventHandler(object : ResourceEventHandler<T> {
22+
override fun onAdd(resource: T) {
23+
logger.debug { "${resource.kind}::OnAdd ${resource.metadata.name}" }
24+
runBlocking { enqueuResource(resource) }
25+
}
26+
27+
override fun onUpdate(resource: T, newResource: T) {
28+
logger.debug { "${resource.kind}::OnUpdate ${resource.metadata.name}" }
29+
runBlocking { enqueuResource(resource) }
30+
}
31+
32+
override fun onDelete(resource: T, b: Boolean) {
33+
logger.debug { "${resource.kind}::OnDelete ${resource.metadata.name}" }
34+
runBlocking { enqueuResource(resource) }
35+
}
36+
})
37+
}
38+
39+
private suspend fun enqueuResource(resource: T) {
40+
val shinyProxy = if (resource is ShinyProxy) {
41+
resource
42+
} else {
43+
val ownerReference = getControllerOf(resource) ?: return
44+
if (ownerReference.kind.toLowerCase() != "shinyproxy") {
45+
return
46+
}
47+
shinyProxyLister[ownerReference.name] ?: return
48+
}
49+
val key = Cache.metaNamespaceKeyFunc(shinyProxy)
50+
if (key != null && key.isNotEmpty()) {
51+
channel.send(key)
52+
}
53+
}
54+
55+
56+
private fun getControllerOf(resource: HasMetadata): OwnerReference? {
57+
val ownerReferences = resource.metadata.ownerReferences
58+
for (ownerReference in ownerReferences) {
59+
if (ownerReference.controller) {
60+
return ownerReference
61+
}
62+
}
63+
return null
64+
}
65+
66+
}

0 commit comments

Comments
 (0)