diff --git a/plugins/inputs/kube_inventory/node.go b/plugins/inputs/kube_inventory/node.go index 16e8d5b2a017f..3c7c9cb38e160 100644 --- a/plugins/inputs/kube_inventory/node.go +++ b/plugins/inputs/kube_inventory/node.go @@ -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()) } } diff --git a/plugins/inputs/kube_inventory/node_test.go b/plugins/inputs/kube_inventory/node_test.go index 560e662bcdd5c..02f330a7d1a2f 100644 --- a/plugins/inputs/kube_inventory/node_test.go +++ b/plugins/inputs/kube_inventory/node_test.go @@ -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}}, diff --git a/plugins/inputs/kube_inventory/pod.go b/plugins/inputs/kube_inventory/pod.go index bbdfb3a699b5e..ebd3ae43a7d4c 100644 --- a/plugins/inputs/kube_inventory/pod.go +++ b/plugins/inputs/kube_inventory/pod.go @@ -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) } } diff --git a/plugins/inputs/kube_inventory/pod_test.go b/plugins/inputs/kube_inventory/pod_test.go index 777e15a017c7c..31600b1010d28 100644 --- a/plugins/inputs/kube_inventory/pod_test.go +++ b/plugins/inputs/kube_inventory/pod_test.go @@ -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"), }, }, }, @@ -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"), }, }, }, @@ -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"), }, }, }, @@ -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"), }, }, }, @@ -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"), }, }, }, @@ -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"), }, }, },