fix: fail Deployment tracking fast on ReplicaSet pod-create errors

Merged
IL
25d ago
+3240-122

Summary

The Deployment tracker now reports pod-create failures of the Deployment's new ReplicaSet (the ReplicaFailure/FailedCreate condition, e.g. exceeded quota) instead of waiting for the tracking timeout. Failures the rollout cannot recover from on its own fail the deploy immediately; the rest are charged against the allowed-failures budget.

This is a port of #399 by @Sharvash. When branch 1 was merged into main (#389) and deleted, GitHub closed #399 without merging it. His two commits are cherry-picked unchanged, with his authorship and sign-off. Our commits on top fix the quota classification and a lint error.

Closes #398

What

  • Commits by @Sharvash (from #399):
    • fix: fail Deployment tracking on ReplicaSet pod-create errors: reads the durable ReplicaFailure condition of the new ReplicaSet rather than transient events, so failures that happened before tracking started are also seen. Only ReplicaSets controlled by the Deployment are considered.
    • fix: handle ReplicaSet failure transitions consistently: waits for the initial ReplicaSet list before classifying, keeps informer events in order (unbuffered, cancellation-aware sends), handles ReplicaSet replacement, selector leave and re-entry, and deletion. Reports a failure once per ReplicaSet incarnation and re-arms it when the condition clears. Ignores a lagging condition once the Deployment is ready.
  • Our commits:
    • fix: keep ReplicaSet quota failures counted while the rollout can release quota: see Quota classification below.
    • chore: fix errorlint in deployment tracker test harness and document FailureMode field.
    • fix: bound quota quantities parsed from a ReplicaSet failure message: the FailedCreate message can be written by anyone who can update the ReplicaSet status, and a quantity with a huge exponent kept the tracker busy for minutes. Only the bounded forms the quota admission plugin prints are parsed now; anything else keeps the failure counted.

Quota classification

The original heuristic reported a quota failure as fatal when the rollout could not scale an old ReplicaSet down at that moment. Several rollouts that do recover were therefore misclassified. One example: replicas: 3, maxSurge: 2, maxUnavailable: 0, a quota of 4 pods. The new ReplicaSet gets 1 of its 2 pods. Once that pod becomes available, an old pod is scaled down, quota is freed and the rollout continues. A quota failure is now fatal only if all of the following hold:

  1. Rollout projection. Assuming every existing pod of the new ReplicaSet becomes available, the Deployment controller still would not scale any old ReplicaSet down: sum(old spec.replicas) + existing new pods <= replicas - maxUnavailable. The same formula as reconcileOldReplicaSets.
  2. Fresh pod list. A quorum list pods confirms the projection: no pod of the rollout is terminating, no old ReplicaSet has surplus pods, and the new ReplicaSet is still missing pods. This check is needed because the status that brings the condition counts only the pods listed before the failed sync created any, and status.terminatingReplicas is feature-gated.
  3. Quota status. get resourcequotas/<name from the message> shows that the quota still has no room for the request. The ReplicaSet controller keeps FailedCreate across its retry backoff, which can last minutes, so the condition can outlive the shortage.
  4. Persistence. All of the above still hold after 10 seconds, re-evaluated from scratch. The checks read different objects at different moments, and quota usage drops only once the quota controller catches up with deleted pods.

If a check cannot be made (the list or get fails, for example because of RBAC, or the message does not parse), the failure stays counted, as on main.

Behaviour changes

  • A Deployment whose new ReplicaSet cannot create pods is reported as failed. Before, tracking waited for the timeout.
    • Fatal, failing the deploy regardless of failures-allowed: an invalid pod spec (... is invalid:), or a quota failure confirmed as described above. A quota deadlock is reported about 10 seconds after it is observed.
    • Counted against the failures budget: everything else, such as webhook or RBAC rejections, recoverable quota failures, and quota failures that cannot be confirmed.
    • A quota failure that clears within the confirmation period is not reported at all.
  • New public API in pkg/tracker/deployment: type FailureMode, FailureModeCounted (the zero value, so existing code that builds a DeploymentStatus keeps the old meaning) and FailureModeFatal, plus the DeploymentStatus.FailureMode field. It is meaningful only when IsFailed is set.
  • dyntracker: a fatal Deployment failure marks the resource Failed, which fails the readiness task immediately.
  • dyntracker fail modes (statestore/readiness_task_state.go): with IgnoreAndContinueDeployProcess, a resource marked Failed does not fail the task. Only FailWholeDeployProcessImmediately and LegacyHopeUntilEndOfDeployProcess fail on it. Before this PR nothing set ResourceStatusFailed, so the change affects only the new fatal failures: they respect werf.io/fail-mode: IgnoreAndContinueDeployProcess.
  • Sticky failures are reported once. After an event-sourced failure, the Deployment tracker no longer re-stamps IsFailed/FailedReason into every later status. Each failure is charged to the budget once, where before it was charged on every subsequent update. The StatefulSet and DaemonSet trackers still re-stamp.
  • utils.FindNewReplicaSet only considers ReplicaSets controlled by the Deployment.
  • RBAC: fast-failing on a quota deadlock needs list pods (already required for tracking) and get resourcequotas in the namespace. The built-in view, edit and admin roles include both. Without them, quota failures stay counted.

Why

#398: a deploy that hits exceeded quota (or any other pod-create error) hangs until the tracking timeout, and the reason only shows up in ReplicaSet events, which the tracker did not watch.

Testing

  • go test ./... and go test -race on the touched packages pass. task lint reports 0 issues.
  • Unit tests for every classification case. Each counted case fails with the corresponding check disabled.
  • kind (Kubernetes v1.36.1) with a ResourceQuota on pods, using the Deployment from the example above, tracked through dyntracker the way nelm does it:
    • quota 3 (deadlock): main waits for the timeout. This branch fails with the FailedCreate ... exceeded quota diagnostic about 10 seconds after the failure appears.
    • quota 4 (recoverable): #399 as it was failed after 2s. This branch keeps waiting, and the rollout completes once the ReplicaSet controller retries. That took several minutes because of its retry backoff.
    • Tracking started in the middle of that backoff, after quota had been freed but with the condition still set: counted, and the rollout completes.

Merging

Please merge with a merge commit or a rebase, not a squash, so that @Sharvash stays the author of his commits.