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 <maplemiao@tencent.com>
pull/32043/head
maplemiao 6 months ago
parent d23a392b68
commit 86a68d7ce7

@ -78,13 +78,18 @@ func WithKStatusReaders(readers ...engine.StatusReader) WaitOption {
// for Deployments) when many resources are updated simultaneously. // for Deployments) when many resources are updated simultaneously.
// //
// A value of 0 (the default) keeps the underlying cli-utils behavior, where // A value of 0 (the default) keeps the underlying cli-utils behavior, where
// status is computed synchronously on the informer goroutine. SDK consumers // status is computed synchronously on the informer goroutine. Negative values
// (for example helm-controller) inherit this conservative default and can // are clamped to 0 so callers cannot propagate invalid counts to the
// opt in explicitly. The Helm CLI passes a non-zero value so that // underlying watcher. SDK consumers (for example helm-controller) inherit
// `helm install/upgrade/rollback` users get the fix for multi-minute waits // this conservative default and can opt in explicitly. The Helm CLI passes
// out of the box. See https://github.com/fluxcd/cli-utils/pull/20. // 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 { func WithStatusComputeWorkers(n int) WaitOption {
return func(wo *waitOptions) { return func(wo *waitOptions) {
if n < 0 {
n = 0
}
wo.statusComputeWorkers = n wo.statusComputeWorkers = n
} }
} }

@ -1301,6 +1301,14 @@ func TestWaitOptionFunctions(t *testing.T) {
assert.Equal(t, 8, opts.statusComputeWorkers) 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.Run("waitOptions.statusComputeWorkers defaults to zero", func(t *testing.T) {
t.Parallel() t.Parallel()
opts := &waitOptions{} opts := &waitOptions{}

Loading…
Cancel
Save