|
| 1 | +package io.javaoperatorsdk.operator.sample.indexdiscriminator; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; |
| 7 | + |
| 8 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 9 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 10 | +import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration; |
| 11 | +import io.javaoperatorsdk.operator.api.reconciler.*; |
| 12 | +import io.javaoperatorsdk.operator.junit.KubernetesClientAware; |
| 13 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 14 | +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; |
| 15 | +import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; |
| 16 | + |
| 17 | +@ControllerConfiguration |
| 18 | +public class IndexDiscriminatorTestReconciler |
| 19 | + implements Reconciler<IndexDiscriminatorTestCustomResource>, |
| 20 | + Cleaner<IndexDiscriminatorTestCustomResource>, |
| 21 | + TestExecutionInfoProvider, EventSourceInitializer<IndexDiscriminatorTestCustomResource>, |
| 22 | + KubernetesClientAware { |
| 23 | + |
| 24 | + public static final String FIRST_CONFIG_MAP_SUFFIX_1 = "-1"; |
| 25 | + public static final String FIRST_CONFIG_MAP_SUFFIX_2 = "-2"; |
| 26 | + public static final String CONFIG_MAP_INDEX_1 = "CONFIG_MAP_INDEX1"; |
| 27 | + public static final String CONFIG_MAP_INDEX_2 = "CONFIG_MAP_INDEX2"; |
| 28 | + |
| 29 | + private final AtomicInteger numberOfExecutions = new AtomicInteger(0); |
| 30 | + |
| 31 | + private final IndexDiscriminatorTestDRConfigMap firstDependentResourceConfigMap; |
| 32 | + private final IndexDiscriminatorTestDRConfigMap secondDependentResourceConfigMap; |
| 33 | + private KubernetesClient client; |
| 34 | + |
| 35 | + public IndexDiscriminatorTestReconciler() { |
| 36 | + firstDependentResourceConfigMap = |
| 37 | + new IndexDiscriminatorTestDRConfigMap(FIRST_CONFIG_MAP_SUFFIX_1); |
| 38 | + secondDependentResourceConfigMap = |
| 39 | + new IndexDiscriminatorTestDRConfigMap(FIRST_CONFIG_MAP_SUFFIX_2); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public UpdateControl<IndexDiscriminatorTestCustomResource> reconcile( |
| 44 | + IndexDiscriminatorTestCustomResource resource, |
| 45 | + Context<IndexDiscriminatorTestCustomResource> context) { |
| 46 | + numberOfExecutions.getAndIncrement(); |
| 47 | + firstDependentResourceConfigMap.reconcile(resource, context); |
| 48 | + secondDependentResourceConfigMap.reconcile(resource, context); |
| 49 | + return UpdateControl.noUpdate(); |
| 50 | + } |
| 51 | + |
| 52 | + public int getNumberOfExecutions() { |
| 53 | + return numberOfExecutions.get(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Map<String, EventSource> prepareEventSources( |
| 58 | + EventSourceContext<IndexDiscriminatorTestCustomResource> context) { |
| 59 | + |
| 60 | + InformerEventSource<ConfigMap, IndexDiscriminatorTestCustomResource> eventSource = |
| 61 | + new InformerEventSource<>(InformerConfiguration.from(ConfigMap.class, context) |
| 62 | + .build(), context); |
| 63 | + |
| 64 | + eventSource.addIndexer(CONFIG_MAP_INDEX_1, cm -> { |
| 65 | + if (cm.getMetadata().getName().endsWith(FIRST_CONFIG_MAP_SUFFIX_1)) { |
| 66 | + return List.of(configMapKey(cm)); |
| 67 | + } else { |
| 68 | + return Collections.emptyList(); |
| 69 | + } |
| 70 | + }); |
| 71 | + eventSource.addIndexer(CONFIG_MAP_INDEX_2, cm -> { |
| 72 | + if (cm.getMetadata().getName().endsWith(FIRST_CONFIG_MAP_SUFFIX_2)) { |
| 73 | + return List.of(configMapKey(cm)); |
| 74 | + } else { |
| 75 | + return Collections.emptyList(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + firstDependentResourceConfigMap.configureWith(eventSource); |
| 80 | + secondDependentResourceConfigMap.configureWith(eventSource); |
| 81 | + |
| 82 | + firstDependentResourceConfigMap |
| 83 | + .setResourceDiscriminator( |
| 84 | + new IndexDiscriminator(CONFIG_MAP_INDEX_1, FIRST_CONFIG_MAP_SUFFIX_1)); |
| 85 | + secondDependentResourceConfigMap |
| 86 | + .setResourceDiscriminator( |
| 87 | + new IndexDiscriminator(CONFIG_MAP_INDEX_2, FIRST_CONFIG_MAP_SUFFIX_2)); |
| 88 | + return EventSourceInitializer.nameEventSources(eventSource); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public KubernetesClient getKubernetesClient() { |
| 93 | + return client; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void setKubernetesClient(KubernetesClient kubernetesClient) { |
| 98 | + this.client = kubernetesClient; |
| 99 | + firstDependentResourceConfigMap.setKubernetesClient(kubernetesClient); |
| 100 | + secondDependentResourceConfigMap.setKubernetesClient(kubernetesClient); |
| 101 | + } |
| 102 | + |
| 103 | + public static String configMapKey(ConfigMap configMap) { |
| 104 | + return configMap.getMetadata().getName() + "#" + configMap.getMetadata().getNamespace(); |
| 105 | + } |
| 106 | + |
| 107 | + public static String configMapKeyFromPrimary(IndexDiscriminatorTestCustomResource primary, |
| 108 | + String nameSuffix) { |
| 109 | + return primary.getMetadata().getName() + nameSuffix + "#" |
| 110 | + + primary.getMetadata().getNamespace(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public DeleteControl cleanup(IndexDiscriminatorTestCustomResource resource, |
| 115 | + Context<IndexDiscriminatorTestCustomResource> context) { |
| 116 | + firstDependentResourceConfigMap.delete(resource, context); |
| 117 | + secondDependentResourceConfigMap.delete(resource, context); |
| 118 | + return DeleteControl.defaultDelete(); |
| 119 | + } |
| 120 | +} |
0 commit comments