Skip to content

Commit 61a1993

Browse files
authored
Merge pull request #2540 from leiwingqueen/fix-value-overflow
fix: big memory overflow
2 parents ca66c9d + c67f02f commit 61a1993

File tree

7 files changed

+126
-28
lines changed

7 files changed

+126
-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)
221221
}
222222
if metricTarget.AverageValue != nil {
223-
metricMap[average] = float64(metricTarget.AverageValue.MilliValue()) / 1000
223+
metricMap[average] = convertValueToFloat64(metricTarget.AverageValue)
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: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
324324
SanitizeLabelName(string(resourceName)),
325325
string(constant.UnitCore),
326326
},
327-
Value: float64(val.MilliValue()) / 1000,
327+
Value: convertValueToFloat64(&val),
328328
})
329329
case v1.ResourceStorage:
330330
fallthrough
@@ -336,15 +336,15 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
336336
SanitizeLabelName(string(resourceName)),
337337
string(constant.UnitByte),
338338
},
339-
Value: float64(val.MilliValue()) / 1000,
339+
Value: convertValueToFloat64(&val),
340340
})
341341
case v1.ResourcePods:
342342
ms = append(ms, &metric.Metric{
343343
LabelValues: []string{
344344
SanitizeLabelName(string(resourceName)),
345345
string(constant.UnitInteger),
346346
},
347-
Value: float64(val.MilliValue()) / 1000,
347+
Value: convertValueToFloat64(&val),
348348
})
349349
default:
350350
if isHugePageResourceName(resourceName) {
@@ -353,7 +353,7 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
353353
SanitizeLabelName(string(resourceName)),
354354
string(constant.UnitByte),
355355
},
356-
Value: float64(val.MilliValue()) / 1000,
356+
Value: convertValueToFloat64(&val),
357357
})
358358
}
359359
if isAttachableVolumeResourceName(resourceName) {
@@ -362,7 +362,7 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
362362
SanitizeLabelName(string(resourceName)),
363363
string(constant.UnitByte),
364364
},
365-
Value: float64(val.MilliValue()) / 1000,
365+
Value: convertValueToFloat64(&val),
366366
})
367367
}
368368
if isExtendedResourceName(resourceName) {
@@ -371,7 +371,7 @@ func createNodeStatusAllocatableFamilyGenerator() generator.FamilyGenerator {
371371
SanitizeLabelName(string(resourceName)),
372372
string(constant.UnitInteger),
373373
},
374-
Value: float64(val.MilliValue()) / 1000,
374+
Value: convertValueToFloat64(&val),
375375
})
376376
}
377377
}
@@ -407,7 +407,7 @@ func createNodeStatusCapacityFamilyGenerator() generator.FamilyGenerator {
407407
SanitizeLabelName(string(resourceName)),
408408
string(constant.UnitCore),
409409
},
410-
Value: float64(val.MilliValue()) / 1000,
410+
Value: convertValueToFloat64(&val),
411411
})
412412
case v1.ResourceStorage:
413413
fallthrough
@@ -419,15 +419,15 @@ func createNodeStatusCapacityFamilyGenerator() generator.FamilyGenerator {
419419
SanitizeLabelName(string(resourceName)),
420420
string(constant.UnitByte),
421421
},
422-
Value: float64(val.MilliValue()) / 1000,
422+
Value: convertValueToFloat64(&val),
423423
})
424424
case v1.ResourcePods:
425425
ms = append(ms, &metric.Metric{
426426
LabelValues: []string{
427427
SanitizeLabelName(string(resourceName)),
428428
string(constant.UnitInteger),
429429
},
430-
Value: float64(val.MilliValue()) / 1000,
430+
Value: convertValueToFloat64(&val),
431431
})
432432
default:
433433
if isHugePageResourceName(resourceName) {
@@ -436,7 +436,7 @@ func createNodeStatusCapacityFamilyGenerator() generator.FamilyGenerator {
436436
SanitizeLabelName(string(resourceName)),
437437
string(constant.UnitByte),
438438
},
439-
Value: float64(val.MilliValue()) / 1000,
439+
Value: convertValueToFloat64(&val),
440440
})
441441
}
442442
if isAttachableVolumeResourceName(resourceName) {
@@ -445,7 +445,7 @@ func createNodeStatusCapacityFamilyGenerator() generator.FamilyGenerator {
445445
SanitizeLabelName(string(resourceName)),
446446
string(constant.UnitByte),
447447
},
448-
Value: float64(val.MilliValue()) / 1000,
448+
Value: convertValueToFloat64(&val),
449449
})
450450
}
451451
if isExtendedResourceName(resourceName) {
@@ -454,7 +454,7 @@ func createNodeStatusCapacityFamilyGenerator() generator.FamilyGenerator {
454454
SanitizeLabelName(string(resourceName)),
455455
string(constant.UnitInteger),
456456
},
457-
Value: float64(val.MilliValue()) / 1000,
457+
Value: convertValueToFloat64(&val),
458458
})
459459
}
460460
}

