|
| 1 | +package io.javaoperatorsdk.operator.api.reconciler; |
| 2 | + |
| 3 | +import java.util.function.Supplier; |
| 4 | +import java.util.function.UnaryOperator; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 10 | +import io.fabric8.kubernetes.client.dsl.base.PatchContext; |
| 11 | +import io.fabric8.kubernetes.client.dsl.base.PatchType; |
| 12 | +import io.javaoperatorsdk.operator.api.reconciler.support.PrimaryResourceCache; |
| 13 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 14 | + |
| 15 | +/** |
| 16 | + * Utility methods to patch the primary resource state and store it to the related cache, to make |
| 17 | + * sure that fresh resource is present for the next reconciliation. The main use case for such |
| 18 | + * updates is to store state is resource status. Use of optimistic locking is not desired for such |
| 19 | + * updates, since we don't want to patch fail and lose information that we want to store. |
| 20 | + */ |
| 21 | +public class PrimaryUpdateAndCacheUtils { |
| 22 | + |
| 23 | + private PrimaryUpdateAndCacheUtils() {} |
| 24 | + |
| 25 | + private static final Logger log = LoggerFactory.getLogger(PrimaryUpdateAndCacheUtils.class); |
| 26 | + |
| 27 | + /** |
| 28 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 29 | + * Using update (PUT) method. |
| 30 | + * |
| 31 | + * @param primary resource |
| 32 | + * @param context of reconciliation |
| 33 | + * @return updated resource |
| 34 | + * @param <P> primary resource type |
| 35 | + */ |
| 36 | + public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<P> context) { |
| 37 | + logWarnIfResourceVersionPresent(primary); |
| 38 | + return patchAndCacheStatus( |
| 39 | + primary, context, () -> context.getClient().resource(primary).updateStatus()); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 44 | + * Using JSON Merge patch. |
| 45 | + * |
| 46 | + * @param primary resource |
| 47 | + * @param context of reconciliation |
| 48 | + * @return updated resource |
| 49 | + * @param <P> primary resource type |
| 50 | + */ |
| 51 | + public static <P extends HasMetadata> P patchAndCacheStatus(P primary, Context<P> context) { |
| 52 | + logWarnIfResourceVersionPresent(primary); |
| 53 | + return patchAndCacheStatus( |
| 54 | + primary, context, () -> context.getClient().resource(primary).patchStatus()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 59 | + * Using JSON Patch. |
| 60 | + * |
| 61 | + * @param primary resource |
| 62 | + * @param context of reconciliation |
| 63 | + * @return updated resource |
| 64 | + * @param <P> primary resource type |
| 65 | + */ |
| 66 | + public static <P extends HasMetadata> P editAndCacheStatus( |
| 67 | + P primary, Context<P> context, UnaryOperator<P> operation) { |
| 68 | + logWarnIfResourceVersionPresent(primary); |
| 69 | + return patchAndCacheStatus( |
| 70 | + primary, context, () -> context.getClient().resource(primary).editStatus(operation)); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 75 | + * |
| 76 | + * @param primary resource |
| 77 | + * @param context of reconciliation |
| 78 | + * @param patch free implementation of cache |
| 79 | + * @return the updated resource. |
| 80 | + * @param <P> primary resource type |
| 81 | + */ |
| 82 | + public static <P extends HasMetadata> P patchAndCacheStatus( |
| 83 | + P primary, Context<P> context, Supplier<P> patch) { |
| 84 | + var updatedResource = patch.get(); |
| 85 | + context |
| 86 | + .eventSourceRetriever() |
| 87 | + .getControllerEventSource() |
| 88 | + .handleRecentResourceUpdate(ResourceID.fromResource(primary), updatedResource, primary); |
| 89 | + return updatedResource; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 94 | + * Using Server Side Apply. |
| 95 | + * |
| 96 | + * @param primary resource |
| 97 | + * @param freshResourceWithStatus - fresh resource with target state |
| 98 | + * @param context of reconciliation |
| 99 | + * @return the updated resource. |
| 100 | + * @param <P> primary resource type |
| 101 | + */ |
| 102 | + public static <P extends HasMetadata> P ssaPatchAndCacheStatus( |
| 103 | + P primary, P freshResourceWithStatus, Context<P> context) { |
| 104 | + logWarnIfResourceVersionPresent(freshResourceWithStatus); |
| 105 | + var res = |
| 106 | + context |
| 107 | + .getClient() |
| 108 | + .resource(freshResourceWithStatus) |
| 109 | + .subresource("status") |
| 110 | + .patch( |
| 111 | + new PatchContext.Builder() |
| 112 | + .withForce(true) |
| 113 | + .withFieldManager(context.getControllerConfiguration().fieldManager()) |
| 114 | + .withPatchType(PatchType.SERVER_SIDE_APPLY) |
| 115 | + .build()); |
| 116 | + |
| 117 | + context |
| 118 | + .eventSourceRetriever() |
| 119 | + .getControllerEventSource() |
| 120 | + .handleRecentResourceUpdate(ResourceID.fromResource(primary), res, primary); |
| 121 | + return res; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Patches the resource and adds it to the {@link PrimaryResourceCache}. |
| 126 | + * |
| 127 | + * @param primary resource |
| 128 | + * @param freshResourceWithStatus - fresh resource with target state |
| 129 | + * @param context of reconciliation |
| 130 | + * @param cache - resource cache managed by user |
| 131 | + * @return the updated resource. |
| 132 | + * @param <P> primary resource type |
| 133 | + */ |
| 134 | + public static <P extends HasMetadata> P ssaPatchAndCacheStatus( |
| 135 | + P primary, P freshResourceWithStatus, Context<P> context, PrimaryResourceCache<P> cache) { |
| 136 | + logWarnIfResourceVersionPresent(freshResourceWithStatus); |
| 137 | + return patchAndCacheStatus( |
| 138 | + primary, |
| 139 | + cache, |
| 140 | + () -> |
| 141 | + context |
| 142 | + .getClient() |
| 143 | + .resource(freshResourceWithStatus) |
| 144 | + .subresource("status") |
| 145 | + .patch( |
| 146 | + new PatchContext.Builder() |
| 147 | + .withForce(true) |
| 148 | + .withFieldManager(context.getControllerConfiguration().fieldManager()) |
| 149 | + .withPatchType(PatchType.SERVER_SIDE_APPLY) |
| 150 | + .build())); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Patches the resource with JSON Patch and adds it to the {@link PrimaryResourceCache}. |
| 155 | + * |
| 156 | + * @param primary resource |
| 157 | + * @param context of reconciliation |
| 158 | + * @param cache - resource cache managed by user |
| 159 | + * @return the updated resource. |
| 160 | + * @param <P> primary resource type |
| 161 | + */ |
| 162 | + public static <P extends HasMetadata> P editAndCacheStatus( |
| 163 | + P primary, Context<P> context, PrimaryResourceCache<P> cache, UnaryOperator<P> operation) { |
| 164 | + logWarnIfResourceVersionPresent(primary); |
| 165 | + return patchAndCacheStatus( |
| 166 | + primary, cache, () -> context.getClient().resource(primary).editStatus(operation)); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Patches the resource with JSON Merge patch and adds it to the {@link PrimaryResourceCache} |
| 171 | + * provided. |
| 172 | + * |
| 173 | + * @param primary resource |
| 174 | + * @param context of reconciliation |
| 175 | + * @param cache - resource cache managed by user |
| 176 | + * @return the updated resource. |
| 177 | + * @param <P> primary resource type |
| 178 | + */ |
| 179 | + public static <P extends HasMetadata> P patchAndCacheStatus( |
| 180 | + P primary, Context<P> context, PrimaryResourceCache<P> cache) { |
| 181 | + logWarnIfResourceVersionPresent(primary); |
| 182 | + return patchAndCacheStatus( |
| 183 | + primary, cache, () -> context.getClient().resource(primary).patchStatus()); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Updates the resource and adds it to the {@link PrimaryResourceCache}. |
| 188 | + * |
| 189 | + * @param primary resource |
| 190 | + * @param context of reconciliation |
| 191 | + * @param cache - resource cache managed by user |
| 192 | + * @return the updated resource. |
| 193 | + * @param <P> primary resource type |
| 194 | + */ |
| 195 | + public static <P extends HasMetadata> P updateAndCacheStatus( |
| 196 | + P primary, Context<P> context, PrimaryResourceCache<P> cache) { |
| 197 | + logWarnIfResourceVersionPresent(primary); |
| 198 | + return patchAndCacheStatus( |
| 199 | + primary, cache, () -> context.getClient().resource(primary).updateStatus()); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Updates the resource using the user provided implementation anc caches the result. |
| 204 | + * |
| 205 | + * @param primary resource |
| 206 | + * @param cache resource cache managed by user |
| 207 | + * @param patch implementation of resource update* |
| 208 | + * @return the updated resource. |
| 209 | + * @param <P> primary resource type |
| 210 | + */ |
| 211 | + public static <P extends HasMetadata> P patchAndCacheStatus( |
| 212 | + P primary, PrimaryResourceCache<P> cache, Supplier<P> patch) { |
| 213 | + var updatedResource = patch.get(); |
| 214 | + cache.cacheResource(primary, updatedResource); |
| 215 | + return updatedResource; |
| 216 | + } |
| 217 | + |
| 218 | + private static <P extends HasMetadata> void logWarnIfResourceVersionPresent(P primary) { |
| 219 | + if (primary.getMetadata().getResourceVersion() != null) { |
| 220 | + log.warn( |
| 221 | + "The metadata.resourceVersion of primary resource is NOT null, " |
| 222 | + + "using optimistic locking is discouraged for this purpose. "); |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments