From 13ed4c3e130a2bdbd620aa12e710d6a27cae7605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Thu, 27 Feb 2025 13:10:10 +0100 Subject: [PATCH 1/2] docs: add section on otpimisitic locking to SSA blog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- docs/content/en/blog/news/nonssa-vs-ssa.md | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/docs/content/en/blog/news/nonssa-vs-ssa.md b/docs/content/en/blog/news/nonssa-vs-ssa.md index 241828d3e3..77e6e5f673 100644 --- a/docs/content/en/blog/news/nonssa-vs-ssa.md +++ b/docs/content/en/blog/news/nonssa-vs-ssa.md @@ -87,3 +87,47 @@ from the custom resource. See [`StatusPatchSSAMigrationIT`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/statuspatchnonlocking/StatusPatchSSAMigrationIT.java) for details. Feel free to report common issues, so we can prepare some utilities to handle them. + +## Optimistic concurrency control + +When you create a resource for SSA as mentioned above: + +```java +@Override +public UpdateControl reconcile(WebPage webPage, Context context) { + + reconcileLogicForManagedResources(webPage); + + WebPage statusPatch = new WebPage(); + statusPatch.setMetadata(new ObjectMetaBuilder() + .withName(webPage.getMetadata().getName()) + .withNamespace(webPage.getMetadata().getNamespace()) + .build()); + statusPatch.setStatus(updatedStatusForWebPage(webPage)); + + return UpdateControl.patchStatus(statusPatch); +} +``` + +It will apply changes even if the underlying resource or status subresource is changed while the reconciliation was running. +First, the framework always forces the conflicts in the background as advised in [Kubernetes docs](https://kubernetes.io/docs/reference/using-api/server-side-apply/#using-server-side-apply-in-a-controller), + in addition to that since the resource version is not set it won't do optimistic locking. If you still +want to have optimistic locking for the patch, use the resource version of the original resource: + +```java +@Override +public UpdateControl reconcile(WebPage webPage, Context context) { + + reconcileLogicForManagedResources(webPage); + + WebPage statusPatch = new WebPage(); + statusPatch.setMetadata(new ObjectMetaBuilder() + .withName(webPage.getMetadata().getName()) + .withNamespace(webPage.getMetadata().getNamespace()) + .withResourceVersion(webPage.getMetadata().getResourceVersion()) + .build()); + statusPatch.setStatus(updatedStatusForWebPage(webPage)); + + return UpdateControl.patchStatus(statusPatch); +} +``` From d8611614bf260e39eb92446ff6b922c31049805d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Thu, 27 Feb 2025 13:12:37 +0100 Subject: [PATCH 2/2] simplify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- docs/content/en/blog/news/nonssa-vs-ssa.md | 24 +++------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/docs/content/en/blog/news/nonssa-vs-ssa.md b/docs/content/en/blog/news/nonssa-vs-ssa.md index 77e6e5f673..e95fb0a295 100644 --- a/docs/content/en/blog/news/nonssa-vs-ssa.md +++ b/docs/content/en/blog/news/nonssa-vs-ssa.md @@ -90,27 +90,9 @@ Feel free to report common issues, so we can prepare some utilities to handle th ## Optimistic concurrency control -When you create a resource for SSA as mentioned above: - -```java -@Override -public UpdateControl reconcile(WebPage webPage, Context context) { - - reconcileLogicForManagedResources(webPage); - - WebPage statusPatch = new WebPage(); - statusPatch.setMetadata(new ObjectMetaBuilder() - .withName(webPage.getMetadata().getName()) - .withNamespace(webPage.getMetadata().getNamespace()) - .build()); - statusPatch.setStatus(updatedStatusForWebPage(webPage)); - - return UpdateControl.patchStatus(statusPatch); -} -``` - -It will apply changes even if the underlying resource or status subresource is changed while the reconciliation was running. -First, the framework always forces the conflicts in the background as advised in [Kubernetes docs](https://kubernetes.io/docs/reference/using-api/server-side-apply/#using-server-side-apply-in-a-controller), +When you create a resource for SSA as mentioned above, the framework will apply changes even if the underlying resource +or status subresource is changed while the reconciliation was running. +First, it always forces the conflicts in the background as advised in [Kubernetes docs](https://kubernetes.io/docs/reference/using-api/server-side-apply/#using-server-side-apply-in-a-controller), in addition to that since the resource version is not set it won't do optimistic locking. If you still want to have optimistic locking for the patch, use the resource version of the original resource: