Skip to content

Commit b890153

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent c5ff462 commit b890153

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.lang.reflect.InvocationTargetException;
44
import java.time.LocalTime;
55
import java.time.temporal.ChronoUnit;
6+
import java.util.function.Predicate;
67
import java.util.function.UnaryOperator;
78

89
import org.slf4j.Logger;
@@ -158,7 +159,7 @@ public static <P extends HasMetadata> P updateAndCacheResource(
158159
long cachePollPeriodMillis) {
159160

160161
if (log.isDebugEnabled()) {
161-
log.debug("Conflict retrying update for: {}", ResourceID.fromResource(resourceToUpdate));
162+
log.debug("Update and cache: {}", ResourceID.fromResource(resourceToUpdate));
162163
}
163164
P modified = null;
164165
int retryIndex = 0;
@@ -240,22 +241,44 @@ private static <P extends HasMetadata> P pollLocalCache(
240241
@SuppressWarnings("unchecked")
241242
public static <P extends HasMetadata> P addFinalizer(
242243
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) {
243272
if (log.isDebugEnabled()) {
244273
log.debug("Conflict retrying update for: {}", ResourceID.fromResource(resource));
245274
}
246275
int retryIndex = 0;
247276
while (true) {
248277
try {
249-
if (resource.hasFinalizer(finalizerName)) {
278+
if (!preCondition.test(resource)) {
250279
return resource;
251280
}
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);
259282
} catch (KubernetesClientException e) {
260283
log.trace("Exception during patch for resource: {}", resource);
261284
retryIndex++;
@@ -342,14 +365,13 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
342365
}
343366
}
344367

345-
public static <P extends HasMetadata> P removeFinalizer() {
346-
return null;
347-
}
348-
349368
/**
350369
* Experimental. Patches finalizer. For retry uses informer cache to get the fresh resources,
351370
* therefore makes less Kubernetes API Calls.
352371
*/
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.")
353375
public static <P extends HasMetadata> P addFinalizer(
354376
P resource, String finalizer, Context<P> context) {
355377

@@ -377,6 +399,9 @@ public static <P extends HasMetadata> P addFinalizer(
377399
* Experimental. Removes finalizer, for retry uses informer cache to get the fresh resources,
378400
* therefore makes less Kubernetes API Calls.
379401
*/
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.")
380405
public static <P extends HasMetadata> P removeFinalizer(
381406
P resource, String finalizer, Context<P> context) {
382407
if (!resource.hasFinalizer(finalizer)) {

0 commit comments

Comments
 (0)