@@ -59,6 +59,15 @@ If the metric no longer exceeds the panic threshold, exit the panic mode.
5959
6060*/
6161
62+ const (
63+ KPALabelPrefix = "kpa." + scalingcontext .AutoscalingLabelPrefix
64+ targetBurstCapacityLabel = KPALabelPrefix + "target-burst-capacity"
65+ activationScaleLabel = KPALabelPrefix + "activation-scale"
66+ panicThresholdLabel = KPALabelPrefix + "panic-threshold"
67+ stableWindowLabel = KPALabelPrefix + "stable-window"
68+ scaleDownDelayLabel = KPALabelPrefix + "scale-down-delay"
69+ )
70+
6271// KpaScalingContext defines parameters for scaling decisions.
6372type KpaScalingContext struct {
6473 scalingcontext.BaseScalingContext
@@ -100,6 +109,57 @@ func NewKpaScalingContext() *KpaScalingContext {
100109 }
101110}
102111
112+ func (k * KpaScalingContext ) UpdateByPaTypes (pa * autoscalingv1alpha1.PodAutoscaler ) error {
113+ err := k .BaseScalingContext .UpdateByPaTypes (pa )
114+ if err != nil {
115+ return err
116+ }
117+ for key , value := range pa .Labels {
118+ switch key {
119+ case targetBurstCapacityLabel :
120+ v , err := strconv .ParseFloat (value , 64 )
121+ if err != nil {
122+ return err
123+ }
124+ k .TargetBurstCapacity = v
125+ case activationScaleLabel :
126+ v , err := strconv .ParseInt (value , 10 , 32 )
127+ if err != nil {
128+ return err
129+ }
130+ k .ActivationScale = int32 (v )
131+ case panicThresholdLabel :
132+ v , err := strconv .ParseFloat (value , 64 )
133+ if err != nil {
134+ return err
135+ }
136+ k .PanicThreshold = v
137+ case stableWindowLabel :
138+ v , err := time .ParseDuration (value )
139+ if err != nil {
140+ return err
141+ }
142+ k .StableWindow = v
143+ case scaleDownDelayLabel :
144+ v , err := time .ParseDuration (value )
145+ if err != nil {
146+ return err
147+ }
148+ k .ScaleDownDelay = v
149+ }
150+ }
151+ // unset some attribute if there are no configuration
152+ if _ , exists := pa .Labels [scaleDownDelayLabel ]; ! exists {
153+ // TODO N.B. three parts of KPAScaler are stateful : panic_window, stable_window and delay windows.
154+ // reconcile() updates KpaScalingContext periodically, but doesn't reset these three parts.
155+ // These three parts are only initialized when controller starts.
156+ // Therefore, apply kpa.yaml cannot modify the panic_duration, stable_duration and delay_window duration
157+ k .ScaleDownDelay = 0
158+ }
159+
160+ return nil
161+ }
162+
103163type KpaAutoscaler struct {
104164 specMux sync.RWMutex
105165 metricClient metrics.MetricClient
@@ -122,10 +182,14 @@ func NewKpaAutoscaler(readyPodsCount int, spec *KpaScalingContext) (*KpaAutoscal
122182 }
123183
124184 // Create a new delay window based on the ScaleDownDelay specified in the spec
125- if spec .ScaleDownDelay <= 0 {
185+ if spec .ScaleDownDelay < 0 {
126186 return nil , errors .New ("ScaleDownDelay must be positive" )
127187 }
128- delayWindow := aggregation .NewTimeWindow (spec .ScaleDownDelay , 1 * time .Second )
188+ var delayWindow * aggregation.TimeWindow
189+ // If specify ScaleDownDelay, KpaAutoscaler.delayWindow will be initialized
190+ if spec .ScaleDownDelay > 0 {
191+ delayWindow = aggregation .NewTimeWindow (spec .ScaleDownDelay , 1 * time .Second )
192+ }
129193
130194 // As KNative stated:
131195 // We always start in the panic mode, if the deployment is scaled up over 1 pod.
@@ -144,6 +208,7 @@ func NewKpaAutoscaler(readyPodsCount int, spec *KpaScalingContext) (*KpaAutoscal
144208 // TODO missing MetricClient
145209 metricsFetcher := & metrics.RestMetricsFetcher {}
146210 metricsClient := metrics .NewKPAMetricsClient (metricsFetcher )
211+
147212 scalingAlgorithm := algorithm.KpaScalingAlgorithm {}
148213
149214 return & KpaAutoscaler {
@@ -253,6 +318,7 @@ func (k *KpaAutoscaler) Scale(originalReadyPodsCount int, metricKey metrics.Name
253318 // in that case).
254319 klog .V (4 ).InfoS ("DelayWindow details" , "delayWindow" , k .delayWindow .String ())
255320 if k .delayWindow != nil {
321+ // the actual desiredPodCount will be recorded, but return the max replicas during passed delayWindow
256322 k .delayWindow .Record (now , float64 (desiredPodCount ))
257323 delayedPodCount , err := k .delayWindow .Max ()
258324 if err != nil {
@@ -319,15 +385,10 @@ func (k *KpaAutoscaler) UpdateSourceMetrics(ctx context.Context, metricKey metri
319385func (k * KpaAutoscaler ) UpdateScalingContext (pa autoscalingv1alpha1.PodAutoscaler ) error {
320386 k .specMux .Lock ()
321387 defer k .specMux .Unlock ()
322-
323- targetValue , err := strconv .ParseFloat (pa .Spec .TargetValue , 64 )
388+ err := k .scalingContext .UpdateByPaTypes (& pa )
324389 if err != nil {
325- klog .ErrorS (err , "Failed to parse target value" , "targetValue" , pa .Spec .TargetValue )
326390 return err
327391 }
328- k .scalingContext .TargetValue = targetValue
329- k .scalingContext .ScalingMetric = pa .Spec .TargetMetric
330-
331392 return nil
332393}
333394
0 commit comments