|
| 1 | +package io.javaoperatorsdk.operator.sample.externalstate; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; |
| 7 | + |
| 8 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 9 | +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; |
| 10 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 11 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 12 | +import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration; |
| 13 | +import io.javaoperatorsdk.operator.api.reconciler.*; |
| 14 | +import io.javaoperatorsdk.operator.junit.KubernetesClientAware; |
| 15 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 16 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 17 | +import io.javaoperatorsdk.operator.processing.event.source.EventSourceStartPriority; |
| 18 | +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; |
| 19 | +import io.javaoperatorsdk.operator.processing.event.source.polling.PerResourcePollingEventSource; |
| 20 | +import io.javaoperatorsdk.operator.support.ExternalIDGenServiceMock; |
| 21 | +import io.javaoperatorsdk.operator.support.ExternalResource; |
| 22 | +import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; |
| 23 | + |
| 24 | +@ControllerConfiguration() |
| 25 | +public class ExternalStateReconciler |
| 26 | + implements Reconciler<ExternalStateCustomResource>, Cleaner<ExternalStateCustomResource>, |
| 27 | + EventSourceInitializer<ExternalStateCustomResource>, KubernetesClientAware, |
| 28 | + TestExecutionInfoProvider { |
| 29 | + |
| 30 | + public static final String ID_KEY = "id"; |
| 31 | + private final AtomicInteger numberOfExecutions = new AtomicInteger(0); |
| 32 | + |
| 33 | + private ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance(); |
| 34 | + private KubernetesClient client; |
| 35 | + |
| 36 | + InformerEventSource<ConfigMap, ExternalStateCustomResource> configMapEventSource; |
| 37 | + PerResourcePollingEventSource<ExternalResource, ExternalStateCustomResource> externalResourceEventSource; |
| 38 | + |
| 39 | + @Override |
| 40 | + public UpdateControl<ExternalStateCustomResource> reconcile( |
| 41 | + ExternalStateCustomResource resource, Context<ExternalStateCustomResource> context) { |
| 42 | + numberOfExecutions.addAndGet(1); |
| 43 | + |
| 44 | + var externalResource = context.getSecondaryResource(ExternalResource.class); |
| 45 | + if (externalResource.isEmpty()) { |
| 46 | + createExternalResource(resource); |
| 47 | + } |
| 48 | + return UpdateControl.noUpdate(); |
| 49 | + } |
| 50 | + |
| 51 | + private void createExternalResource(ExternalStateCustomResource resource) { |
| 52 | + var createdResource = |
| 53 | + externalService.create(new ExternalResource(resource.getMetadata().getName())); |
| 54 | + var configMap = new ConfigMapBuilder() |
| 55 | + .withMetadata(new ObjectMetaBuilder() |
| 56 | + .withName(resource.getMetadata().getName()) |
| 57 | + .withNamespace(resource.getMetadata().getNamespace()) |
| 58 | + .build()) |
| 59 | + .withData(Map.of(ID_KEY, createdResource.getId())) |
| 60 | + .build(); |
| 61 | + client.configMaps().resource(configMap).create(); |
| 62 | + |
| 63 | + var primaryID = ResourceID.fromResource(resource); |
| 64 | + configMapEventSource.handleRecentResourceCreate(primaryID, configMap); |
| 65 | + externalResourceEventSource.handleRecentResourceCreate(primaryID, createdResource); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public DeleteControl cleanup(ExternalStateCustomResource resource, |
| 70 | + Context<ExternalStateCustomResource> context) { |
| 71 | + client.configMaps().inNamespace(resource.getMetadata().getNamespace()) |
| 72 | + .withName(resource.getMetadata().getName()).delete(); |
| 73 | + return DeleteControl.defaultDelete(); |
| 74 | + } |
| 75 | + |
| 76 | + public int getNumberOfExecutions() { |
| 77 | + return numberOfExecutions.get(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Map<String, EventSource> prepareEventSources( |
| 82 | + EventSourceContext<ExternalStateCustomResource> context) { |
| 83 | + |
| 84 | + configMapEventSource = new InformerEventSource<>( |
| 85 | + InformerConfiguration.from(ConfigMap.class, context).build(), context); |
| 86 | + configMapEventSource.setEventSourcePriority(EventSourceStartPriority.RESOURCE_STATE_LOADER); |
| 87 | + |
| 88 | + externalResourceEventSource = new PerResourcePollingEventSource<>(primaryResource -> { |
| 89 | + var configMap = configMapEventSource.getSecondaryResource(primaryResource).orElseThrow(); |
| 90 | + var id = configMap.getData().get(ID_KEY); |
| 91 | + var externalResource = externalService.read(id); |
| 92 | + return externalResource.map(er -> Set.of(er)).orElse(Collections.emptySet()); |
| 93 | + }, context.getPrimaryCache(), 300L, ExternalResource.class); |
| 94 | + |
| 95 | + return EventSourceInitializer.nameEventSources(configMapEventSource, |
| 96 | + externalResourceEventSource); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public KubernetesClient getKubernetesClient() { |
| 101 | + return client; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void setKubernetesClient(KubernetesClient kubernetesClient) { |
| 106 | + this.client = kubernetesClient; |
| 107 | + } |
| 108 | +} |
0 commit comments