Skip to content

Commit f94ef65

Browse files
committed
fix overflow in other occasion
1 parent e5eb2ed commit f94ef65

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

internal/store/horizontalpodautoscaler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ func createHPASpecTargetMetric() generator.FamilyGenerator {
217217
}
218218

219219
if metricTarget.Value != nil {
220-
metricMap[value] = float64(metricTarget.Value.MilliValue()) / 1000
220+
metricMap[value] = convertValueToFloat64(metricTarget.Value) / 1000
221221
}
222222
if metricTarget.AverageValue != nil {
223-
metricMap[average] = float64(metricTarget.AverageValue.MilliValue()) / 1000
223+
metricMap[average] = convertValueToFloat64(metricTarget.AverageValue) / 1000
224224
}
225225
if metricTarget.AverageUtilization != nil {
226226
metricMap[utilization] = float64(*metricTarget.AverageUtilization)
@@ -276,10 +276,10 @@ func createHPAStatusTargetMetric() generator.FamilyGenerator {
276276
}
277277

278278
if currentMetric.Value != nil {
279-
metricMap[value] = float64(currentMetric.Value.MilliValue()) / 1000
279+
metricMap[value] = convertValueToFloat64(currentMetric.Value)
280280
}
281281
if currentMetric.AverageValue != nil {
282-
metricMap[average] = float64(currentMetric.AverageValue.MilliValue()) / 1000
282+
metricMap[average] = convertValueToFloat64(currentMetric.AverageValue)
283283
}
284284
if currentMetric.AverageUtilization != nil {
285285
metricMap[utilization] = float64(*currentMetric.AverageUtilization)

internal/store/limitrange.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,35 @@ var (
4949
for resource, min := range rawLimitRange.Min {
5050
ms = append(ms, &metric.Metric{
5151
LabelValues: []string{string(resource), string(rawLimitRange.Type), "min"},
52-
Value: float64(min.MilliValue()) / 1000,
52+
Value: convertValueToFloat64(&min),
5353
})
5454
}
5555

5656
for resource, max := range rawLimitRange.Max {
5757
ms = append(ms, &metric.Metric{
5858
LabelValues: []string{string(resource), string(rawLimitRange.Type), "max"},
59-
Value: float64(max.MilliValue()) / 1000,
59+
Value: convertValueToFloat64(&max),
6060
})
6161
}
6262

6363
for resource, df := range rawLimitRange.Default {
6464
ms = append(ms, &metric.Metric{
6565
LabelValues: []string{string(resource), string(rawLimitRange.Type), "default"},
66-
Value: float64(df.MilliValue()) / 1000,
66+
Value: convertValueToFloat64(&df),
6767
})
6868
}
6969

7070
for resource, dfR := range rawLimitRange.DefaultRequest {
7171
ms = append(ms, &metric.Metric{
7272
LabelValues: []string{string(resource), string(rawLimitRange.Type), "defaultRequest"},
73-
Value: float64(dfR.MilliValue()) / 1000,
73+
Value: convertValueToFloat64(&dfR),
7474
})
7575
}
7676

7777
for resource, mLR := range rawLimitRange.MaxLimitRequestRatio {
7878
ms = append(ms, &metric.Metric{
7979
LabelValues: []string{string(resource), string(rawLimitRange.Type), "maxLimitRequestRatio"},
80-
Value: float64(mLR.MilliValue()) / 1000,
80+
Value: convertValueToFloat64(&mLR),
8181
})
8282
}
8383
}

internal/store/node.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"context"
2121
"strings"
2222

23-
"k8s.io/apimachinery/pkg/api/resource"
24-
2523
basemetrics "k8s.io/component-base/metrics"
2624

2725
"k8s.io/kube-state-metrics/v2/pkg/constant"
@@ -326,7 +324,7 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
326324
SanitizeLabelName(string(resourceName)),
327325
string(constant.UnitCore),
328326
},
329-
Value: float64(val.MilliValue()) / 1000,
327+
Value: convertValueToFloat64(&val),
330328
})
331329
case v1.ResourceStorage:
332330
fallthrough
@@ -338,15 +336,15 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
338336
SanitizeLabelName(string(resourceName)),
339337
string(constant.UnitByte),
340338
},
341-
Value: float64(val.MilliValue()) / 1000,
339+
Value: convertValueToFloat64(&val),
342340
})
343341
case v1.ResourcePods:
344342
ms = append(ms, &metric.Metric{
345343
LabelValues: []string{
346344
SanitizeLabelName(string(resourceName)),
347345
string(constant.UnitInteger),
348346
},
349-
Value: float64(val.MilliValue()) / 1000,
347+
Value: convertValueToFloat64(&val),
350348
})
351349
default:
352350
if isHugePageResourceName(resourceName) {
@@ -532,10 +530,3 @@ func createNodeListWatch(kubeClient clientset.Interface, _ string, _ string) cac
532530
},
533531
}
534532
}
535-
536-
func convertValueToFloat64(q *resource.Quantity) float64 {
537-
if q.Value() > resource.MaxMilliValue {
538-
return float64(q.Value())
539-
}
540-
return float64(q.MilliValue()) / 1000
541-
}

