8000 Operator: don't recreate when the Auto update job fails without a compatibility result · Issue #55401 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

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

Description

@fma-xx

Description

With update.strategy: Auto, the operator runs the update-compatibility job and then reads the job pod's exit codes in AutoUpdateLogic.checkUpdateType. Exit code 0 means a rolling update. Every other outcome leads to decideRecreateUpdate, and that includes outcomes where the check didn't produce a result:

  • The Job is Failed with reason DeadlineExceeded (kc.operator.keycloak.update-pod-deadline-seconds, default 300 s).
  • A container never terminated. AutoUpdateLogic.exitCode() falls back to 1 when there is no terminated state.
  • The container exited with 1. In update-compatibility check that code only comes from picocli's exitCodeOnExecutionException(), i.e. an uncaught exception. Compatibility results are only 0 (ROLLING) or 3 (RECREATE).
  • The container was killed, e.g. exit 137, which lands in the default branch.

In all of these cases the operator has no compatibility result. It still takes the most disruptive path: handleRecreateUpdate scales the StatefulSet to 0 and every instance goes down.

The deadline covers sandbox creation and image pull, not only the two Keycloak starts. On a cluster with node autoscaling, the job pod can land on a fresh node and run out of time without any incompatible change. What we saw:

  • Keycloak Operator 26.8.2, Auto, 2 instances, CR change was env only.
  • Job created 09:05:15. Deadline reached 09:10:21. Both Keycloak pods were deleted at 09:10:20 and 09:10:24. The first pod was ready again at about 09:12:45.
  • We reproduced the same outage on a test CR.

Value proposition

Auto is meant to avoid downtime for compatible changes. Today a slow node or registry is enough to cause a full outage, and the CR status only shows that a recreate happened. Users then raise the deadline and hope it holds.

Goals

  • Recreate only on a real result that rules out rolling: exit 3 (incompatible) and 4 (rolling-updates feature disabled). Exit 2 (invalid config) probably belongs here too, since a retry won't change it.
  • For job deadline exceeded, container not terminated, exit 1 or killed: keep the current StatefulSet, set a status condition such as UpdateCheckFailed with the reason, and retry the job with backoff. The operator already does this when the job pod is missing (AutoUpdate force recreate #48812, fix: updating the auto logic if the pod cannot be found #49030).
  • Optional: a setting to keep today's behavior, e.g. spec.update.onCheckFailure: Recreate | Retry.

Non-Goals

  • Changing what happens when the check reports incompatible changes.
  • Changing Explicit or RecreateOnImageChange.

Discussion

  • Endless retries could block an update. A retry limit, after which the operator stops and reports instead of recreating, seems safer to us than an outage.

Notes

Related: #48812 (recreate after the job pod went missing, about 15 min outage), #49030 (its fix).

Activity

  1. fma-xx commented on Nov 20, 2026

    @fma-xx
    Author

    A correction to the description: activeDeadlineSeconds is set on the pod spec of the update job (KeycloakUpdateJobDependentResource#createPodSpec). So the pod fails with reason DeadlineExceeded, and with backoffLimit: 0 the Job ends as Failed with reason BackoffLimitExceeded. The effect is the same, but that points to a fix that mostly uses the Job controller.

    Proposal, which keeps today's behavior by default:

    1. New operator setting, e.g. kc.operator.keycloak.update-job-retries, default 0. desired() uses it for backoffLimit instead of JOB_RETRIES.

    2. When retries are > 0, add a podFailurePolicy that fails the Job right away on real results, so only "no result" outcomes are retried:

      podFailurePolicy:
        rules:
          - action: FailJob
            onExitCodes:
              containerName: desired   # update-compatibility check
              operator: In
              values: [2, 3, 4]
          - action: FailJob
            onExitCodes:
              containerName: actual    # update-compatibility metadata (init container)
              operator: In
              values: [2, 4]

      The Job controller only matches containers that terminated with a non-zero code, init containers included. A pod that hit the deadline, never pulled its image or was killed falls to the default Count and gets a new pod after the usual 10s/20s/40s backoff, with a fresh deadline. restartPolicy: Never is already set. podFailurePolicy is GA since Kubernetes 1.31.

    3. AutoUpdateLogic needs no new branch. isJobRunning already waits for the Complete/Failed condition, so the current StatefulSet stays as it is during retries. Once retries are used up, the logic is the same as today and still recreates.

    4. findPodForJob returns the first pod of the list. With retries the Job can have several pods, so it has to pick the newest one.

    With the default of 0, testImageChange and testNoJobReuse keep passing unchanged. I'd add a unit test for desired() and an UpdateTest case with retries set to 1 and a job pod that fails once.

    Would you accept a PR along these lines? If you'd rather have this on the CR (e.g. spec.update), or change the default so the operator stops and reports instead of recreating, I can do it that way instead.

  2. shawkins commented on Nov 23, 2026

    @shawkins
    Contributor

    #48812 was a related fix. I would be fine with a more aggressive code change this time around - as you are describing only do rolling on the exit codes that are known results from the command.

    In all other circumstances it seems like we need a status update and to retry the job. Whether this is done via the podFailurePolicy or via the KeycloakController/KeycloakUpdateJobDependentResource should be based upon what looks like the cleanest implementation. I don't mind if the behavior is to simply keep retrying indefinitely - the KeycloakController already polls / retries on failures.

  3. fma-xx commented on Nov 23, 2026

    @fma-xx
    Author

    Thanks. I'll do it in the controller like #49030 first: on any outcome that isn't a result, keep the StatefulSet, delete the Job, put the reason in the status and retry with backoff. Exit 0 rolls, 3 and 4 recreate as today.

    What was the exit 4 for?

  4. shawkins commented on Nov 23, 2026

    @shawkins
    Contributor

    What was the exit 4 for?

    Should be rolling-updates feature disabled.

  5. added 3 commits that reference this issue on Nov 27, 2026
    3bbe5a3
    00b0c40
    62bb193
  6. added
    kind/enhancement
    on Nov 27, 2026
  7. stianst commented on Nov 27, 2026

    @stianst
    Contributor

    It's not immediately clear to me if this is an enhancement or bug; please check the type if it is a bug

  8. added a commit that references this issue on Nov 27, 2026
    c8b1e3f
  9. fma-xx commented on Nov 27, 2026

    @fma-xx
    Author

    I'd keep it as an enhancement. Recreating when the check fails was the intended behavior, and this changes it: a broken image or config now blocks the update instead of recreating. That's also why it has an upgrade note for 27.0.0.

  10. added 4 commits that reference this issue on Dec 1, 2026
    af12ab6
    e6667e7
    920e481
    5c0277e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancement

    Fields

    Value

    None yet

    Effort

    None yet

    Votes

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0