From 86a68d7ce7ffda8dbd4b7dfefdd0fce32f187fd3 Mon Sep 17 00:00:00 2001 From: maplemiao Date: Wed, 22 Apr 2026 10:55:31 +0800 Subject: [PATCH] refactor(kube): clamp negative StatusComputeWorkers values to zero Address review feedback: when a caller passes a negative value to WithStatusComputeWorkers, coerce it to zero rather than propagating it to the underlying cli-utils watcher, where the behavior is undefined. Zero is the safe default and matches the SDK opt-in contract. Signed-off-by: maplemiao --- pkg/kube/options.go | 15 ++++++++++----- pkg/kube/statuswait_test.go | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/kube/options.go b/pkg/kube/options.go index db3de2f33..7853cb63a 100644 --- a/pkg/kube/options.go +++ b/pkg/kube/options.go @@ -78,13 +78,18 @@ func WithKStatusReaders(readers ...engine.StatusReader) WaitOption { // for Deployments) when many resources are updated simultaneously. // // A value of 0 (the default) keeps the underlying cli-utils behavior, where -// status is computed synchronously on the informer goroutine. SDK consumers -// (for example helm-controller) inherit this conservative default and can -// opt in explicitly. The Helm CLI passes a non-zero value so that -// `helm install/upgrade/rollback` users get the fix for multi-minute waits -// out of the box. See https://github.com/fluxcd/cli-utils/pull/20. +// status is computed synchronously on the informer goroutine. Negative values +// are clamped to 0 so callers cannot propagate invalid counts to the +// underlying watcher. SDK consumers (for example helm-controller) inherit +// this conservative default and can opt in explicitly. The Helm CLI passes +// a non-zero value so that `helm install/upgrade/rollback` users get the +// fix for multi-minute waits out of the box. +// See https://github.com/fluxcd/cli-utils/pull/20. func WithStatusComputeWorkers(n int) WaitOption { return func(wo *waitOptions) { + if n < 0 { + n = 0 + } wo.statusComputeWorkers = n } } diff --git a/pkg/kube/statuswait_test.go b/pkg/kube/statuswait_test.go index 56d4b98e2..0a0052f1a 100644 --- a/pkg/kube/statuswait_test.go +++ b/pkg/kube/statuswait_test.go @@ -1301,6 +1301,14 @@ func TestWaitOptionFunctions(t *testing.T) { assert.Equal(t, 8, opts.statusComputeWorkers) }) + t.Run("WithStatusComputeWorkers clamps negative values to zero", func(t *testing.T) { + t.Parallel() + opts := &waitOptions{} + WithStatusComputeWorkers(-1)(opts) + assert.Equal(t, 0, opts.statusComputeWorkers, + "negative worker counts must not propagate to the underlying watcher") + }) + t.Run("waitOptions.statusComputeWorkers defaults to zero", func(t *testing.T) { t.Parallel() opts := &waitOptions{}