|
3 | 3 | import java.lang.reflect.InvocationTargetException;
|
4 | 4 | import java.time.LocalTime;
|
5 | 5 | import java.time.temporal.ChronoUnit;
|
| 6 | +import java.util.function.Predicate; |
6 | 7 | import java.util.function.UnaryOperator;
|
7 | 8 |
|
8 | 9 | import org.slf4j.Logger;
|
@@ -158,7 +159,7 @@ public static <P extends HasMetadata> P updateAndCacheResource(
|
158 | 159 | long cachePollPeriodMillis) {
|
159 | 160 |
|
160 | 161 | if (log.isDebugEnabled()) {
|
161 |
| - log.debug("Conflict retrying update for: {}", ResourceID.fromResource(resourceToUpdate)); |
| 162 | + log.debug("Update and cache: {}", ResourceID.fromResource(resourceToUpdate)); |
162 | 163 | }
|
163 | 164 | P modified = null;
|
164 | 165 | int retryIndex = 0;
|
@@ -240,22 +241,44 @@ private static <P extends HasMetadata> P pollLocalCache(
|
240 | 241 | @SuppressWarnings("unchecked")
|
241 | 242 | public static <P extends HasMetadata> P addFinalizer(
|
242 | 243 | KubernetesClient client, P resource, String finalizerName) {
|
| 244 | + return conflictRetryingPatch( |
| 245 | + client, |
| 246 | + resource, |
| 247 | + r -> { |
| 248 | + r.addFinalizer(finalizerName); |
| 249 | + return r; |
| 250 | + }, |
| 251 | + r -> !r.hasFinalizer(finalizerName)); |
| 252 | + } |
| 253 | + |
| 254 | + public static <P extends HasMetadata> P removeFinalizer( |
| 255 | + KubernetesClient client, P resource, String finalizerName) { |
| 256 | + return conflictRetryingPatch( |
| 257 | + client, |
| 258 | + resource, |
| 259 | + r -> { |
| 260 | + r.removeFinalizer(finalizerName); |
| 261 | + return r; |
| 262 | + }, |
| 263 | + r -> r.hasFinalizer(finalizerName)); |
| 264 | + } |
| 265 | + |
| 266 | + @SuppressWarnings("unchecked") |
| 267 | + public static <P extends HasMetadata> P conflictRetryingPatch( |
| 268 | + KubernetesClient client, |
| 269 | + P resource, |
| 270 | + UnaryOperator<P> unaryOperator, |
| 271 | + Predicate<P> preCondition) { |
243 | 272 | if (log.isDebugEnabled()) {
|
244 | 273 | log.debug("Conflict retrying update for: {}", ResourceID.fromResource(resource));
|
245 | 274 | }
|
246 | 275 | int retryIndex = 0;
|
247 | 276 | while (true) {
|
248 | 277 | try {
|
249 |
| - if (resource.hasFinalizer(finalizerName)) { |
| 278 | + if (!preCondition.test(resource)) { |
250 | 279 | return resource;
|
251 | 280 | }
|
252 |
| - return client |
253 |
| - .resource(resource) |
254 |
| - .edit( |
255 |
| - r -> { |
256 |
| - r.addFinalizer(finalizerName); |
257 |
| - return r; |
258 |
| - }); |
| 281 | + return client.resource(resource).edit(unaryOperator); |
259 | 282 | } catch (KubernetesClientException e) {
|
260 | 283 | log.trace("Exception during patch for resource: {}", resource);
|
261 | 284 | retryIndex++;
|
@@ -342,14 +365,13 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
|
342 | 365 | }
|
343 | 366 | }
|
344 | 367 |
|
345 |
| - public static <P extends HasMetadata> P removeFinalizer() { |
346 |
| - return null; |
347 |
| - } |
348 |
| - |
349 | 368 | /**
|
350 | 369 | * Experimental. Patches finalizer. For retry uses informer cache to get the fresh resources,
|
351 | 370 | * therefore makes less Kubernetes API Calls.
|
352 | 371 | */
|
| 372 | + @Experimental( |
| 373 | + "Not used internally for now. Therefor we don't consider it well tested. But the intention is" |
| 374 | + + " to have it as default in the future.") |
353 | 375 | public static <P extends HasMetadata> P addFinalizer(
|
354 | 376 | P resource, String finalizer, Context<P> context) {
|
355 | 377 |
|
@@ -377,6 +399,9 @@ public static <P extends HasMetadata> P addFinalizer(
|
377 | 399 | * Experimental. Removes finalizer, for retry uses informer cache to get the fresh resources,
|
378 | 400 | * therefore makes less Kubernetes API Calls.
|
379 | 401 | */
|
| 402 | + @Experimental( |
| 403 | + "Not used internally for now. Therefor we don't consider it well tested. But the intention is" |
| 404 | + + " to have it as default in the future.") |
380 | 405 | public static <P extends HasMetadata> P removeFinalizer(
|
381 | 406 | P resource, String finalizer, Context<P> context) {
|
382 | 407 | if (!resource.hasFinalizer(finalizer)) {
|
|
0 commit comments