Skip to content

fix(keda): hold one replica until the new revision is ready - #4143

Open
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/keda-hold-replica-until-ready
Open

Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/keda-hold-replica-until-ready

Conversation

@Elvand-Lie

@Elvand-Lie Elvand-Lie commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

On a keda redeploy, an existing scaler at min 0 can scale the Deployment to zero before the readiness wait sees a pod of the new revision. The deploy then reports success without proving the new image started, so a broken image deploys "successfully".

Before the raw deploy (and after exposure preflight validation), raise an existing HTTPScaledObject or Kafka ScaledObject from min 0 to 1. For HTTP, also wait until the ScaledObject the KEDA HTTP add-on generates reports min 1, since KEDA scales from that one. Opt keda.NewDeployer into k8s.WithRequireReadyReplica(true) so the readiness wait keeps polling through any settled zero caused by a stale KEDA loop until a pod of the new revision becomes ready, and require ObservedGeneration >= Generation before evaluating nonzero readiness so stale status and pods from the previous revision cannot satisfy the wait. The raw deployer and exported k8s.WaitForDeploymentAvailable* functions keep #4075's scale-to-zero behavior unchanged.

Once ensureHTTPScaledObject or ensureScaledObject reconciles the configured scaler spec, disarm the deferred restore so a later failure (for example RecordExposure, clearExposure, or WaitForHTTPScaledObjectAvailable after Update) cannot roll a configured scale.min back to 0. If the deploy fails before scaler reconciliation, the minimum is restored to 0 under a bounded timeout.

Changes

  • Add k8s.WithRequireReadyReplica(bool) and waitForDeploymentAvailable(..., allowZero bool) in pkg/k8s/deployer.go and pkg/k8s/wait.go; guard nonzero readiness with ObservedGeneration >= Generation
  • Opt keda.NewDeployer into k8s.WithRequireReadyReplica(true), move validateExposure before the hold, add setScalerMin / WaitForScaledObjectMinReplicas, and disarm the deferred restore once ensureHTTPScaledObject or ensureScaledObject reconciles the configured spec (pkg/keda/deployer.go, pkg/keda/kafka_scaling.go, pkg/keda/wait.go)
  • Write the scaler with update, since the documented deploy roles do not grant patch
  • Add unit and fake-API-server Deploy tests in pkg/k8s/wait_test.go, pkg/k8s/wait_poll_test.go, pkg/keda/deployer_hold_test.go, and pkg/keda/kafka_scaling_test.go

/kind bug

Fixes #4118

Related: #4068, #4075, #4089

Testing

go test -v -race -count=1 ./pkg/keda/...
go test -v -race -count=1 -run 'TestCheckIfDeploymentIsAvailable|TestWaitForDeploymentAvailable|Test_preserveAutoscaledReplicas' ./pkg/k8s/...
go vet ./pkg/k8s/... ./pkg/keda/...

Covers HTTP and Kafka hold/restore ordering, waiting through settled zero and stale previous-revision generations, raw keeping settled-zero acceptance, and restore disarming after ensureHTTPScaledObject (Update and DeepEqual no-op). Not run locally: a live cluster with KEDA installed.

Release Note

On a keda redeploy, func deploy now holds one replica and waits for a ready pod of the new revision before allowing scale-to-zero.

Docs


@knative-prow knative-prow Bot added the kind/bug Bugs label Nov 8, 2026
@knative-prow

knative-prow Bot commented Nov 8, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Elvand-Lie
Once this PR has been reviewed and has the lgtm label, please assign jrangelramos for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added size/L 🤖 PR changes 100-499 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels Nov 8, 2026
@knative-prow

knative-prow Bot commented Nov 8, 2026

Copy link
Copy Markdown

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@Elvand-Lie
Elvand-Lie force-pushed the fix/keda-hold-replica-until-ready branch from eac53a3 to e5da8ff Compare November 8, 2026 13:11
@lkingland

Copy link
Copy Markdown
Member

Thanks @Elvand-Lie. Holding the minimum at 1 across the redeploy looks like the right direction for #4118, and it sits well with #4075 and #4089 now that both are in. A few questions:

  1. Should the readiness wait also refuse a settled zero while the hold is in place? A KEDA loop that hasn't picked up the new minimum can still scale to zero, and the wait would accept that. With the minimum at 1, KEDA brings the pod back, so waiting it out would close KEDA deployer: verify the new revision runs before allowing scale-to-zero #4118 rather than narrow it.
  2. Could the deferred restore end up undoing a configured minimum? It stays armed after ensureHTTPScaledObject / ensureScaledObject have written the configured value. So if scale.min is 1 and a later step fails, it looks like the restore would set it back to 0. Would disarming it once ensure* has run make sense?

/ok-to-test

@knative-prow knative-prow Bot added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels Nov 24, 2026
On a redeploy the existing scaler can still be at min 0, so KEDA may
scale the Deployment away before the readiness wait sees a pod of the
new revision. The deploy then reports success without proving the new
image ever started.

Before the raw deploy (and after exposure preflight validation), raise
an existing HTTPScaledObject or Kafka ScaledObject from min 0 to 1,
whatever minimum is now configured. For HTTP, also wait until the
generated ScaledObject KEDA scales from reports min 1. Opt KEDA into
k8s.WithRequireReadyReplica(true) so the readiness wait keeps polling
through any settled zero caused by a stale KEDA loop until a pod of
the new revision becomes ready, while keeping the raw deployer's
scale-to-zero readiness behavior unchanged. Also require
ObservedGeneration >= Generation before evaluating nonzero readiness so
stale ready counts and pods from the previous revision cannot satisfy
the wait after an image update.

Disarm the deferred restore to min 0 once ensureHTTPScaledObject or
ensureScaledObject has reconciled the configured scaler spec, so a
later failure cannot roll a configured scale.min back to 0. If the
deploy fails before scaler reconciliation, restore min 0 under a
bounded timeout. The scaler is written with update, which the
documented deploy roles already grant.

Fixes knative#4118

Signed-off-by: Elvand Lie Nababan <elvandlie@gmail.com>
@Elvand-Lie
Elvand-Lie force-pushed the fix/keda-hold-replica-until-ready branch from e5da8ff to 29ce097 Compare November 25, 2026 18:55
@knative-prow knative-prow Bot added size/XL 🤖 PR changes 500-999 lines, ignoring generated files. and removed size/L 🤖 PR changes 100-499 lines, ignoring generated files. labels Nov 25, 2026
@Elvand-Lie

Copy link
Copy Markdown
Contributor Author

@lkingland I rebased onto main (with #4075 and #4089) and updated both parts:

  1. I added k8s.WithRequireReadyReplica(true) for keda.NewDeployer so the readiness wait keeps polling through a settled zero until KEDA brings a pod back, while raw and exported k8s.WaitForDeploymentAvailable* keep fix(k8s): tolerate scale-to-zero during deployment readiness #4075's zero-replica behavior. While testing that, I noticed the nonzero path in pkg/k8s/wait.go did not check ObservedGeneration < Generation, so after an image update stale ready counts and the old revision's pod (which the hold can wake up) could satisfy the wait immediately. I lifted the ObservedGeneration < Generation guard above both branches.
  2. I disarmed the deferred restore once ensureHTTPScaledObject (Create, Update, or DeepEqual no-op) or ensureScaledObject reconciles the configured spec, so failures in RecordExposure, clearExposure, or WaitForHTTPScaledObjectAvailable after Update leave a configured scale.min: 1 intact. Earlier failures still restore 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Bugs ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/XL 🤖 PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KEDA deployer: verify the new revision runs before allowing scale-to-zero

2 participants