From 8497b03948b654df3d35f6f0242d9616eb4c0f56 Mon Sep 17 00:00:00 2001 From: Darren Foo Date: Tue, 5 Dec 2023 11:06:42 -0800 Subject: [PATCH] update internal_ip and external_ip node label for multi-value --- internal/store/node.go | 35 ++++++++++++++++++++++++----------- internal/store/node_test.go | 4 +++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/internal/store/node.go b/internal/store/node.go index 5dd1f28c41..b3cee9a366 100644 --- a/internal/store/node.go +++ b/internal/store/node.go @@ -134,23 +134,36 @@ func createNodeInfoFamilyGenerator() generator.FamilyGenerator { n.Status.NodeInfo.SystemUUID, } - internalIP := "" + internalIPs := []string{} + externalIPs := []string{} for _, address := range n.Status.Addresses { - if address.Type == "InternalIP" { - internalIP = address.Address + switch address.Type { + case "InternalIP": + internalIPs = append(internalIPs, address.Address) + case "ExternalIP": + externalIPs = append(externalIPs, address.Address) } } - labelKeys = append(labelKeys, "internal_ip") - labelValues = append(labelValues, internalIP) - externalIP := "" - for _, address := range n.Status.Addresses { - if address.Type == "ExternalIP" { - externalIP = address.Address + if len(internalIPs) > 0 { + for _, ip := range internalIPs { + labelKeys = append(labelKeys, "internal_ip") + labelValues = append(labelValues, ip) + } + } else { + labelKeys = append(labelKeys, "internal_ip") + labelValues = append(labelValues, "") + } + + if len(externalIPs) > 0 { + for _, ip := range externalIPs { + labelKeys = append(labelKeys, "external_ip") + labelValues = append(labelValues, ip) } + } else { + labelKeys = append(labelKeys, "external_ip") + labelValues = append(labelValues, "") } - labelKeys = append(labelKeys, "external_ip") - labelValues = append(labelValues, externalIP) return &metric.Family{ Metrics: []*metric.Metric{ diff --git a/internal/store/node_test.go b/internal/store/node_test.go index d25c2ec9af..ab2d0b014c 100644 --- a/internal/store/node_test.go +++ b/internal/store/node_test.go @@ -106,7 +106,9 @@ func TestNodeStore(t *testing.T) { }, Addresses: []v1.NodeAddress{ {Type: "InternalIP", Address: "1.2.3.4"}, + {Type: "InternalIP", Address: "fc00::"}, {Type: "ExternalIP", Address: "5.6.7.8"}, + {Type: "ExternalIP", Address: "2001:db8::"}, }, Capacity: v1.ResourceList{ v1.ResourceCPU: resource.MustParse("4.3"), @@ -142,7 +144,7 @@ func TestNodeStore(t *testing.T) { # TYPE kube_node_status_allocatable gauge # TYPE kube_node_status_capacity gauge kube_node_created{node="127.0.0.1"} 1.5e+09 - kube_node_info{container_runtime_version="rkt",kernel_version="kernel",kubelet_version="kubelet",kubeproxy_version="kubeproxy",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",external_ip="5.6.7.8",system_uuid="6a934e21-5207-4a84-baea-3a952d926c80"} 1 + kube_node_info{container_runtime_version="rkt",kernel_version="kernel",kubelet_version="kubelet",kubeproxy_version="kubeproxy",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",internal_ip="fc00::",external_ip="5.6.7.8",external_ip="2001:db8::",system_uuid="6a934e21-5207-4a84-baea-3a952d926c80"} 1 kube_node_role{node="127.0.0.1",role="master"} 1 kube_node_spec_unschedulable{node="127.0.0.1"} 1 kube_node_status_allocatable{node="127.0.0.1",resource="cpu",unit="core"} 3