Skip to content

Commit 96e59cd

Browse files
csviriCopilot
andauthored
fix: do not NPE when adding a finalizer to a resource deleted mid-retry (#3530)
* fix: do not NPE when adding a finalizer to a resource deleted mid-retry `conflictRetryingPatchPrimary` / `conflictRetryingPatch` re-read the resource from the API server after a 409 or 422 and then re-evaluate the precondition: resource = operation.inNamespace(ns).withName(name).get(); `get()` returns null if the resource was deleted in the meantime, so the next iteration calls the precondition with null. `removeFinalizer` anticipates this: r -> { if (r == null) { log.warn("Cannot remove finalizer since resource not exists."); return false; } return r.hasFinalizer(finalizerName); } but `addFinalizer` passes `r -> !r.hasFinalizer(finalizerName)`, which throws a NullPointerException instead of exiting cleanly. Gives `addFinalizer` the same null guard, in both `ResourceOperations` and the deprecated `PrimaryUpdateAndCacheUtils`. No test is added: reaching the retry path requires stubbing the client to answer 409/422 and then 404 through the whole fabric8 DSL chain, which the existing unit tests are not set up for. * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4a12a39 commit 96e59cd

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,13 @@ public static <P extends HasMetadata> P addFinalizer(
287287
r.addFinalizer(finalizerName);
288288
return r;
289289
},
290-
r -> !r.hasFinalizer(finalizerName));
290+
r -> {
291+
if (r == null) {
292+
log.warn("Cannot add finalizer since resource no longer exists.");
293+
return false;
294+
}
295+
return !r.hasFinalizer(finalizerName);
296+
});
291297
}
292298

293299
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,13 @@ public P addFinalizer(String finalizerName, boolean cacheOnly) {
10851085
r.addFinalizer(finalizerName);
10861086
return r;
10871087
},
1088-
r -> !r.hasFinalizer(finalizerName),
1088+
r -> {
1089+
if (r == null) {
1090+
log.warn("Cannot add finalizer since resource no longer exists.");
1091+
return false;
1092+
}
1093+
return !r.hasFinalizer(finalizerName);
1094+
},
10891095
cacheOnly);
10901096
}
10911097

0 commit comments

Comments
 (0)