From 3cf905128e666e8e0f4ced7c141a15322b32bdcf Mon Sep 17 00:00:00 2001 From: Sascha Schwarze Date: Thu, 28 Dec 2023 09:38:50 +0100 Subject: [PATCH] Add environment variable to disable changing an existing PodAutoscaler status to a negative desired count --- pkg/reconciler/autoscaling/kpa/kpa.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/reconciler/autoscaling/kpa/kpa.go b/pkg/reconciler/autoscaling/kpa/kpa.go index 013022d2df49..00ec8a06bf0e 100644 --- a/pkg/reconciler/autoscaling/kpa/kpa.go +++ b/pkg/reconciler/autoscaling/kpa/kpa.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "math" + "os" "go.opencensus.io/stats" "go.uber.org/zap" @@ -53,6 +54,14 @@ const ( minActivators = 2 ) +var ( + ignoreSwitchToNegativeDesiredScale = false +) + +func init() { + ignoreSwitchToNegativeDesiredScale = os.Getenv("IGNORE_SWITCH_TO_NEGATIVE_DESIRED_SCALE") == "true" +} + // podCounts keeps record of various numbers of pods // for each revision. type podCounts struct { @@ -220,7 +229,10 @@ func (c *Reconciler) reconcileDecider(ctx context.Context, pa *autoscalingv1alph } func computeStatus(ctx context.Context, pa *autoscalingv1alpha1.PodAutoscaler, pc podCounts, logger *zap.SugaredLogger) { - pa.Status.DesiredScale, pa.Status.ActualScale = ptr.Int32(int32(pc.want)), ptr.Int32(int32(pc.ready)) + pa.Status.ActualScale = ptr.Int32(int32(pc.ready)) + if !ignoreSwitchToNegativeDesiredScale || pc.want > -1 || pa.Status.DesiredScale == nil || *pa.Status.DesiredScale < 0 { + pa.Status.DesiredScale = ptr.Int32(int32(pc.want)) + } reportMetrics(pa, pc) computeActiveCondition(ctx, pa, pc)