Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixing k8s nodes and pods parsing error #9581

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions plugins/inputs/kube_inventory/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@ func (ki *KubernetesInventory) gatherNode(n corev1.Node, acc telegraf.Accumulato
}

for resourceName, val := range n.Status.Capacity {

switch resourceName {
case "cpu":
fields["capacity_cpu_cores"] = convertQuantity(string(val.Format), 1)
fields["capacity_millicpu_cores"] = convertQuantity(string(val.Format), 1000)
fields["capacity_cpu_cores"] = convertQuantity(val.String(), 1)
fields["capacity_millicpu_cores"] = convertQuantity(val.String(), 1000)
case "memory":
fields["capacity_memory_bytes"] = convertQuantity(string(val.Format), 1)
fields["capacity_memory_bytes"] = convertQuantity(val.String(), 1)
case "pods":
fields["capacity_pods"] = atoi(string(val.Format))
fields["capacity_pods"] = atoi(val.String())
}
}

for resourceName, val := range n.Status.Allocatable {
switch resourceName {
case "cpu":
fields["allocatable_cpu_cores"] = convertQuantity(string(val.Format), 1)
fields["allocatable_millicpu_cores"] = convertQuantity(string(val.Format), 1000)
fields["allocatable_cpu_cores"] = convertQuantity(val.String(), 1)
fields["allocatable_millicpu_cores"] = convertQuantity(val.String(), 1000)
case "memory":
fields["allocatable_memory_bytes"] = convertQuantity(string(val.Format), 1)
fields["allocatable_memory_bytes"] = convertQuantity(val.String(), 1)
case "pods":
fields["allocatable_pods"] = atoi(string(val.Format))
fields["allocatable_pods"] = atoi(val.String())
}
}

Expand Down
24 changes: 12 additions & 12 deletions plugins/inputs/kube_inventory/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ func TestNode(t *testing.T) {
},
Phase: "Running",
Capacity: corev1.ResourceList{
"cpu": resource.Quantity{Format: "16"},
"ephemeral_storage_bytes": resource.Quantity{Format: "49536401408"},
"hugepages_1Gi_bytes": resource.Quantity{Format: "0"},
"hugepages_2Mi_bytes": resource.Quantity{Format: "0"},
"memory": resource.Quantity{Format: "125817904Ki"},
"pods": resource.Quantity{Format: "110"},
"cpu": resource.MustParse("16"),
"ephemeral_storage_bytes": resource.MustParse("49536401408"),
"hugepages_1Gi_bytes": resource.MustParse("0"),
"hugepages_2Mi_bytes": resource.MustParse("0"),
"memory": resource.MustParse("125817904Ki"),
"pods": resource.MustParse("110"),
},
Allocatable: corev1.ResourceList{
"cpu": resource.Quantity{Format: "1000m"},
"ephemeral_storage_bytes": resource.Quantity{Format: "44582761194"},
"hugepages_1Gi_bytes": resource.Quantity{Format: "0"},
"hugepages_2Mi_bytes": resource.Quantity{Format: "0"},
"memory": resource.Quantity{Format: "125715504Ki"},
"pods": resource.Quantity{Format: "110"},
"cpu": resource.MustParse("1000m"),
"ephemeral_storage_bytes": resource.MustParse("44582761194"),
"hugepages_1Gi_bytes": resource.MustParse("0"),
"hugepages_2Mi_bytes": resource.MustParse("0"),
"memory": resource.MustParse("125715504Ki"),
"pods": resource.MustParse("110"),
},
Conditions: []corev1.NodeCondition{
{Type: "Ready", Status: "true", LastTransitionTime: metav1.Time{Time: now}},
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/kube_inventory/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ func gatherPodContainer(ki *KubernetesInventory, p corev1.Pod, cs corev1.Contain
for resourceName, val := range req {
switch resourceName {
case "cpu":
fields["resource_requests_millicpu_units"] = convertQuantity(string(val.Format), 1000)
fields["resource_requests_millicpu_units"] = convertQuantity(val.String(), 1000)
case "memory":
fields["resource_requests_memory_bytes"] = convertQuantity(string(val.Format), 1)
fields["resource_requests_memory_bytes"] = convertQuantity(val.String(), 1)
}
}
for resourceName, val := range lim {
switch resourceName {
case "cpu":
fields["resource_limits_millicpu_units"] = convertQuantity(string(val.Format), 1000)
fields["resource_limits_millicpu_units"] = convertQuantity(val.String(), 1000)
case "memory":
fields["resource_limits_memory_bytes"] = convertQuantity(string(val.Format), 1)
fields["resource_limits_memory_bytes"] = convertQuantity(val.String(), 1)
}
}

Expand Down
24 changes: 12 additions & 12 deletions plugins/inputs/kube_inventory/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func TestPod(t *testing.T) {
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
Requests: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
},
},
Expand All @@ -78,10 +78,10 @@ func TestPod(t *testing.T) {
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
Requests: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
},
},
Expand All @@ -96,10 +96,10 @@ func TestPod(t *testing.T) {
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
Requests: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
},
},
Expand Down Expand Up @@ -335,10 +335,10 @@ func TestPodSelectorFilter(t *testing.T) {
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
Requests: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
},
},
Expand Down Expand Up @@ -582,10 +582,10 @@ func TestPodPendingContainers(t *testing.T) {
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
Requests: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
},
},
Expand All @@ -600,10 +600,10 @@ func TestPodPendingContainers(t *testing.T) {
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
Requests: corev1.ResourceList{
"cpu": resource.Quantity{Format: "100m"},
"cpu": resource.MustParse("100m"),
},
},
},
Expand Down