|
| 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.readcacheafterwrite.readownupdates; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 21 | + |
| 22 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 23 | +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; |
| 24 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 25 | +import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration; |
| 26 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 27 | +import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; |
| 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.informer.InformerEventSource; |
| 34 | + |
| 35 | +@ControllerConfiguration |
| 36 | +public class ReadOwnUpdatesReconciler implements Reconciler<ReadOwnUpdatesCustomResource> { |
| 37 | + |
| 38 | + public static final String RESOURCE_VERSION_INDEX = "resourceVersionIndex"; |
| 39 | + private final AtomicBoolean issueFound = new AtomicBoolean(false); |
| 40 | + |
| 41 | + private InformerEventSource<ConfigMap, ReadOwnUpdatesCustomResource> configMapEventSource; |
| 42 | + |
| 43 | + @Override |
| 44 | + public UpdateControl<ReadOwnUpdatesCustomResource> reconcile( |
| 45 | + ReadOwnUpdatesCustomResource resource, Context<ReadOwnUpdatesCustomResource> context) { |
| 46 | + try { |
| 47 | + var updated = context.resourceOperations().serverSideApply(prepareCM(resource, 1)); |
| 48 | + var cachedCM = context.getSecondaryResource(ConfigMap.class); |
| 49 | + if (cachedCM.isEmpty()) { |
| 50 | + throw new IllegalStateException("Error for resource: " + ResourceID.fromResource(resource)); |
| 51 | + } |
| 52 | + checkListContainsCM(updated); |
| 53 | + checkIfResourceVersionIndexContainsUpdated(updated); |
| 54 | + updated = context.resourceOperations().serverSideApply(prepareCM(resource, 2)); |
| 55 | + cachedCM = context.getSecondaryResource(ConfigMap.class); |
| 56 | + if (!cachedCM |
| 57 | + .orElseThrow() |
| 58 | + .getMetadata() |
| 59 | + .getResourceVersion() |
| 60 | + .equals(updated.getMetadata().getResourceVersion())) { |
| 61 | + throw new IllegalStateException( |
| 62 | + "Update error for resource: " + ResourceID.fromResource(resource)); |
| 63 | + } |
| 64 | + checkListContainsCM(updated); |
| 65 | + checkIfResourceVersionIndexContainsUpdated(updated); |
| 66 | + |
| 67 | + ensureStatusExists(resource); |
| 68 | + resource.getStatus().setUpdated(true); |
| 69 | + return UpdateControl.patchStatus(resource); |
| 70 | + } catch (IllegalStateException e) { |
| 71 | + issueFound.set(true); |
| 72 | + throw e; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void checkIfResourceVersionIndexContainsUpdated(ConfigMap updated) { |
| 77 | + if (configMapEventSource |
| 78 | + .byIndex(RESOURCE_VERSION_INDEX, updated.getMetadata().getResourceVersion()) |
| 79 | + .stream() |
| 80 | + .noneMatch( |
| 81 | + r -> |
| 82 | + ResourceID.fromResource(r).equals(ResourceID.fromResource(updated)) |
| 83 | + && r.getMetadata() |
| 84 | + .getResourceVersion() |
| 85 | + .equals(updated.getMetadata().getResourceVersion()))) { |
| 86 | + throw new IllegalStateException( |
| 87 | + "Index does not contain resource: " + ResourceID.fromResource(updated)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void checkListContainsCM(ConfigMap updated) { |
| 92 | + if (configMapEventSource |
| 93 | + .list() |
| 94 | + .noneMatch( |
| 95 | + r -> |
| 96 | + ResourceID.fromResource(r).equals(ResourceID.fromResource(updated)) |
| 97 | + && r.getMetadata() |
| 98 | + .getResourceVersion() |
| 99 | + .equals(updated.getMetadata().getResourceVersion()))) { |
| 100 | + throw new IllegalStateException( |
| 101 | + "List does not contain resource: " + ResourceID.fromResource(updated)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static ConfigMap prepareCM(ReadOwnUpdatesCustomResource p, int num) { |
| 106 | + var cm = |
| 107 | + new ConfigMapBuilder() |
| 108 | + .withMetadata( |
| 109 | + new ObjectMetaBuilder() |
| 110 | + .withName(p.getMetadata().getName()) |
| 111 | + .withNamespace(p.getMetadata().getNamespace()) |
| 112 | + .build()) |
| 113 | + .withData(Map.of("name", p.getMetadata().getName(), "num", "" + num)) |
| 114 | + .build(); |
| 115 | + cm.addOwnerReference(p); |
| 116 | + return cm; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public List<EventSource<?, ReadOwnUpdatesCustomResource>> prepareEventSources( |
| 121 | + EventSourceContext<ReadOwnUpdatesCustomResource> context) { |
| 122 | + configMapEventSource = |
| 123 | + new InformerEventSource<>( |
| 124 | + InformerEventSourceConfiguration.from( |
| 125 | + ConfigMap.class, ReadOwnUpdatesCustomResource.class) |
| 126 | + .build(), |
| 127 | + context); |
| 128 | + configMapEventSource.addIndexers( |
| 129 | + Map.of(RESOURCE_VERSION_INDEX, cm -> List.of(cm.getMetadata().getResourceVersion()))); |
| 130 | + return List.of(configMapEventSource); |
| 131 | + } |
| 132 | + |
| 133 | + private void ensureStatusExists(ReadOwnUpdatesCustomResource resource) { |
| 134 | + ReadOwnUpdatesStatus status = resource.getStatus(); |
| 135 | + if (status == null) { |
| 136 | + status = new ReadOwnUpdatesStatus(); |
| 137 | + resource.setStatus(status); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public boolean isIssueFound() { |
| 142 | + return issueFound.get(); |
| 143 | + } |
| 144 | +} |
0 commit comments