internal/store/node_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,94 @@ func TestNodeStore(t *testing.T) {
298298
`,
299299
MetricNames: []string{"kube_node_status_addresses"},
300300
},
301+
// memory overflow test
302+
{
303+
Obj: &v1.Node{
304+
ObjectMeta: metav1.ObjectMeta{
305+
Name: "127.0.0.1",
306+
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
307+
Labels: map[string]string{
308+
"node-role.kubernetes.io/master": "",
309+
},
310+
},
311+
Spec: v1.NodeSpec{
312+
Unschedulable: true,
313+
ProviderID: "provider://i-randomidentifier",
314+
PodCIDR: "172.24.10.0/24",
315+
},
316+
Status: v1.NodeStatus{
317+
NodeInfo: v1.NodeSystemInfo{
318+
KernelVersion: "kernel",
319+
KubeletVersion: "kubelet",
320+
KubeProxyVersion: "kubeproxy",
321+
OSImage: "osimage",
322+
ContainerRuntimeVersion: "rkt",
323+
SystemUUID: "6a934e21-5207-4a84-baea-3a952d926c80",
324+
},
325+
Addresses: []v1.NodeAddress{
326+
{Type: "InternalIP", Address: "1.2.3.4"},
327+
},
328+
Capacity: v1.ResourceList{
329+
v1.ResourceCPU: resource.MustParse("4.3"),
330+
// overflow test
331+
v1.ResourceMemory: resource.MustParse("10000T"),
332+
v1.ResourcePods: resource.MustParse("1000"),
333+
v1.ResourceStorage: resource.MustParse("3G"),
334+
v1.ResourceEphemeralStorage: resource.MustParse("4G"),
335+
v1.ResourceName("nvidia.com/gpu"): resource.MustParse("4"),
336+
},
337+
Allocatable: v1.ResourceList{
338+
v1.ResourceCPU: resource.MustParse("3"),
339+
v1.ResourceMemory: resource.MustParse("1G"),
340+
v1.ResourcePods: resource.MustParse("555"),
341+
v1.ResourceStorage: resource.MustParse("2G"),
342+
v1.ResourceEphemeralStorage: resource.MustParse("3G"),
343+
v1.ResourceName("nvidia.com/gpu"): resource.MustParse("1"),
344+
},
345+
},
346+
},
347+
Want: `
348+
# HELP kube_node_created [STABLE] Unix creation timestamp
349+
# HELP kube_node_info [STABLE] Information about a cluster node.
350+
# HELP kube_node_labels [STABLE] Kubernetes labels converted to Prometheus labels.
351+
# HELP kube_node_role The role of a cluster node.
352+
# HELP kube_node_spec_unschedulable [STABLE] Whether a node can schedule new pods.
353+
# HELP kube_node_status_allocatable [STABLE] The allocatable for different resources of a node that are available for scheduling.
354+
# HELP kube_node_status_capacity [STABLE] The capacity for different resources of a node.
355+
# TYPE kube_node_created gauge
356+
# TYPE kube_node_info gauge
357+
# TYPE kube_node_labels gauge
358+
# TYPE kube_node_role gauge
359+
# TYPE kube_node_spec_unschedulable gauge
360+
# TYPE kube_node_status_allocatable gauge
361+
# TYPE kube_node_status_capacity gauge
362+
kube_node_created{node="127.0.0.1"} 1.5e+09
363+
kube_node_info{container_runtime_version="rkt",kernel_version="kernel",kubelet_version="kubelet",kubeproxy_version="deprecated",node="127.0.0.1",os_image="osimage",pod_cidr="172.24.10.0/24",provider_id="provider://i-randomidentifier",internal_ip="1.2.3.4",system_uuid="6a934e21-5207-4a84-baea-3a952d926c80"} 1
364+
kube_node_role{node="127.0.0.1",role="master"} 1
365+
kube_node_spec_unschedulable{node="127.0.0.1"} 1
366+
kube_node_status_allocatable{node="127.0.0.1",resource="cpu",unit="core"} 3
367+
kube_node_status_allocatable{node="127.0.0.1",resource="ephemeral_storage",unit="byte"} 3e+09
368+
kube_node_status_allocatable{node="127.0.0.1",resource="memory",unit="byte"} 1e+09
369+
kube_node_status_allocatable{node="127.0.0.1",resource="nvidia_com_gpu",unit="integer"} 1
370+
kube_node_status_allocatable{node="127.0.0.1",resource="pods",unit="integer"} 555
371+
kube_node_status_allocatable{node="127.0.0.1",resource="storage",unit="byte"} 2e+09
372+
kube_node_status_capacity{node="127.0.0.1",resource="cpu",unit="core"} 4.3
373+
kube_node_status_capacity{node="127.0.0.1",resource="ephemeral_storage",unit="byte"} 4e+09
374+
kube_node_status_capacity{node="127.0.0.1",resource="memory",unit="byte"} 1e+16
375+
kube_node_status_capacity{node="127.0.0.1",resource="nvidia_com_gpu",unit="integer"} 4
376+
kube_node_status_capacity{node="127.0.0.1",resource="pods",unit="integer"} 1000
377+
kube_node_status_capacity{node="127.0.0.1",resource="storage",unit="byte"} 3e+09
378+
`,
379+
MetricNames: []string{
380+
"kube_node_status_capacity",
381+
"kube_node_status_allocatable",
382+
"kube_node_spec_unschedulable",
383+
"kube_node_labels",
384+
"kube_node_role",
385+
"kube_node_info",
386+
"kube_node_created",
387+
},
388+
},
301389
}
302390
for i, c := range cases {
303391
c.Func = generator.ComposeMetricGenFuncs(nodeMetricFamilies(nil, nil))

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"strconv"
2424
"strings"
2525

26+
"k8s.io/apimachinery/pkg/api/resource"
27+
2628
v1 "k8s.io/api/core/v1"
2729
"k8s.io/apimachinery/pkg/util/validation"
2830

@@ -215,3 +217,11 @@ func mergeKeyValues(keyValues ...[]string) (keys, values []string) {
215217

216218
return keys, values
217219
}
220+
221+
// convertValueToFloat64 converts a resource.Quantity to a float64 and checks for a possible overflow in the value.
222+
func convertValueToFloat64(q *resource.Quantity) float64 {
223+
if q.Value() > resource.MaxMilliValue {
224+
return float64(q.Value())
225+
}
226+
return float64(q.MilliValue()) / 1000
227+
}

0 commit comments

Comments
 (0)