|
| 1 | +package io.javaoperatorsdk.operator.sample.kubernetesdependentgarbagecollection; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 6 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 7 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 8 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
| 9 | +import io.javaoperatorsdk.operator.api.reconciler.*; |
| 10 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected; |
| 11 | +import io.javaoperatorsdk.operator.junit.KubernetesClientAware; |
| 12 | +import io.javaoperatorsdk.operator.processing.dependent.Creator; |
| 13 | +import io.javaoperatorsdk.operator.processing.dependent.Updater; |
| 14 | +import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource; |
| 15 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 16 | + |
| 17 | +@ControllerConfiguration |
| 18 | +public class DependentGarbageCollectionTestReconciler |
| 19 | + implements Reconciler<DependentGarbageCollectionTestCustomResource>, |
| 20 | + EventSourceInitializer<DependentGarbageCollectionTestCustomResource>, |
| 21 | + KubernetesClientAware, ErrorStatusHandler<DependentGarbageCollectionTestCustomResource> { |
| 22 | + |
| 23 | + private KubernetesClient kubernetesClient; |
| 24 | + private volatile boolean errorOccurred = false; |
| 25 | + |
| 26 | + ConfigMapDependentResource configMapDependent; |
| 27 | + |
| 28 | + public DependentGarbageCollectionTestReconciler() { |
| 29 | + configMapDependent = new ConfigMapDependentResource(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public Map<String, EventSource> prepareEventSources( |
| 34 | + EventSourceContext<DependentGarbageCollectionTestCustomResource> context) { |
| 35 | + return EventSourceInitializer |
| 36 | + .nameEventSources(configMapDependent.initEventSource(context)); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public UpdateControl<DependentGarbageCollectionTestCustomResource> reconcile( |
| 41 | + DependentGarbageCollectionTestCustomResource primary, |
| 42 | + Context<DependentGarbageCollectionTestCustomResource> context) { |
| 43 | + |
| 44 | + if (primary.getSpec().isCreateConfigMap()) { |
| 45 | + configMapDependent.reconcile(primary, context); |
| 46 | + } else { |
| 47 | + configMapDependent.delete(primary, context); |
| 48 | + } |
| 49 | + |
| 50 | + return UpdateControl.noUpdate(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void setKubernetesClient(KubernetesClient kubernetesClient) { |
| 55 | + this.kubernetesClient = kubernetesClient; |
| 56 | + configMapDependent.setKubernetesClient(kubernetesClient); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public KubernetesClient getKubernetesClient() { |
| 61 | + return this.kubernetesClient; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public ErrorStatusUpdateControl<DependentGarbageCollectionTestCustomResource> updateErrorStatus( |
| 66 | + DependentGarbageCollectionTestCustomResource resource, |
| 67 | + Context<DependentGarbageCollectionTestCustomResource> context, Exception e) { |
| 68 | + // this can happen when a namespace is terminated in test |
| 69 | + if (e instanceof KubernetesClientException) { |
| 70 | + return ErrorStatusUpdateControl.noStatusUpdate(); |
| 71 | + } |
| 72 | + errorOccurred = true; |
| 73 | + return ErrorStatusUpdateControl.noStatusUpdate(); |
| 74 | + } |
| 75 | + |
| 76 | + public boolean isErrorOccurred() { |
| 77 | + return errorOccurred; |
| 78 | + } |
| 79 | + |
| 80 | + private static class ConfigMapDependentResource extends |
| 81 | + KubernetesDependentResource<ConfigMap, DependentGarbageCollectionTestCustomResource> |
| 82 | + implements Creator<ConfigMap, DependentGarbageCollectionTestCustomResource>, |
| 83 | + Updater<ConfigMap, DependentGarbageCollectionTestCustomResource>, |
| 84 | + GarbageCollected<DependentGarbageCollectionTestCustomResource> { |
| 85 | + |
| 86 | + public ConfigMapDependentResource() { |
| 87 | + super(ConfigMap.class); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected ConfigMap desired(DependentGarbageCollectionTestCustomResource primary, |
| 92 | + Context<DependentGarbageCollectionTestCustomResource> context) { |
| 93 | + ConfigMap configMap = new ConfigMap(); |
| 94 | + configMap.setMetadata(new ObjectMetaBuilder() |
| 95 | + .withName(primary.getMetadata().getName()) |
| 96 | + .withNamespace(primary.getMetadata().getNamespace()) |
| 97 | + .build()); |
| 98 | + configMap.setData(Map.of("key", "data")); |
| 99 | + return configMap; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments