@@ -845,9 +845,11 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare
845
845
846
846
logger .V (4 ).Info ("Proposing desired replicas" ,
847
847
"desiredReplicas" , metricDesiredReplicas ,
848
+ "currentReplicas" , currentReplicas ,
848
849
"metric" , metricName ,
849
850
"timestamp" , metricTimestamp ,
850
- "scaleTarget" , reference )
851
+ "scaleTarget" , reference ,
852
+ "metrics" , metricStatuses )
851
853
852
854
rescaleMetric := ""
853
855
if metricDesiredReplicas > desiredReplicas {
@@ -861,9 +863,9 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare
861
863
rescaleReason = "All metrics below target"
862
864
}
863
865
if hpa .Spec .Behavior == nil {
864
- desiredReplicas = a .normalizeDesiredReplicas (hpa , key , currentReplicas , desiredReplicas , minReplicas )
866
+ desiredReplicas = a .normalizeDesiredReplicas (ctx , hpa , key , currentReplicas , desiredReplicas , minReplicas )
865
867
} else {
866
- desiredReplicas = a .normalizeDesiredReplicasWithBehaviors (hpa , key , currentReplicas , desiredReplicas , minReplicas )
868
+ desiredReplicas = a .normalizeDesiredReplicasWithBehaviors (ctx , hpa , key , currentReplicas , desiredReplicas , minReplicas )
867
869
}
868
870
rescale = desiredReplicas != currentReplicas
869
871
}
@@ -885,6 +887,7 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare
885
887
a .storeScaleEvent (hpa .Spec .Behavior , key , currentReplicas , desiredReplicas )
886
888
logger .Info ("Successfully rescaled" ,
887
889
"HPA" , klog .KObj (hpa ),
890
+ "scaleTarget" , reference ,
888
891
"currentReplicas" , currentReplicas ,
889
892
"desiredReplicas" , desiredReplicas ,
890
893
"reason" , rescaleReason )
@@ -942,7 +945,7 @@ func (a *HorizontalController) stabilizeRecommendation(key string, prenormalized
942
945
943
946
// normalizeDesiredReplicas takes the metrics desired replicas value and normalizes it based on the appropriate conditions (i.e. < maxReplicas, >
944
947
// minReplicas, etc...)
945
- func (a * HorizontalController ) normalizeDesiredReplicas (hpa * autoscalingv2.HorizontalPodAutoscaler , key string , currentReplicas int32 , prenormalizedDesiredReplicas int32 , minReplicas int32 ) int32 {
948
+ func (a * HorizontalController ) normalizeDesiredReplicas (ctx context. Context , hpa * autoscalingv2.HorizontalPodAutoscaler , key string , currentReplicas int32 , prenormalizedDesiredReplicas int32 , minReplicas int32 ) int32 {
946
949
stabilizedRecommendation := a .stabilizeRecommendation (key , prenormalizedDesiredReplicas )
947
950
if stabilizedRecommendation != prenormalizedDesiredReplicas {
948
951
setCondition (hpa , autoscalingv2 .AbleToScale , v1 .ConditionTrue , "ScaleDownStabilized" , "recent recommendations were higher than current one, applying the highest recent recommendation" )
@@ -957,6 +960,16 @@ func (a *HorizontalController) normalizeDesiredReplicas(hpa *autoscalingv2.Horiz
957
960
} else {
958
961
setCondition (hpa , autoscalingv2 .ScalingLimited , v1 .ConditionTrue , reason , "%s" , message )
959
962
}
963
+ logger := klog .FromContext (ctx )
964
+ reference := fmt .Sprintf ("%s/%s/%s" , hpa .Spec .ScaleTargetRef .Kind , hpa .Namespace , hpa .Spec .ScaleTargetRef .Name )
965
+ logger .V (4 ).Info ("Normalized desired replicas" ,
966
+ "scaleTarget" , reference ,
967
+ "currentReplicas" , currentReplicas ,
968
+ "desiredReplicas" , desiredReplicas ,
969
+ "minReplicas" , minReplicas ,
970
+ "stabilizedRecommendation" , stabilizedRecommendation ,
971
+ "prenormalizedDesiredReplicas" , prenormalizedDesiredReplicas ,
972
+ "reason" , reason )
960
973
961
974
return desiredReplicas
962
975
}
@@ -977,7 +990,7 @@ type NormalizationArg struct {
977
990
// 2. Apply the scale up/down limits from the hpaSpec.Behaviors (i.e. add no more than 4 pods)
978
991
// 3. Apply the constraints period (i.e. add no more than 4 pods per minute)
979
992
// 4. Apply the stabilization (i.e. add no more than 4 pods per minute, and pick the smallest recommendation during last 5 minutes)
980
- func (a * HorizontalController ) normalizeDesiredReplicasWithBehaviors (hpa * autoscalingv2.HorizontalPodAutoscaler , key string , currentReplicas , prenormalizedDesiredReplicas , minReplicas int32 ) int32 {
993
+ func (a * HorizontalController ) normalizeDesiredReplicasWithBehaviors (ctx context. Context , hpa * autoscalingv2.HorizontalPodAutoscaler , key string , currentReplicas , prenormalizedDesiredReplicas , minReplicas int32 ) int32 {
981
994
a .maybeInitScaleDownStabilizationWindow (hpa )
982
995
normalizationArg := NormalizationArg {
983
996
Key : key ,
@@ -987,21 +1000,42 @@ func (a *HorizontalController) normalizeDesiredReplicasWithBehaviors(hpa *autosc
987
1000
MaxReplicas : hpa .Spec .MaxReplicas ,
988
1001
CurrentReplicas : currentReplicas ,
989
1002
DesiredReplicas : prenormalizedDesiredReplicas }
990
- stabilizedRecommendation , reason , message := a .stabilizeRecommendationWithBehaviors (normalizationArg )
1003
+ stabilizedRecommendation , reason , message := a .stabilizeRecommendationWithBehaviors (ctx , normalizationArg )
991
1004
normalizationArg .DesiredReplicas = stabilizedRecommendation
992
1005
if stabilizedRecommendation != prenormalizedDesiredReplicas {
993
1006
// "ScaleUpStabilized" || "ScaleDownStabilized"
994
1007
setCondition (hpa , autoscalingv2 .AbleToScale , v1 .ConditionTrue , reason , "%s" , message )
995
1008
} else {
996
1009
setCondition (hpa , autoscalingv2 .AbleToScale , v1 .ConditionTrue , "ReadyForNewScale" , "recommended size matches current size" )
997
1010
}
998
- desiredReplicas , reason , message := a .convertDesiredReplicasWithBehaviorRate (normalizationArg )
1011
+
1012
+ logger := klog .FromContext (ctx )
1013
+ reference := fmt .Sprintf ("%s/%s/%s" , hpa .Spec .ScaleTargetRef .Kind , hpa .Namespace , hpa .Spec .ScaleTargetRef .Name )
1014
+ logger .V (4 ).Info ("Normalized desired replicas with behaviors - after stabilized recommendation" ,
1015
+ "scaleTarget" , reference ,
1016
+ "currentReplicas" , currentReplicas ,
1017
+ "minReplicas" , minReplicas ,
1018
+ "stabilizedRecommendation" , stabilizedRecommendation ,
1019
+ "prenormalizedDesiredReplicas" , prenormalizedDesiredReplicas ,
1020
+ "reason" , reason ,
1021
+ "message" , message )
1022
+
1023
+ desiredReplicas , reason , message := a .convertDesiredReplicasWithBehaviorRate (ctx , normalizationArg )
999
1024
if desiredReplicas == stabilizedRecommendation {
1000
1025
setCondition (hpa , autoscalingv2 .ScalingLimited , v1 .ConditionFalse , reason , "%s" , message )
1001
1026
} else {
1002
1027
setCondition (hpa , autoscalingv2 .ScalingLimited , v1 .ConditionTrue , reason , "%s" , message )
1003
1028
}
1004
1029
1030
+ logger .V (4 ).Info ("Normalized desired replicas with behaviors - after rated recommendation" ,
1031
+ "scaleTarget" , reference ,
1032
+ "currentReplicas" , currentReplicas ,
1033
+ "minReplicas" , minReplicas ,
1034
+ "stabilizedRecommendation" , stabilizedRecommendation ,
1035
+ "desiredReplicas" , desiredReplicas ,
1036
+ "reason" , reason ,
1037
+ "message" , message )
1038
+
1005
1039
return desiredReplicas
1006
1040
}
1007
1041
@@ -1090,7 +1124,7 @@ func (a *HorizontalController) storeScaleEvent(behavior *autoscalingv2.Horizonta
1090
1124
// stabilizeRecommendationWithBehaviors:
1091
1125
// - replaces old recommendation with the newest recommendation,
1092
1126
// - returns {max,min} of recommendations that are not older than constraints.Scale{Up,Down}.DelaySeconds
1093
- func (a * HorizontalController ) stabilizeRecommendationWithBehaviors (args NormalizationArg ) (int32 , string , string ) {
1127
+ func (a * HorizontalController ) stabilizeRecommendationWithBehaviors (ctx context. Context , args NormalizationArg ) (int32 , string , string ) {
1094
1128
now := time .Now ()
1095
1129
1096
1130
foundOldSample := false
@@ -1128,6 +1162,20 @@ func (a *HorizontalController) stabilizeRecommendationWithBehaviors(args Normali
1128
1162
if recommendation > downRecommendation {
1129
1163
recommendation = downRecommendation
1130
1164
}
1165
+ // Only keep the recommendations and ignore timestamp for logging.
1166
+ var recommendations []int32
1167
+ for _ , rec := range a .recommendations [args .Key ] {
1168
+ recommendations = append (recommendations , rec .recommendation )
1169
+ }
1170
+
1171
+ logger := klog .FromContext (ctx )
1172
+ logger .V (4 ).Info ("Stabilizing recommendation" ,
1173
+ "key" , args .Key ,
1174
+ "currentReplicas" , args .CurrentReplicas ,
1175
+ "desiredReplicas" , args .DesiredReplicas ,
1176
+ "upRecommendation" , upRecommendation ,
1177
+ "downRecommendation" , downRecommendation ,
1178
+ "recommendations" , recommendations )
1131
1179
1132
1180
// Record the unstabilized recommendation.
1133
1181
if foundOldSample {
@@ -1150,14 +1198,22 @@ func (a *HorizontalController) stabilizeRecommendationWithBehaviors(args Normali
1150
1198
1151
1199
// convertDesiredReplicasWithBehaviorRate performs the actual normalization, given the constraint rate
1152
1200
// It doesn't consider the stabilizationWindow, it is done separately
1153
- func (a * HorizontalController ) convertDesiredReplicasWithBehaviorRate (args NormalizationArg ) (int32 , string , string ) {
1201
+ func (a * HorizontalController ) convertDesiredReplicasWithBehaviorRate (ctx context. Context , args NormalizationArg ) (int32 , string , string ) {
1154
1202
var possibleLimitingReason , possibleLimitingMessage string
1203
+ logger := klog .FromContext (ctx )
1155
1204
1156
1205
if args .DesiredReplicas > args .CurrentReplicas {
1157
1206
a .scaleUpEventsLock .RLock ()
1158
1207
defer a .scaleUpEventsLock .RUnlock ()
1159
1208
a .scaleDownEventsLock .RLock ()
1160
1209
defer a .scaleDownEventsLock .RUnlock ()
1210
+ logger .V (4 ).Info ("Converting desired replicas with behavior rate - scale up" ,
1211
+ "key" , args .Key ,
1212
+ "currentReplicas" , args .CurrentReplicas ,
1213
+ "desiredReplicas" , args .DesiredReplicas ,
1214
+ "scaleUpEvents" , a .scaleUpEvents [args .Key ],
1215
+ "scaleDownEvents" , a .scaleDownEvents [args .Key ])
1216
+
1161
1217
scaleUpLimit := calculateScaleUpLimitWithScalingRules (args .CurrentReplicas , a .scaleUpEvents [args .Key ], a .scaleDownEvents [args .Key ], args .ScaleUpBehavior )
1162
1218
1163
1219
if scaleUpLimit < args .CurrentReplicas {
@@ -1181,6 +1237,13 @@ func (a *HorizontalController) convertDesiredReplicasWithBehaviorRate(args Norma
1181
1237
defer a .scaleUpEventsLock .RUnlock ()
1182
1238
a .scaleDownEventsLock .RLock ()
1183
1239
defer a .scaleDownEventsLock .RUnlock ()
1240
+ logger .V (4 ).Info ("Converting desired replicas with behavior rate - scale down" ,
1241
+ "key" , args .Key ,
1242
+ "currentReplicas" , args .CurrentReplicas ,
1243
+ "desiredReplicas" , args .DesiredReplicas ,
1244
+ "scaleUpEvents" , a .scaleUpEvents [args .Key ],
1245
+ "scaleDownEvents" , a .scaleDownEvents [args .Key ])
1246
+
1184
1247
scaleDownLimit := calculateScaleDownLimitWithBehaviors (args .CurrentReplicas , a .scaleUpEvents [args .Key ], a .scaleDownEvents [args .Key ], args .ScaleDownBehavior )
1185
1248
1186
1249
if scaleDownLimit > args .CurrentReplicas {
0 commit comments