NokiaTube | New address | Mode Phone | Off high quality for Nokia Browser
Skip to content

Retry the update job when the compatibility check has no result - #55615

Open
fma-xx wants to merge 2 commits into
keycloak:mainfrom
fma-xx:55401-update-job-retry
Open

fma-xx wants to merge 2 commits into
keycloak:mainfrom
fma-xx:55401-update-job-retry

Conversation

@fma-xx

@fma-xx fma-xx commented Nov 27, 2026

Copy link
Copy Markdown

Closes #55401

The update type is now decided only by exit codes that the update-compatibility commands return as a result: 0 rolls, 3 and 4 recreate. For anything else (exit 1 or 2, a killed container, an exceeded deadline, a container that never finished), the operator keeps the current StatefulSet and retries the update job, as discussed in the issue.

Some choices you may want to check:

  • Exit 2 is retried as well. Keycloak returns it for an invalid command line or an invalid option value (PropertyException), so the new pods would fail to start too, and a recreate would take down all instances. The reason is reported in HasErrors. If you prefer to recreate on exit 2, it is a one-line change.
  • A failed job is kept for 5 minutes after it finished before it is replaced. That leaves time to inspect it and keeps the check from running in a tight loop. With the default pod deadline of 300s, one attempt takes at most about 10 minutes. The delay has to stay below the job's TTL of 30 minutes, since it is computed from the finished job. Exponential backoff would need an attempt count that survives the job deletion, which means new state on the CR. I can add that in a follow-up, e.g. as a status field.
  • Retries never stop. The reason shows up as a warning in the HasErrors condition. The status is written without advancing observedGeneration, since the current generation has not been applied yet, and it is only patched when it changes. It still runs the usual status checks, so validation errors of the new spec show up under the old observedGeneration, which seemed more useful than hiding them. A side effect is that tools waiting for the new generation can't tie the retry warning to their change.
  • This goes beyond the issue: when the current or desired StatefulSet has 0 instances, the check is skipped and a recreate update is used. Without that, a pending retry blocks every change to the CR, including scaling to 0, because the container still differs from the running one. I found this when UpdateTest cleanup timed out while scaling down a CR with a broken image. With no instances there is no availability to keep, so the check has nothing to decide. An update job that is already running when the CR is scaled to 0 runs until its deadline. A recreate also scales the StatefulSet to 0 first, so the shortcut keeps the reason of that recreate until it is finished.
  • A failing metadata command in the init container is retried too, although it runs with the currently deployed image and configuration, so changing the CR does not fix it. This affects for example a current image older than 26.2, which does not have the command (exit 2), and used to fall through to a recreate. The docs name the ways out: scale to 0 instances, or switch to RecreateOnImageChange or Explicit. Should a failure of the init container keep the old recreate behavior instead?
  • While a retry is reported, the status lacks the ServiceMonitor warning (metrics disabled or CRD missing). The dependent only sets it in reconcileManagedWorkflow(), which runs after the update logic interrupts, and running the workflow during a retry would also reconcile the StatefulSet. The warning is back after the next full reconciliation.

I tested this on a single-node cluster with the operator running locally and a 1 minute retry delay at the time. With a 15s deadline the job was retried four times and the pods were left alone. After raising the deadline, the operator did a rolling update. An invalid option value (exit 2) was retried too. AutoUpdateLogicTest covers the exit code mapping and the retry delay. UpdateTest#testImageChange used to expect a recreate for a non-existing image and now expects the retry warning and an unchanged StatefulSet. The new UpdateTest#testIncompatibleChange covers an Auto recreate (exit 3) and checks that its reason is kept. Both pass on that cluster in local mode.

The rolling update guide and the 27.0.0 upgrade notes describe the new behavior, since a broken image or configuration now blocks the update instead of recreating.

The change and tests were written with the help of an AI agent (Claude) and reviewed by me.

With the Auto update strategy, only the results of the update-compatibility commands decide the update type: exit code 0 rolls, 3 and 4 recreate. Any other outcome, like an exceeded deadline, a killed container or a failed command, keeps the current StatefulSet, reports a warning in the HasErrors condition and replaces the job 5 minutes after it finished.

When the current or desired StatefulSet has no instances, the check is skipped and a recreate update is used, so a pending retry does not block scaling to zero. While a recreate update scales down, its original reason is kept.

Closes keycloak#55401

Signed-off-by: Florian Mayer <[email protected]>
@fma-xx
fma-xx requested review from a team as code owners November 27, 2026 11:48
Copilot AI balanced review requested due to automatic review settings November 27, 2026 11:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The compatibility check can retry indefinitely by reading the wrong init-container result, and a blocked StatefulSet update can incorrectly mark the current generation as observed.

2 open issues
What changed in this PR

This PR makes the Operator’s Auto update strategy retry compatibility checks that produce no result, preserving the running Keycloak deployment instead of causing an unnecessary recreate.

Changes:

  • Adds five-minute retries for inconclusive checks and bypasses checks when scaling to or from zero instances.
  • Reports retry warnings while preserving the previously observed generation.
  • Adds test coverage and documents the behavior change and recovery options.
File Description
operator/​src/​test/​java/​org/​keycloak/​operator/​update/​impl/​AutoUpdateLogicTest.java Tests exit-code mapping, retry delays, and zero-instance handling.
operator/​src/​test/​java/​org/​keycloak/​operator/​testsuite/​integration/​UpdateTest.java Checks deployment preservation and incompatible-change recreates.
operator/​src/​main/​java/​org/​keycloak/​operator/​update/​UpdateLogic.java Adds a hook for status updates during interrupted reconciliation.
operator/​src/​main/​java/​org/​keycloak/​operator/​update/​impl/​BaseUpdateLogic.java Tracks retries and adds status warnings.
operator/​src/​main/​java/​org/​keycloak/​operator/​update/​impl/​AutoUpdateLogic.java Implements retry decisions, delays, diagnostics, and zero-instance handling.
operator/​src/​main/​java/​org/​keycloak/​operator/​controllers/​KeycloakUpdateJobDependentResource.java Exposes the Job lifetime constant for validation in tests.
operator/​src/​main/​java/​org/​keycloak/​operator/​controllers/​KeycloakController.java Patches retry status while preserving rescheduling.
docs/​guides/​operator/​rolling-updates.adoc Explains retry behavior and recovery options.
docs/​documentation/​upgrading/​topics/​changes/​changes-27_0_0.adoc Documents the upgrade impact of inconclusive checks blocking updates.

Review effort: Balanced

@stianst stianst added the kind/enhancement Categorizes a PR related to an enhancement label Nov 27, 2026
The update job keeps init containers of the pod template, so the 'actual' and 'desired' containers are selected by name. A retry without a previous status no longer marks the current generation as observed.

Closes keycloak#55401

Signed-off-by: Florian Mayer <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 13:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A cached rolling-check Job can retain a stale success across a zero-replica update and incorrectly authorize a later rolling update between different versions.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment on lines +91 to 97
if (hasNoInstances(ContextUtils.getCurrentStatefulSet(context).orElse(null), ContextUtils.getDesiredStatefulSet(context))) {
// no instances to keep available, and a pending update without a check result must not block scaling to zero.
decideRecreateUpdate(noInstancesReason(ContextUtils.getCurrentStatefulSet(context).orElse(null)));
return Optional.empty();
}

var existingJob = context.getSecondaryResource(Job.class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement Categorizes a PR related to an enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operator: don't recreate when the Auto update job fails without a compatibility result

3 participants