|
11 | 11 | import io.fabric8.kubernetes.client.dsl.base.PatchContext; |
12 | 12 | import io.fabric8.kubernetes.client.dsl.base.PatchType; |
13 | 13 | import io.javaoperatorsdk.operator.api.reconciler.support.PrimaryResourceCache; |
| 14 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
14 | 15 |
|
15 | 16 | public class PrimaryUpdateAndCacheUtils { |
16 | 17 |
|
17 | 18 | private PrimaryUpdateAndCacheUtils() {} |
18 | 19 |
|
19 | 20 | private static final Logger log = LoggerFactory.getLogger(PrimaryUpdateAndCacheUtils.class); |
20 | 21 |
|
| 22 | + /** |
| 23 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 24 | + * Using update (PUT) method. |
| 25 | + * |
| 26 | + * @param primary resource |
| 27 | + * @param context of reconciliation |
| 28 | + * @return updated resource |
| 29 | + * @param <P> primary resource type |
| 30 | + */ |
| 31 | + public static <P extends HasMetadata> P updateAndCacheStatusWithLock( |
| 32 | + P primary, Context<P> context) { |
| 33 | + return patchAndCacheStatusWithLock( |
| 34 | + primary, context, (p, c) -> c.resource(primary).updateStatus()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 39 | + * Using JSON Merge patch. |
| 40 | + * |
| 41 | + * @param primary resource |
| 42 | + * @param context of reconciliation |
| 43 | + * @return updated resource |
| 44 | + * @param <P> primary resource type |
| 45 | + */ |
| 46 | + public static <P extends HasMetadata> P patchAndCacheStatusWithLock( |
| 47 | + P primary, Context<P> context) { |
| 48 | + return patchAndCacheStatusWithLock( |
| 49 | + primary, context, (p, c) -> c.resource(primary).patchStatus()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 54 | + * Using JSON Patch. |
| 55 | + * |
| 56 | + * @param primary resource |
| 57 | + * @param context of reconciliation |
| 58 | + * @return updated resource |
| 59 | + * @param <P> primary resource type |
| 60 | + */ |
| 61 | + public static <P extends HasMetadata> P editAndCacheStatusWithLock( |
| 62 | + P primary, Context<P> context, UnaryOperator<P> operation) { |
| 63 | + return patchAndCacheStatusWithLock( |
| 64 | + primary, context, (p, c) -> c.resource(primary).editStatus(operation)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 69 | + * |
| 70 | + * @param primary resource |
| 71 | + * @param context of reconciliation |
| 72 | + * @param patch free implementation of cache - make sure you use optimistic locking during the |
| 73 | + * update |
| 74 | + * @return the updated resource. |
| 75 | + * @param <P> primary resource type |
| 76 | + */ |
| 77 | + public static <P extends HasMetadata> P patchAndCacheStatusWithLock( |
| 78 | + P primary, Context<P> context, BiFunction<P, KubernetesClient, P> patch) { |
| 79 | + checkResourceVersionPresent(primary); |
| 80 | + var updatedResource = patch.apply(primary, context.getClient()); |
| 81 | + context |
| 82 | + .eventSourceRetriever() |
| 83 | + .getControllerEventSource() |
| 84 | + .handleRecentResourceUpdate(ResourceID.fromResource(primary), updatedResource, primary); |
| 85 | + return updatedResource; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Makes sure that the up-to-date primary resource will be present during the next reconciliation. |
| 90 | + * Using Server Side Apply. |
| 91 | + * |
| 92 | + * @param primary resource |
| 93 | + * @param freshResourceWithStatus - fresh resource with target state |
| 94 | + * @param context of reconciliation |
| 95 | + * @return the updated resource. |
| 96 | + * @param <P> primary resource type |
| 97 | + */ |
| 98 | + public static <P extends HasMetadata> P ssaPatchAndCacheStatusWithLock( |
| 99 | + P primary, P freshResourceWithStatus, Context<P> context) { |
| 100 | + checkResourceVersionPresent(freshResourceWithStatus); |
| 101 | + var res = |
| 102 | + context |
| 103 | + .getClient() |
| 104 | + .resource(freshResourceWithStatus) |
| 105 | + .subresource("status") |
| 106 | + .patch( |
| 107 | + new PatchContext.Builder() |
| 108 | + .withForce(true) |
| 109 | + .withFieldManager(context.getControllerConfiguration().fieldManager()) |
| 110 | + .withPatchType(PatchType.SERVER_SIDE_APPLY) |
| 111 | + .build()); |
| 112 | + |
| 113 | + context |
| 114 | + .eventSourceRetriever() |
| 115 | + .getControllerEventSource() |
| 116 | + .handleRecentResourceUpdate(ResourceID.fromResource(primary), res, primary); |
| 117 | + return res; |
| 118 | + } |
| 119 | + |
21 | 120 | /** |
22 | 121 | * Patches the resource and adds it to the {@link PrimaryResourceCache} provided. Optimistic |
23 | 122 | * locking is not required. |
|
0 commit comments