@@ -235,7 +235,8 @@ func (a *HorizontalController) processNextWorkItem() bool {
235
235
func (a * HorizontalController ) computeReplicasForMetrics (hpa * autoscalingv2.HorizontalPodAutoscaler , scale * autoscalingv1.Scale ,
236
236
metricSpecs []autoscalingv2.MetricSpec ) (replicas int32 , metric string , statuses []autoscalingv2.MetricStatus , timestamp time.Time , err error ) {
237
237
238
- currentReplicas := scale .Status .Replicas
238
+ specReplicas := scale .Spec .Replicas
239
+ statusReplicas := scale .Status .Replicas
239
240
240
241
statuses = make ([]autoscalingv2.MetricStatus , len (metricSpecs ))
241
242
@@ -258,7 +259,7 @@ func (a *HorizontalController) computeReplicasForMetrics(hpa *autoscalingv2.Hori
258
259
var invalidMetricError error
259
260
260
261
for i , metricSpec := range metricSpecs {
261
- replicaCountProposal , metricNameProposal , timestampProposal , err := a .computeReplicasForMetric (hpa , metricSpec , currentReplicas , selector , & statuses [i ])
262
+ replicaCountProposal , metricNameProposal , timestampProposal , err := a .computeReplicasForMetric (hpa , metricSpec , specReplicas , statusReplicas , selector , & statuses [i ])
262
263
263
264
if err != nil {
264
265
invalidMetricsCount ++
@@ -274,7 +275,7 @@ func (a *HorizontalController) computeReplicasForMetrics(hpa *autoscalingv2.Hori
274
275
// If all metrics are invalid or some are invalid and we would scale down,
275
276
// return an error and set the condition of the hpa based on the first invalid metric.
276
277
// Otherwise set the condition as scaling active as we're going to scale
277
- if invalidMetricsCount >= len (metricSpecs ) || (invalidMetricsCount > 0 && replicas < currentReplicas ) {
278
+ if invalidMetricsCount >= len (metricSpecs ) || (invalidMetricsCount > 0 && replicas < specReplicas ) {
278
279
return 0 , "" , statuses , time.Time {}, fmt .Errorf ("Invalid metrics (%v invalid out of %v), last error was: %v" , invalidMetricsCount , len (metricSpecs ), invalidMetricError )
279
280
} else {
280
281
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionTrue , "ValidMetricFound" , "the HPA was able to successfully calculate a replica count from %v" , metric )
@@ -284,7 +285,7 @@ func (a *HorizontalController) computeReplicasForMetrics(hpa *autoscalingv2.Hori
284
285
285
286
// computeReplicasForMetric computes the desired number of replicas for for a specific hpa and single metric specification.
286
287
func (a * HorizontalController ) computeReplicasForMetric (hpa * autoscalingv2.HorizontalPodAutoscaler , spec autoscalingv2.MetricSpec ,
287
- currentReplicas int32 , selector labels.Selector , status * autoscalingv2.MetricStatus ) (replicaCountProposal int32 , metricNameProposal string ,
288
+ specReplicas , statusReplicas int32 , selector labels.Selector , status * autoscalingv2.MetricStatus ) (replicaCountProposal int32 , metricNameProposal string ,
288
289
timestampProposal time.Time , err error ) {
289
290
290
291
switch spec .Type {
@@ -295,7 +296,7 @@ func (a *HorizontalController) computeReplicasForMetric(hpa *autoscalingv2.Horiz
295
296
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionFalse , "FailedGetObjectMetric" , "the HPA was unable to compute the replica count: %v" , err )
296
297
return 0 , "" , time.Time {}, fmt .Errorf ("failed to get object metric value: %v" , err )
297
298
}
298
- replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForObjectMetric (currentReplicas , spec , hpa , selector , status , metricSelector )
299
+ replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForObjectMetric (specReplicas , statusReplicas , spec , hpa , selector , status , metricSelector )
299
300
if err != nil {
300
301
return 0 , "" , time.Time {}, fmt .Errorf ("failed to get object metric value: %v" , err )
301
302
}
@@ -306,17 +307,17 @@ func (a *HorizontalController) computeReplicasForMetric(hpa *autoscalingv2.Horiz
306
307
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionFalse , "FailedGetPodsMetric" , "the HPA was unable to compute the replica count: %v" , err )
307
308
return 0 , "" , time.Time {}, fmt .Errorf ("failed to get pods metric value: %v" , err )
308
309
}
309
- replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForPodsMetric (currentReplicas , spec , hpa , selector , status , metricSelector )
310
+ replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForPodsMetric (specReplicas , spec , hpa , selector , status , metricSelector )
310
311
if err != nil {
311
312
return 0 , "" , time.Time {}, fmt .Errorf ("failed to get object metric value: %v" , err )
312
313
}
313
314
case autoscalingv2 .ResourceMetricSourceType :
314
- replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForResourceMetric (currentReplicas , spec , hpa , selector , status )
315
+ replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForResourceMetric (specReplicas , spec , hpa , selector , status )
315
316
if err != nil {
316
317
return 0 , "" , time.Time {}, err
317
318
}
318
319
case autoscalingv2 .ExternalMetricSourceType :
319
- replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForExternalMetric (currentReplicas , spec , hpa , selector , status )
320
+ replicaCountProposal , timestampProposal , metricNameProposal , err = a .computeStatusForExternalMetric (specReplicas , statusReplicas , spec , hpa , selector , status )
320
321
if err != nil {
321
322
return 0 , "" , time.Time {}, err
322
323
}
@@ -346,9 +347,9 @@ func (a *HorizontalController) reconcileKey(key string) (deleted bool, err error
346
347
}
347
348
348
349
// computeStatusForObjectMetric computes the desired number of replicas for the specified metric of type ObjectMetricSourceType.
349
- func (a * HorizontalController ) computeStatusForObjectMetric (currentReplicas int32 , metricSpec autoscalingv2.MetricSpec , hpa * autoscalingv2.HorizontalPodAutoscaler , selector labels.Selector , status * autoscalingv2.MetricStatus , metricSelector labels.Selector ) (int32 , time.Time , string , error ) {
350
+ func (a * HorizontalController ) computeStatusForObjectMetric (specReplicas , statusReplicas int32 , metricSpec autoscalingv2.MetricSpec , hpa * autoscalingv2.HorizontalPodAutoscaler , selector labels.Selector , status * autoscalingv2.MetricStatus , metricSelector labels.Selector ) (int32 , time.Time , string , error ) {
350
351
if metricSpec .Object .Target .Type == autoscalingv2 .ValueMetricType {
351
- replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetObjectMetricReplicas (currentReplicas , metricSpec .Object .Target .Value .MilliValue (), metricSpec .Object .Metric .Name , hpa .Namespace , & metricSpec .Object .DescribedObject , selector , metricSelector )
352
+ replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetObjectMetricReplicas (specReplicas , metricSpec .Object .Target .Value .MilliValue (), metricSpec .Object .Metric .Name , hpa .Namespace , & metricSpec .Object .DescribedObject , selector , metricSelector )
352
353
if err != nil {
353
354
a .eventRecorder .Event (hpa , v1 .EventTypeWarning , "FailedGetObjectMetric" , err .Error ())
354
355
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionFalse , "FailedGetObjectMetric" , "the HPA was unable to compute the replica count: %v" , err )
@@ -369,7 +370,7 @@ func (a *HorizontalController) computeStatusForObjectMetric(currentReplicas int3
369
370
}
370
371
return replicaCountProposal , timestampProposal , fmt .Sprintf ("%s metric %s" , metricSpec .Object .DescribedObject .Kind , metricSpec .Object .Metric .Name ), nil
371
372
} else if metricSpec .Object .Target .Type == autoscalingv2 .AverageValueMetricType {
372
- replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetObjectPerPodMetricReplicas (currentReplicas , metricSpec .Object .Target .AverageValue .MilliValue (), metricSpec .Object .Metric .Name , hpa .Namespace , & metricSpec .Object .DescribedObject , metricSelector )
373
+ replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetObjectPerPodMetricReplicas (statusReplicas , metricSpec .Object .Target .AverageValue .MilliValue (), metricSpec .Object .Metric .Name , hpa .Namespace , & metricSpec .Object .DescribedObject , metricSelector )
373
374
if err != nil {
374
375
a .eventRecorder .Event (hpa , v1 .EventTypeWarning , "FailedGetObjectMetric" , err .Error ())
375
376
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionFalse , "FailedGetObjectMetric" , "the HPA was unable to compute the replica count: %v" , err )
@@ -472,9 +473,9 @@ func (a *HorizontalController) computeStatusForResourceMetric(currentReplicas in
472
473
}
473
474
474
475
// computeStatusForExternalMetric computes the desired number of replicas for the specified metric of type ExternalMetricSourceType.
475
- func (a * HorizontalController ) computeStatusForExternalMetric (currentReplicas int32 , metricSpec autoscalingv2.MetricSpec , hpa * autoscalingv2.HorizontalPodAutoscaler , selector labels.Selector , status * autoscalingv2.MetricStatus ) (int32 , time.Time , string , error ) {
476
+ func (a * HorizontalController ) computeStatusForExternalMetric (specReplicas , statusReplicas int32 , metricSpec autoscalingv2.MetricSpec , hpa * autoscalingv2.HorizontalPodAutoscaler , selector labels.Selector , status * autoscalingv2.MetricStatus ) (int32 , time.Time , string , error ) {
476
477
if metricSpec .External .Target .AverageValue != nil {
477
- replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetExternalPerPodMetricReplicas (currentReplicas , metricSpec .External .Target .AverageValue .MilliValue (), metricSpec .External .Metric .Name , hpa .Namespace , metricSpec .External .Metric .Selector )
478
+ replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetExternalPerPodMetricReplicas (statusReplicas , metricSpec .External .Target .AverageValue .MilliValue (), metricSpec .External .Metric .Name , hpa .Namespace , metricSpec .External .Metric .Selector )
478
479
if err != nil {
479
480
a .eventRecorder .Event (hpa , v1 .EventTypeWarning , "FailedGetExternalMetric" , err .Error ())
480
481
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionFalse , "FailedGetExternalMetric" , "the HPA was unable to compute the replica count: %v" , err )
@@ -495,7 +496,7 @@ func (a *HorizontalController) computeStatusForExternalMetric(currentReplicas in
495
496
return replicaCountProposal , timestampProposal , fmt .Sprintf ("external metric %s(%+v)" , metricSpec .External .Metric .Name , metricSpec .External .Metric .Selector ), nil
496
497
}
497
498
if metricSpec .External .Target .Value != nil {
498
- replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetExternalMetricReplicas (currentReplicas , metricSpec .External .Target .Value .MilliValue (), metricSpec .External .Metric .Name , hpa .Namespace , metricSpec .External .Metric .Selector , selector )
499
+ replicaCountProposal , utilizationProposal , timestampProposal , err := a .replicaCalc .GetExternalMetricReplicas (specReplicas , metricSpec .External .Target .Value .MilliValue (), metricSpec .External .Metric .Name , hpa .Namespace , metricSpec .External .Metric .Selector , selector )
499
500
if err != nil {
500
501
a .eventRecorder .Event (hpa , v1 .EventTypeWarning , "FailedGetExternalMetric" , err .Error ())
501
502
setCondition (hpa , autoscalingv2 .ScalingActive , v1 .ConditionFalse , "FailedGetExternalMetric" , "the HPA was unable to compute the replica count: %v" , err )
@@ -570,7 +571,7 @@ func (a *HorizontalController) reconcileAutoscaler(hpav1Shared *autoscalingv1.Ho
570
571
return fmt .Errorf ("failed to query scale subresource for %s: %v" , reference , err )
571
572
}
572
573
setCondition (hpa , autoscalingv2 .AbleToScale , v1 .ConditionTrue , "SucceededGetScale" , "the HPA controller was able to get the target's current scale" )
573
- currentReplicas := scale .Status .Replicas
574
+ currentReplicas := scale .Spec .Replicas
574
575
a .recordInitialRecommendation (currentReplicas , key )
575
576
576
577
var (
0 commit comments