|
| 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.resourceoperations; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.atomic.AtomicInteger; |
| 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.ResourceOperations.Options; |
| 31 | +import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; |
| 32 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
| 33 | +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; |
| 34 | + |
| 35 | +/** |
| 36 | + * Exercises the secondary-resource variants of {@link |
| 37 | + * io.javaoperatorsdk.operator.api.reconciler.ResourceOperations} - the overloads that take an |
| 38 | + * {@link InformerEventSource} so the written secondary is cached (and its own event filtered) |
| 39 | + * against that source. On every reconciliation it ensures a {@code ConfigMap} secondary exists with |
| 40 | + * the value dictated by the selected {@link Operation}, using {@code context.resourceOperations()}. |
| 41 | + * |
| 42 | + * <p>The operations are applied idempotently: the secondary is only written when missing or when |
| 43 | + * its data differs from the desired value. Together with own event filtering this keeps the |
| 44 | + * controller from looping on the secondary writes it performs itself. |
| 45 | + */ |
| 46 | +@ControllerConfiguration |
| 47 | +public class SecondaryResourceOperationsReconciler |
| 48 | + implements Reconciler<SecondaryResourceOperationsCustomResource> { |
| 49 | + |
| 50 | + public static final String DATA_KEY = "value"; |
| 51 | + public static final String CREATE_VALUE = "created"; |
| 52 | + public static final String APPLIED_VALUE = "applied"; |
| 53 | + |
| 54 | + public enum Operation { |
| 55 | + CREATE, |
| 56 | + SSA, |
| 57 | + UPDATE, |
| 58 | + JSON_PATCH, |
| 59 | + JSON_MERGE_PATCH |
| 60 | + } |
| 61 | + |
| 62 | + private volatile Operation operation; |
| 63 | + final AtomicInteger numberOfExecutions = new AtomicInteger(); |
| 64 | + |
| 65 | + private InformerEventSource<ConfigMap, SecondaryResourceOperationsCustomResource> |
| 66 | + configMapEventSource; |
| 67 | + |
| 68 | + @Override |
| 69 | + public UpdateControl<SecondaryResourceOperationsCustomResource> reconcile( |
| 70 | + SecondaryResourceOperationsCustomResource resource, |
| 71 | + Context<SecondaryResourceOperationsCustomResource> context) { |
| 72 | + numberOfExecutions.incrementAndGet(); |
| 73 | + var ops = context.resourceOperations(); |
| 74 | + var actual = context.getSecondaryResource(ConfigMap.class).orElse(null); |
| 75 | + |
| 76 | + if (operation == Operation.CREATE) { |
| 77 | + if (actual == null) { |
| 78 | + ops.create( |
| 79 | + desiredConfigMap(resource, CREATE_VALUE), configMapEventSource, Options.cacheOnly()); |
| 80 | + } |
| 81 | + return UpdateControl.noUpdate(); |
| 82 | + } |
| 83 | + |
| 84 | + // for the update/patch variants the secondary must exist first |
| 85 | + if (actual == null) { |
| 86 | + ops.create( |
| 87 | + desiredConfigMap(resource, CREATE_VALUE), configMapEventSource, Options.cacheOnly()); |
| 88 | + return UpdateControl.noUpdate(); |
| 89 | + } |
| 90 | + |
| 91 | + // idempotency guard: only write when the secondary does not yet hold the desired value |
| 92 | + if (APPLIED_VALUE.equals(actual.getData().get(DATA_KEY))) { |
| 93 | + return UpdateControl.noUpdate(); |
| 94 | + } |
| 95 | + |
| 96 | + switch (operation) { |
| 97 | + case SSA -> { |
| 98 | + var desired = desiredConfigMap(resource, APPLIED_VALUE); |
| 99 | + desired.getMetadata().setResourceVersion(null); |
| 100 | + ops.serverSideApply(desired, configMapEventSource, Options.forceFilterEvents()); |
| 101 | + } |
| 102 | + case UPDATE -> { |
| 103 | + actual.getData().put(DATA_KEY, APPLIED_VALUE); |
| 104 | + ops.update(actual, configMapEventSource, Options.filterIfOptimisticLocking()); |
| 105 | + } |
| 106 | + case JSON_PATCH -> |
| 107 | + ops.jsonPatch( |
| 108 | + actual, |
| 109 | + cm -> { |
| 110 | + cm.getData().put(DATA_KEY, APPLIED_VALUE); |
| 111 | + return cm; |
| 112 | + }, |
| 113 | + configMapEventSource, |
| 114 | + Options.filterIfOptimisticLocking()); |
| 115 | + case JSON_MERGE_PATCH -> { |
| 116 | + var desired = desiredConfigMap(resource, APPLIED_VALUE); |
| 117 | + ops.jsonMergePatch(desired, configMapEventSource, Options.filterIfOptimisticLocking()); |
| 118 | + } |
| 119 | + default -> throw new IllegalStateException("Unexpected operation: " + operation); |
| 120 | + } |
| 121 | + return UpdateControl.noUpdate(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public List<EventSource<?, SecondaryResourceOperationsCustomResource>> prepareEventSources( |
| 126 | + EventSourceContext<SecondaryResourceOperationsCustomResource> context) { |
| 127 | + configMapEventSource = |
| 128 | + new InformerEventSource<>( |
| 129 | + InformerEventSourceConfiguration.from( |
| 130 | + ConfigMap.class, SecondaryResourceOperationsCustomResource.class) |
| 131 | + .build(), |
| 132 | + context); |
| 133 | + return List.of(configMapEventSource); |
| 134 | + } |
| 135 | + |
| 136 | + private static ConfigMap desiredConfigMap( |
| 137 | + SecondaryResourceOperationsCustomResource primary, String value) { |
| 138 | + var cm = |
| 139 | + new ConfigMapBuilder() |
| 140 | + .withMetadata( |
| 141 | + new ObjectMetaBuilder() |
| 142 | + .withName(primary.getMetadata().getName()) |
| 143 | + .withNamespace(primary.getMetadata().getNamespace()) |
| 144 | + .build()) |
| 145 | + .withData(Map.of(DATA_KEY, value)) |
| 146 | + .build(); |
| 147 | + cm.addOwnerReference(primary); |
| 148 | + return cm; |
| 149 | + } |
| 150 | + |
| 151 | + public void setOperation(Operation operation) { |
| 152 | + this.operation = operation; |
| 153 | + } |
| 154 | +} |
0 commit comments