|
| 1 | +package eu.glasskube.operator.apps.gitea.dependent |
| 2 | + |
| 3 | +import eu.glasskube.kubernetes.api.model.metadata |
| 4 | +import eu.glasskube.kubernetes.api.model.namespace |
| 5 | +import eu.glasskube.operator.apps.gitea.Gitea |
| 6 | +import eu.glasskube.operator.apps.gitea.GiteaActionRunnerSpecTemplate |
| 7 | +import eu.glasskube.operator.apps.gitea.GiteaReconciler |
| 8 | +import eu.glasskube.operator.apps.gitea.resourceLabels |
| 9 | +import eu.glasskube.operator.apps.gitea.runner.GiteaActionRunner |
| 10 | +import eu.glasskube.operator.apps.gitea.runner.GiteaActionRunnerSpec |
| 11 | +import eu.glasskube.operator.apps.gitea.runner.giteaActionRunner |
| 12 | +import eu.glasskube.operator.apps.gitea.tokenHash |
| 13 | +import io.fabric8.kubernetes.api.model.LocalObjectReference |
| 14 | +import io.javaoperatorsdk.operator.api.reconciler.Context |
| 15 | +import io.javaoperatorsdk.operator.processing.dependent.BulkDependentResource |
| 16 | +import io.javaoperatorsdk.operator.processing.dependent.Matcher |
| 17 | +import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource |
| 18 | +import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent |
| 19 | + |
| 20 | +@KubernetesDependent(labelSelector = GiteaReconciler.SELECTOR) |
| 21 | +class GiteaActionRunners : |
| 22 | + CRUDKubernetesDependentResource<GiteaActionRunner, Gitea>(GiteaActionRunner::class.java), |
| 23 | + BulkDependentResource<GiteaActionRunner, Gitea> { |
| 24 | + |
| 25 | + fun desired(primary: Gitea, template: GiteaActionRunnerSpecTemplate) = giteaActionRunner { |
| 26 | + metadata { |
| 27 | + name = "${primary.metadata.name}-${template.tokenHash}" |
| 28 | + namespace = primary.namespace |
| 29 | + labels = primary.resourceLabels |
| 30 | + } |
| 31 | + spec = GiteaActionRunnerSpec( |
| 32 | + template.token, |
| 33 | + LocalObjectReference(primary.metadata.name) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + override fun desiredResources(primary: Gitea, context: Context<Gitea>) = primary.spec.actions.run { |
| 38 | + if (enabled) { |
| 39 | + runners.map { desired(primary, it) }.associateBy { it.metadata.name } |
| 40 | + } else { |
| 41 | + emptyMap() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + override fun getSecondaryResources(primary: Gitea, context: Context<Gitea>) = |
| 46 | + context.getSecondaryResources(GiteaActionRunner::class.java) |
| 47 | + .filter { it.spec.gitea.name == primary.metadata.name } |
| 48 | + .associateBy { it.metadata.name } |
| 49 | + |
| 50 | + override fun match( |
| 51 | + actualResource: GiteaActionRunner, |
| 52 | + desired: GiteaActionRunner, |
| 53 | + primary: Gitea, |
| 54 | + context: Context<Gitea> |
| 55 | + ): Matcher.Result<GiteaActionRunner> = |
| 56 | + super<CRUDKubernetesDependentResource>.match(actualResource, desired, primary, context) |
| 57 | +} |
0 commit comments