Skip to content

Commit d4f431d

Browse files
committed
feat: experimental utility methods to add finalizer
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 4aa17d4 commit d4f431d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,62 @@ private static <P extends HasMetadata> P pollLocalCache(
229229
throw new OperatorException(e);
230230
}
231231
}
232+
233+
/**
234+
* Experimental. Patches finalizer. For retry uses informer cache to get the fresh resources,
235+
* therefore makes less Kubernetes API Calls.
236+
*/
237+
@Experimental(
238+
"Not used internally for now. Therefor we don't consider it well tested. But the intention is"
239+
+ " to have it as default in the future.")
240+
public static <P extends HasMetadata> P addFinalizer(
241+
P resource, String finalizer, Context<P> context) {
242+
243+
if (resource.hasFinalizer(finalizer)) {
244+
log.debug("Skipping adding finalizer, since already present.");
245+
return resource;
246+
}
247+
248+
return updateAndCacheResource(
249+
resource,
250+
context,
251+
r -> r,
252+
r ->
253+
context
254+
.getClient()
255+
.resource(r)
256+
.edit(
257+
res -> {
258+
res.addFinalizer(finalizer);
259+
return res;
260+
}));
261+
}
262+
263+
/**
264+
* Experimental. Removes finalizer, for retry uses informer cache to get the fresh resources,
265+
* therefore makes less Kubernetes API Calls.
266+
*/
267+
@Experimental(
268+
"Not used internally for now. Therefor we don't consider it well tested. But the intention is"
269+
+ " to have it as default in the future.")
270+
public static <P extends HasMetadata> P removeFinalizer(
271+
P resource, String finalizer, Context<P> context) {
272+
if (!resource.hasFinalizer(finalizer)) {
273+
log.debug("Skipping removing finalizer, since not present.");
274+
return resource;
275+
}
276+
return updateAndCacheResource(
277+
resource,
278+
context,
279+
r -> r,
280+
r ->
281+
context
282+
.getClient()
283+
.resource(r)
284+
.edit(
285+
res -> {
286+
res.removeFinalizer(finalizer);
287+
return res;
288+
}));
289+
}
232290
}

0 commit comments

Comments
 (0)