Skip to content

Commit

Permalink
Merge pull request #2539 from mrueg/expose-empty-labels
Browse files Browse the repository at this point in the history
fix: expose empty labels
  • Loading branch information
k8s-ci-robot authored Nov 5, 2024
2 parents dfb688c + 2aa766e commit 0738de0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 89 deletions.
22 changes: 6 additions & 16 deletions internal/store/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,25 @@ func endpointMetricFamilies(allowAnnotationsList, allowLabelsList []string) []ge
"",
wrapEndpointFunc(func(e *v1.Endpoints) *metric.Family {
ms := []*metric.Metric{}
labelKeys := []string{"port_protocol", "port_number", "port_name", "ip", "ready"}

for _, s := range e.Subsets {
for _, port := range s.Ports {
for _, available := range s.Addresses {
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10)}
labelKeys := []string{"port_protocol", "port_number"}

if port.Name != "" {
labelKeys = append(labelKeys, "port_name")
labelValues = append(labelValues, port.Name)
}
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10), port.Name}

ms = append(ms, &metric.Metric{
LabelValues: append(labelValues, available.IP, "true"),
LabelKeys: append(labelKeys, "ip", "ready"),
LabelKeys: labelKeys,
Value: 1,
})
}
for _, notReadyAddresses := range s.NotReadyAddresses {
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10)}
labelKeys := []string{"port_protocol", "port_number"}

if port.Name != "" {
labelKeys = append(labelKeys, "port_name")
labelValues = append(labelValues, port.Name)
}
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10), port.Name}

ms = append(ms, &metric.Metric{
LabelValues: append(labelValues, notReadyAddresses.IP, "false"),
LabelKeys: append(labelKeys, "ip", "ready"),
LabelKeys: labelKeys,
Value: 1,
})
}
Expand Down
12 changes: 6 additions & 6 deletions internal/store/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func TestEndpointStore(t *testing.T) {
kube_endpoint_created{endpoint="single-port-endpoint",namespace="default"} 1.5e+09
kube_endpoint_info{endpoint="single-port-endpoint",namespace="default"} 1
kube_endpoint_ports{endpoint="single-port-endpoint",namespace="default",port_name="",port_number="8080",port_protocol="TCP"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_number="8080",port_protocol="TCP",ready="false"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="false"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
`,
},
}
Expand Down Expand Up @@ -269,9 +269,9 @@ func TestEndpointStoreWithLabels(t *testing.T) {
kube_endpoint_info{endpoint="single-port-endpoint",namespace="default"} 1
kube_endpoint_labels{endpoint="single-port-endpoint",label_app="single-foobar",namespace="default"} 1
kube_endpoint_ports{endpoint="single-port-endpoint",namespace="default",port_name="",port_number="8080",port_protocol="TCP"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_number="8080",port_protocol="TCP",ready="false"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="false"} 1
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
`,
},
}
Expand Down
47 changes: 17 additions & 30 deletions internal/store/endpointslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,54 +121,41 @@ func endpointSliceMetricFamilies(allowAnnotationsList, allowLabelsList []string)
wrapEndpointSliceFunc(func(e *discoveryv1.EndpointSlice) *metric.Family {
m := []*metric.Metric{}
for _, ep := range e.Endpoints {
var (
labelKeys,
labelValues []string
)

var ready, serving, terminating, hostname, targetrefKind, targetrefName, targetrefNamespace, endpointNodename, endpointZone string

if ep.Conditions.Ready != nil {
labelKeys = append(labelKeys, "ready")
labelValues = append(labelValues, strconv.FormatBool(*ep.Conditions.Ready))
ready = strconv.FormatBool(*ep.Conditions.Ready)
}

if ep.Conditions.Serving != nil {
labelKeys = append(labelKeys, "serving")
labelValues = append(labelValues, strconv.FormatBool(*ep.Conditions.Serving))
serving = strconv.FormatBool(*ep.Conditions.Serving)
}

if ep.Conditions.Terminating != nil {
labelKeys = append(labelKeys, "terminating")
labelValues = append(labelValues, strconv.FormatBool(*ep.Conditions.Terminating))
serving = strconv.FormatBool(*ep.Conditions.Terminating)
}

if ep.Hostname != nil {
labelKeys = append(labelKeys, "hostname")
labelValues = append(labelValues, *ep.Hostname)
hostname = *ep.Hostname
}

if ep.TargetRef != nil {
if ep.TargetRef.Kind != "" {
labelKeys = append(labelKeys, "targetref_kind")
labelValues = append(labelValues, ep.TargetRef.Kind)
}
if ep.TargetRef.Name != "" {
labelKeys = append(labelKeys, "targetref_name")
labelValues = append(labelValues, ep.TargetRef.Name)
}
if ep.TargetRef.Namespace != "" {
labelKeys = append(labelKeys, "targetref_namespace")
labelValues = append(labelValues, ep.TargetRef.Namespace)
}
targetrefKind = ep.TargetRef.Kind
targetrefName = ep.TargetRef.Name
targetrefNamespace = ep.TargetRef.Namespace
}

if ep.NodeName != nil {
labelKeys = append(labelKeys, "endpoint_nodename")
labelValues = append(labelValues, *ep.NodeName)
endpointNodename = *ep.NodeName
}

if ep.Zone != nil {
labelKeys = append(labelKeys, "endpoint_zone")
labelValues = append(labelValues, *ep.Zone)
endpointZone = *ep.Zone
}
labelKeys = append(labelKeys, "address")

labelKeys := []string{"ready", "serving", "hostname", "terminating", "targetref_kind", "targetref_name", "targetref_namespace", "endpoint_nodename", "endpoint_zone", "address"}
labelValues := []string{ready, serving, terminating, hostname, targetrefKind, targetrefName, targetrefNamespace, endpointNodename, endpointZone}

for _, address := range ep.Addresses {
newlabelValues := make([]string, len(labelValues))
copy(newlabelValues, labelValues)
Expand Down
10 changes: 5 additions & 5 deletions internal/store/endpointslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func TestEndpointSliceStore(t *testing.T) {
# HELP kube_endpointslice_endpoints_hints Topology routing hints attached to endpoints
# TYPE kube_endpointslice_endpoints gauge
# TYPE kube_endpointslice_endpoints_hints gauge
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
`,

MetricNames: []string{
Expand Down Expand Up @@ -159,9 +159,9 @@ func TestEndpointSliceStore(t *testing.T) {
# TYPE kube_endpointslice_endpoints gauge
# TYPE kube_endpointslice_endpoints_hints gauge
kube_endpointslice_endpoints_hints{address="10.0.0.1",endpointslice="test_endpointslice-endpoints",for_zone="zone1",namespace="test"} 1
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
`,
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
`,

MetricNames: []string{
"kube_endpointslice_endpoints",
Expand Down
31 changes: 8 additions & 23 deletions internal/store/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,41 +1653,26 @@ func createPodTolerationsFamilyGenerator() generator.FamilyGenerator {
var ms []*metric.Metric

for _, t := range p.Spec.Tolerations {
var labelKeys []string
var labelValues []string

if t.Key != "" {
labelKeys = append(labelKeys, "key")
labelValues = append(labelValues, t.Key)
}
var key, operator, value, effect, tolerationSeconds string

key = t.Key
if t.Operator != "" {
labelKeys = append(labelKeys, "operator")
labelValues = append(labelValues, string(t.Operator))
operator = string(t.Operator)
}

if t.Value != "" {
labelKeys = append(labelKeys, "value")
labelValues = append(labelValues, t.Value)
}
value = t.Value

if t.Effect != "" {
labelKeys = append(labelKeys, "effect")
labelValues = append(labelValues, string(t.Effect))
effect = string(t.Effect)
}

if t.TolerationSeconds != nil {
labelKeys = append(labelKeys, "toleration_seconds")
labelValues = append(labelValues, strconv.FormatInt(*t.TolerationSeconds, 10))
}

if len(labelKeys) == 0 {
continue
tolerationSeconds = strconv.FormatInt(*t.TolerationSeconds, 10)
}

ms = append(ms, &metric.Metric{
LabelKeys: labelKeys,
LabelValues: labelValues,
LabelKeys: []string{"key", "operator", "value", "effect", "toleration_seconds"},
LabelValues: []string{key, operator, value, effect, tolerationSeconds},
Value: 1,
})
}
Expand Down
7 changes: 4 additions & 3 deletions internal/store/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2123,9 +2123,10 @@ func TestPodStore(t *testing.T) {
Want: `
# HELP kube_pod_tolerations Information about the pod tolerations
# TYPE kube_pod_tolerations gauge
kube_pod_tolerations{namespace="ns1",pod="pod1",uid="uid1",key="key1",operator="Equal",value="value1",effect="NoSchedule"} 1
kube_pod_tolerations{namespace="ns1",pod="pod1",uid="uid1",key="key2",operator="Exists"} 1
kube_pod_tolerations{namespace="ns1",pod="pod1",uid="uid1",key="key3",operator="Equal",value="value3"} 1
kube_pod_tolerations{effect="",key="",namespace="ns1",operator="",pod="pod1",toleration_seconds="",uid="uid1",value=""} 1
kube_pod_tolerations{effect="",key="key2",namespace="ns1",operator="Exists",pod="pod1",toleration_seconds="",uid="uid1",value=""} 1
kube_pod_tolerations{effect="",key="key3",namespace="ns1",operator="Equal",pod="pod1",toleration_seconds="",uid="uid1",value="value3"} 1
kube_pod_tolerations{effect="NoSchedule",key="key1",namespace="ns1",operator="Equal",pod="pod1",toleration_seconds="",uid="uid1",value="value1"} 1
`,
MetricNames: []string{
"kube_pod_tolerations",
Expand Down
10 changes: 4 additions & 6 deletions internal/store/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,16 @@ func createServiceAccountInfoFamilyGenerator() generator.FamilyGenerator {
basemetrics.ALPHA,
"",
wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family {
var labelKeys []string
var labelValues []string
var automountToken string

if sa.AutomountServiceAccountToken != nil {
labelKeys = append(labelKeys, "automount_token")
labelValues = append(labelValues, strconv.FormatBool(*sa.AutomountServiceAccountToken))
automountToken = strconv.FormatBool(*sa.AutomountServiceAccountToken)
}

return &metric.Family{
Metrics: []*metric.Metric{{
LabelKeys: labelKeys,
LabelValues: labelValues,
LabelKeys: []string{"automount_token"},
LabelValues: []string{automountToken},
Value: 1,
}},
}
Expand Down

0 comments on commit 0738de0

Please sign in to comment.