internal/store/pod.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func createPodContainerResourceLimitsFamilyGenerator() generator.FamilyGenerator
182182
case v1.ResourceCPU:
183183
ms = append(ms, &metric.Metric{
184184
LabelValues: []string{c.Name, p.Spec.NodeName, SanitizeLabelName(string(resourceName)), string(constant.UnitCore)},
185-
Value: float64(val.MilliValue()) / 1000,
185+
Value: convertValueToFloat64(&val),
186186
})
187187
case v1.ResourceStorage:
188188
fallthrough
@@ -246,7 +246,7 @@ func createPodContainerResourceRequestsFamilyGenerator() generator.FamilyGenerat
246246
case v1.ResourceCPU:
247247
ms = append(ms, &metric.Metric{
248248
LabelValues: []string{c.Name, p.Spec.NodeName, SanitizeLabelName(string(resourceName)), string(constant.UnitCore)},
249-
Value: float64(val.MilliValue()) / 1000,
249+
Value: convertValueToFloat64(&val),
250250
})
251251
case v1.ResourceStorage:
252252
fallthrough
@@ -749,7 +749,7 @@ func createPodInitContainerResourceLimitsFamilyGenerator() generator.FamilyGener
749749
case v1.ResourceCPU:
750750
ms = append(ms, &metric.Metric{
751751
LabelValues: []string{c.Name, p.Spec.NodeName, SanitizeLabelName(string(resourceName)), string(constant.UnitCore)},
752-
Value: float64(val.MilliValue()) / 1000,
752+
Value: convertValueToFloat64(&val),
753753
})
754754
case v1.ResourceStorage:
755755
fallthrough
@@ -813,7 +813,7 @@ func createPodInitContainerResourceRequestsFamilyGenerator() generator.FamilyGen
813813
case v1.ResourceCPU:
814814
ms = append(ms, &metric.Metric{
815815
LabelValues: []string{c.Name, p.Spec.NodeName, SanitizeLabelName(string(resourceName)), string(constant.UnitCore)},
816-
Value: float64(val.MilliValue()) / 1000,
816+
Value: convertValueToFloat64(&val),
817817
})
818818
case v1.ResourceStorage:
819819
fallthrough
@@ -1122,7 +1122,7 @@ func createPodOverheadCPUCoresFamilyGenerator() generator.FamilyGenerator {
11221122
for resourceName, val := range p.Spec.Overhead {
11231123
if resourceName == v1.ResourceCPU {
11241124
ms = append(ms, &metric.Metric{
1125-
Value: float64(val.MilliValue()) / 1000,
1125+
Value: convertValueToFloat64(&val),
11261126
})
11271127
}
11281128
}

internal/store/resourcequota.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ func resourceQuotaMetricFamilies(allowAnnotationsList, allowLabelsList []string)
7474
for res, qty := range r.Status.Hard {
7575
ms = append(ms, &metric.Metric{
7676
LabelValues: []string{string(res), "hard"},
77-
Value: float64(qty.MilliValue()) / 1000,
77+
Value: convertValueToFloat64(&qty),
7878
})
7979
}
8080
for res, qty := range r.Status.Used {
8181
ms = append(ms, &metric.Metric{
8282
LabelValues: []string{string(res), "used"},
83-
Value: float64(qty.MilliValue()) / 1000,
83+
Value: convertValueToFloat64(&qty),
8484
})
8585
}
8686

internal/store/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package store
1818

1919
import (
2020
"fmt"
21+
"k8s.io/apimachinery/pkg/api/resource"
2122
"regexp"
2223
"sort"
2324
"strconv"
@@ -215,3 +216,11 @@ func mergeKeyValues(keyValues ...[]string) (keys, values []string) {
215216

216217
return keys, values
217218
}
219+
220+
// convertValueToFloat64 converts a resource.Quantity to a float64.
221+
func convertValueToFloat64(q *resource.Quantity) float64 {
222+
if q.Value() > resource.MaxMilliValue {
223+
return float64(q.Value())
224+
}
225+
return float64(q.MilliValue()) / 1000
226+
}

0 commit comments

Comments
 (0)