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