|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.javaoperatorsdk.operator.baseapi.externalstateinstatus; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +import io.javaoperatorsdk.operator.api.reconciler.Cleaner; |
| 25 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 26 | +import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; |
| 27 | +import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; |
| 28 | +import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext; |
| 29 | +import io.javaoperatorsdk.operator.api.reconciler.Reconciler; |
| 30 | +import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; |
| 31 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 32 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 33 | +import io.javaoperatorsdk.operator.processing.event.source.polling.PerResourcePollingConfigurationBuilder; |
| 34 | +import io.javaoperatorsdk.operator.processing.event.source.polling.PerResourcePollingEventSource; |
| 35 | +import io.javaoperatorsdk.operator.support.ExternalIDGenServiceMock; |
| 36 | +import io.javaoperatorsdk.operator.support.ExternalResource; |
| 37 | +import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; |
| 38 | + |
| 39 | +/** |
| 40 | + * Manages an external resource (living in the {@link ExternalIDGenServiceMock fake external |
| 41 | + * service}) while storing the external resource's state - here just its generated ID - directly in |
| 42 | + * the <b>status</b> of the custom resource. |
| 43 | + * |
| 44 | + * <p>This pattern only works reliably thanks to the stronger read-after-write consistency for |
| 45 | + * updates: after the external resource is created, its ID is persisted through {@link |
| 46 | + * UpdateControl#patchStatus(io.fabric8.kubernetes.api.model.HasMetadata)}. The patched resource |
| 47 | + * (holding the ID) is placed into the controller's cache, so the very next reconciliation observes |
| 48 | + * the ID and does not create a duplicate external resource - even before the informer delivers the |
| 49 | + * update event. |
| 50 | + */ |
| 51 | +@ControllerConfiguration |
| 52 | +public class ExternalStateInStatusReconciler |
| 53 | + implements Reconciler<ExternalStateInStatusCustomResource>, |
| 54 | + Cleaner<ExternalStateInStatusCustomResource>, |
| 55 | + TestExecutionInfoProvider { |
| 56 | + |
| 57 | + private final AtomicInteger numberOfExecutions = new AtomicInteger(0); |
| 58 | + |
| 59 | + private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance(); |
| 60 | + |
| 61 | + PerResourcePollingEventSource<ExternalResource, ExternalStateInStatusCustomResource, String> |
| 62 | + externalResourceEventSource; |
| 63 | + |
| 64 | + @Override |
| 65 | + public UpdateControl<ExternalStateInStatusCustomResource> reconcile( |
| 66 | + ExternalStateInStatusCustomResource resource, |
| 67 | + Context<ExternalStateInStatusCustomResource> context) { |
| 68 | + numberOfExecutions.addAndGet(1); |
| 69 | + |
| 70 | + var externalResource = context.getSecondaryResource(ExternalResource.class); |
| 71 | + if (externalResource.isEmpty()) { |
| 72 | + // No external resource is associated with this primary yet. If we already stored an ID we |
| 73 | + // are just waiting for the poll to catch up (do nothing), otherwise we create the external |
| 74 | + // resource and persist its ID into the status. Relying on read-after-write consistency the |
| 75 | + // stored ID is visible on the next reconciliation, so no duplicate is created. |
| 76 | + if (idFromStatus(resource) == null) { |
| 77 | + return createExternalResource(resource); |
| 78 | + } |
| 79 | + return UpdateControl.noUpdate(); |
| 80 | + } |
| 81 | + |
| 82 | + var currentExternalResource = externalResource.orElseThrow(); |
| 83 | + if (!currentExternalResource.getData().equals(resource.getSpec().getData())) { |
| 84 | + updateExternalResource(resource, currentExternalResource); |
| 85 | + } |
| 86 | + return UpdateControl.noUpdate(); |
| 87 | + } |
| 88 | + |
| 89 | + private UpdateControl<ExternalStateInStatusCustomResource> createExternalResource( |
| 90 | + ExternalStateInStatusCustomResource resource) { |
| 91 | + var createdResource = |
| 92 | + externalService.create(new ExternalResource(resource.getSpec().getData())); |
| 93 | + |
| 94 | + // Make sure the freshly created external resource is available in the poll cache for the next |
| 95 | + // reconciliation, so it is not created again. |
| 96 | + externalResourceEventSource.handleRecentResourceCreate( |
| 97 | + ResourceID.fromResource(resource), createdResource); |
| 98 | + |
| 99 | + resource.setStatus(new ExternalStateInStatusStatus().setId(createdResource.getId())); |
| 100 | + return UpdateControl.patchStatus(resource); |
| 101 | + } |
| 102 | + |
| 103 | + private void updateExternalResource( |
| 104 | + ExternalStateInStatusCustomResource resource, ExternalResource externalResource) { |
| 105 | + var newResource = new ExternalResource(externalResource.getId(), resource.getSpec().getData()); |
| 106 | + externalService.update(newResource); |
| 107 | + externalResourceEventSource.handleRecentResourceUpdate( |
| 108 | + ResourceID.fromResource(resource), newResource, externalResource); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public DeleteControl cleanup( |
| 113 | + ExternalStateInStatusCustomResource resource, |
| 114 | + Context<ExternalStateInStatusCustomResource> context) { |
| 115 | + var id = idFromStatus(resource); |
| 116 | + if (id != null) { |
| 117 | + externalService.delete(id); |
| 118 | + } |
| 119 | + return DeleteControl.defaultDelete(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int getNumberOfExecutions() { |
| 124 | + return numberOfExecutions.get(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public List<EventSource<?, ExternalStateInStatusCustomResource>> prepareEventSources( |
| 129 | + EventSourceContext<ExternalStateInStatusCustomResource> context) { |
| 130 | + |
| 131 | + final PerResourcePollingEventSource.ResourceFetcher< |
| 132 | + ExternalResource, ExternalStateInStatusCustomResource> |
| 133 | + fetcher = |
| 134 | + (ExternalStateInStatusCustomResource primaryResource) -> { |
| 135 | + var id = idFromStatus(primaryResource); |
| 136 | + if (id == null) { |
| 137 | + return Collections.emptySet(); |
| 138 | + } |
| 139 | + return externalService.read(id).map(Set::of).orElseGet(Collections::emptySet); |
| 140 | + }; |
| 141 | + externalResourceEventSource = |
| 142 | + new PerResourcePollingEventSource<>( |
| 143 | + ExternalResource.class, |
| 144 | + context, |
| 145 | + new PerResourcePollingConfigurationBuilder< |
| 146 | + ExternalResource, ExternalStateInStatusCustomResource, String>( |
| 147 | + fetcher, Duration.ofMillis(300L)) |
| 148 | + .build()); |
| 149 | + |
| 150 | + return List.of(externalResourceEventSource); |
| 151 | + } |
| 152 | + |
| 153 | + private static String idFromStatus(ExternalStateInStatusCustomResource resource) { |
| 154 | + return resource.getStatus() == null ? null : resource.getStatus().getId(); |
| 155 | + } |
| 156 | +} |
0 commit comments