Skip to content

Commit 1659758

Browse files
fix: Fixing k8s nodes and pods parsing error (#9581)
1 parent bfcd0f6 commit 1659758

4 files changed

Lines changed: 37 additions & 36 deletions

File tree

plugins/inputs/kube_inventory/node.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,27 @@ func (ki *KubernetesInventory) gatherNode(n corev1.Node, acc telegraf.Accumulato
2626
}
2727

2828
for resourceName, val := range n.Status.Capacity {
29+
2930
switch resourceName {
3031
case "cpu":
31-
fields["capacity_cpu_cores"] = convertQuantity(string(val.Format), 1)
32-
fields["capacity_millicpu_cores"] = convertQuantity(string(val.Format), 1000)
32+
fields["capacity_cpu_cores"] = convertQuantity(val.String(), 1)
33+
fields["capacity_millicpu_cores"] = convertQuantity(val.String(), 1000)
3334
case "memory":
34-
fields["capacity_memory_bytes"] = convertQuantity(string(val.Format), 1)
35+
fields["capacity_memory_bytes"] = convertQuantity(val.String(), 1)
3536
case "pods":
36-
fields["capacity_pods"] = atoi(string(val.Format))
37+
fields["capacity_pods"] = atoi(val.String())
3738
}
3839
}
3940

4041
for resourceName, val := range n.Status.Allocatable {
4142
switch resourceName {
4243
case "cpu":
43-
fields["allocatable_cpu_cores"] = convertQuantity(string(val.Format), 1)
44-
fields["allocatable_millicpu_cores"] = convertQuantity(string(val.Format), 1000)
44+
fields["allocatable_cpu_cores"] = convertQuantity(val.String(), 1)
45+
fields["allocatable_millicpu_cores"] = convertQuantity(val.String(), 1000)
4546
case "memory":
46-
fields["allocatable_memory_bytes"] = convertQuantity(string(val.Format), 1)
47+
fields["allocatable_memory_bytes"] = convertQuantity(val.String(), 1)
4748
case "pods":
48-
fields["allocatable_pods"] = atoi(string(val.Format))
49+
fields["allocatable_pods"] = atoi(val.String())
4950
}
5051
}
5152

plugins/inputs/kube_inventory/node_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ func TestNode(t *testing.T) {
5050
},
5151
Phase: "Running",
5252
Capacity: corev1.ResourceList{
53-
"cpu": resource.Quantity{Format: "16"},
54-
"ephemeral_storage_bytes": resource.Quantity{Format: "49536401408"},
55-
"hugepages_1Gi_bytes": resource.Quantity{Format: "0"},
56-
"hugepages_2Mi_bytes": resource.Quantity{Format: "0"},
57-
"memory": resource.Quantity{Format: "125817904Ki"},
58-
"pods": resource.Quantity{Format: "110"},
53+
"cpu": resource.MustParse("16"),
54+
"ephemeral_storage_bytes": resource.MustParse("49536401408"),
55+
"hugepages_1Gi_bytes": resource.MustParse("0"),
56+
"hugepages_2Mi_bytes": resource.MustParse("0"),
57+
"memory": resource.MustParse("125817904Ki"),
58+
"pods": resource.MustParse("110"),
5959
},
6060
Allocatable: corev1.ResourceList{
61-
"cpu": resource.Quantity{Format: "1000m"},
62-
"ephemeral_storage_bytes": resource.Quantity{Format: "44582761194"},
63-
"hugepages_1Gi_bytes": resource.Quantity{Format: "0"},
64-
"hugepages_2Mi_bytes": resource.Quantity{Format: "0"},
65-
"memory": resource.Quantity{Format: "125715504Ki"},
66-
"pods": resource.Quantity{Format: "110"},
61+
"cpu": resource.MustParse("1000m"),
62+
"ephemeral_storage_bytes": resource.MustParse("44582761194"),
63+
"hugepages_1Gi_bytes": resource.MustParse("0"),
64+
"hugepages_2Mi_bytes": resource.MustParse("0"),
65+
"memory": resource.MustParse("125715504Ki"),
66+
"pods": resource.MustParse("110"),
6767
},
6868
Conditions: []corev1.NodeCondition{
6969
{Type: "Ready", Status: "true", LastTransitionTime: metav1.Time{Time: now}},

plugins/inputs/kube_inventory/pod.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ func gatherPodContainer(ki *KubernetesInventory, p corev1.Pod, cs corev1.Contain
102102
for resourceName, val := range req {
103103
switch resourceName {
104104
case "cpu":
105-
fields["resource_requests_millicpu_units"] = convertQuantity(string(val.Format), 1000)
105+
fields["resource_requests_millicpu_units"] = convertQuantity(val.String(), 1000)
106106
case "memory":
107-
fields["resource_requests_memory_bytes"] = convertQuantity(string(val.Format), 1)
107+
fields["resource_requests_memory_bytes"] = convertQuantity(val.String(), 1)
108108
}
109109
}
110110
for resourceName, val := range lim {
111111
switch resourceName {
112112
case "cpu":
113-
fields["resource_limits_millicpu_units"] = convertQuantity(string(val.Format), 1000)
113+
fields["resource_limits_millicpu_units"] = convertQuantity(val.String(), 1000)
114114
case "memory":
115-
fields["resource_limits_memory_bytes"] = convertQuantity(string(val.Format), 1)
115+
fields["resource_limits_memory_bytes"] = convertQuantity(val.String(), 1)
116116
}
117117
}
118118

plugins/inputs/kube_inventory/pod_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func TestPod(t *testing.T) {
6060
},
6161
Resources: corev1.ResourceRequirements{
6262
Limits: corev1.ResourceList{
63-
"cpu": resource.Quantity{Format: "100m"},
63+
"cpu": resource.MustParse("100m"),
6464
},
6565
Requests: corev1.ResourceList{
66-
"cpu": resource.Quantity{Format: "100m"},
66+
"cpu": resource.MustParse("100m"),
6767
},
6868
},
6969
},
@@ -78,10 +78,10 @@ func TestPod(t *testing.T) {
7878
},
7979
Resources: corev1.ResourceRequirements{
8080
Limits: corev1.ResourceList{
81-
"cpu": resource.Quantity{Format: "100m"},
81+
"cpu": resource.MustParse("100m"),
8282
},
8383
Requests: corev1.ResourceList{
84-
"cpu": resource.Quantity{Format: "100m"},
84+
"cpu": resource.MustParse("100m"),
8585
},
8686
},
8787
},
@@ -96,10 +96,10 @@ func TestPod(t *testing.T) {
9696
},
9797
Resources: corev1.ResourceRequirements{
9898
Limits: corev1.ResourceList{
99-
"cpu": resource.Quantity{Format: "100m"},
99+
"cpu": resource.MustParse("100m"),
100100
},
101101
Requests: corev1.ResourceList{
102-
"cpu": resource.Quantity{Format: "100m"},
102+
"cpu": resource.MustParse("100m"),
103103
},
104104
},
105105
},
@@ -335,10 +335,10 @@ func TestPodSelectorFilter(t *testing.T) {
335335
},
336336
Resources: corev1.ResourceRequirements{
337337
Limits: corev1.ResourceList{
338-
"cpu": resource.Quantity{Format: "100m"},
338+
"cpu": resource.MustParse("100m"),
339339
},
340340
Requests: corev1.ResourceList{
341-
"cpu": resource.Quantity{Format: "100m"},
341+
"cpu": resource.MustParse("100m"),
342342
},
343343
},
344344
},
@@ -582,10 +582,10 @@ func TestPodPendingContainers(t *testing.T) {
582582
},
583583
Resources: corev1.ResourceRequirements{
584584
Limits: corev1.ResourceList{
585-
"cpu": resource.Quantity{Format: "100m"},
585+
"cpu": resource.MustParse("100m"),
586586
},
587587
Requests: corev1.ResourceList{
588-
"cpu": resource.Quantity{Format: "100m"},
588+
"cpu": resource.MustParse("100m"),
589589
},
590590
},
591591
},
@@ -600,10 +600,10 @@ func TestPodPendingContainers(t *testing.T) {
600600
},
601601
Resources: corev1.ResourceRequirements{
602602
Limits: corev1.ResourceList{
603-
"cpu": resource.Quantity{Format: "100m"},
603+
"cpu": resource.MustParse("100m"),
604604
},
605605
Requests: corev1.ResourceList{
606-
"cpu": resource.Quantity{Format: "100m"},
606+
"cpu": resource.MustParse("100m"),
607607
},
608608
},
609609
},

0 commit comments

Comments
 (0)