mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-05-28 04:04:39 -04:00
hpa: prevent integer overflow in external metrics sum
Signed-off-by: Omer Aplatony <omerap12@gmail.com>
This commit is contained in:
parent
947a8ebfd1
commit
fbd33bd6b3
2 changed files with 24 additions and 0 deletions
|
|
@ -360,8 +360,14 @@ func (c *ReplicaCalculator) GetExternalMetricReplicas(currentReplicas int32, tar
|
|||
if err != nil {
|
||||
return 0, 0, time.Time{}, fmt.Errorf("unable to get external metric %s/%s/%+v: %s", namespace, metricName, metricSelector, err)
|
||||
}
|
||||
|
||||
usage = 0
|
||||
for _, val := range metrics {
|
||||
// Cap at MaxInt64 for positive overflow
|
||||
if val > 0 && usage > math.MaxInt64-val {
|
||||
usage = math.MaxInt64
|
||||
break
|
||||
}
|
||||
usage = usage + val
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2477,3 +2477,21 @@ func TestCalculatePodRequestsFromContainers_NonExistentContainer(t *testing.T) {
|
|||
assert.Equal(t, expectedErr, err.Error(), "error message should match expected format")
|
||||
assert.Equal(t, int64(0), request, "request should be 0 when container does not exist")
|
||||
}
|
||||
|
||||
func TestReplicaCalcExternalMetricUsageOverflow(t *testing.T) {
|
||||
tc := replicaCalcTestCase{
|
||||
currentReplicas: 3,
|
||||
expectedReplicas: 3,
|
||||
metric: &metricInfo{
|
||||
name: "qps",
|
||||
// Two values that when added together will overflow int64
|
||||
levels: []int64{math.MaxInt64/2 + 1, math.MaxInt64/2 + 1},
|
||||
targetUsage: math.MaxInt64, // Set high target
|
||||
metricType: externalMetric,
|
||||
selector: &metav1.LabelSelector{MatchLabels: map[string]string{"label": "value"}},
|
||||
expectedUsage: math.MaxInt64, // expect capped value
|
||||
|
||||
},
|
||||
}
|
||||
tc.runTest(t)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue