Skip to content

Commit

Permalink
Add support for endpoint topology routing hints
Browse files Browse the repository at this point in the history
Fix goimports

Fix gocritic issue
  • Loading branch information
MarkSRobinson committed Jun 5, 2023
1 parent 93fe0be commit 5968a5e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/endpointslice-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
| kube_endpointslice_annotations | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; <br> `annotation_ENDPOINTSLICE_ANNOTATION`=&lt;ENDPOINTSLICE_ANNOTATION&gt; | EXPERIMENTAL |
| kube_endpointslice_info | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; | EXPERIMENTAL |
| kube_endpointslice_ports | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; <br> `port_name`=&lt;endpointslice-port-name&gt; <br> `port_protocol`=&lt;endpointslice-port-protocol&gt; <br> `port_number`=&lt;endpointslice-port-number&gt; | EXPERIMENTAL |
| kube_endpointslice_endpoints | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; <br> `ready`=&lt;endpointslice-ready&gt; <br> `serving`=&lt;endpointslice-serving&gt; <br> `terminating`=&lt;endpointslice-terminating&gt; <br> `hostname`=&lt;endpointslice-hostname&gt; <br> `targetref_kind`=&lt;endpointslice-targetref-kind&gt; <br> `targetref_name`=&lt;endpointslice-targetref-name&gt; <br> `targetref_namespace`=&lt;endpointslice-targetref-namespace&gt; <br> `nodename`=&lt;endpointslice-nodename&gt; <br> `endpoint_zone`=&lt;endpointslice-zone&gt; | EXPERIMENTAL |
| kube_endpointslice_endpoints | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; <br> `ready`=&lt;endpointslice-ready&gt; <br> `serving`=&lt;endpointslice-serving&gt; <br> `terminating`=&lt;endpointslice-terminating&gt; <br> `hostname`=&lt;endpointslice-hostname&gt; <br> `targetref_kind`=&lt;endpointslice-targetref-kind&gt; <br> `targetref_name`=&lt;endpointslice-targetref-name&gt; <br> `targetref_namespace`=&lt;endpointslice-targetref-namespace&gt; <br> `nodename`=&lt;endpointslice-nodename&gt; <br> `endpoint_zone`=&lt;endpointslice-zone&gt; <br> `hint`=&lt;topology-aware-routing-hint&gt; | EXPERIMENTAL |
| kube_endpointslice_labels | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; <br> `label_ENDPOINTSLICE_LABEL`=&lt;ENDPOINTSLICE_LABEL&gt; | EXPERIMENTAL |
| kube_endpointslice_created | Gauge | `endpointslice`=&lt;endpointslice-name&gt; <br> `namespace`=&lt;endpointslice-namespace&gt; | EXPERIMENTAL |
32 changes: 27 additions & 5 deletions internal/store/endpointslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,33 @@ func endpointSliceMetricFamilies(allowAnnotationsList, allowLabelsList []string)
for _, address := range ep.Addresses {
newlabelValues := make([]string, len(labelValues))
copy(newlabelValues, labelValues)
m = append(m, &metric.Metric{
LabelKeys: labelKeys,
LabelValues: append(newlabelValues, address),
Value: 1,
})
newlabelValues = append(newlabelValues, address)

// Hint is populated when the endpoint is configured to be zone aware and preferentially route requests to its local zone.
if ep.Hints != nil && len(ep.Hints.ForZones) > 0 {

// Because each endpoint can have multiple zones, we need to create a metric for each zone.
// and we need to make sure we aren't adding the hint label repeatedly, we need to copy the array
zoneLabelKeys := make([]string, len(labelKeys))
copy(zoneLabelKeys, labelKeys)
zoneLabelKeys = append(zoneLabelKeys, "hint")
for _, zone := range ep.Hints.ForZones {
zoneLabelValues := make([]string, len(newlabelValues))
copy(zoneLabelValues, newlabelValues)
zoneLabelValues = append(zoneLabelValues, zone.Name)
m = append(m, &metric.Metric{
LabelKeys: zoneLabelKeys,
LabelValues: zoneLabelValues,
Value: 1,
})
}
} else {
m = append(m, &metric.Metric{
LabelKeys: labelKeys,
LabelValues: newlabelValues,
Value: 1,
})
}
}
}
return &metric.Family{
Expand Down
36 changes: 36 additions & 0 deletions internal/store/endpointslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"

corev1 "k8s.io/api/core/v1"

discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -122,6 +123,41 @@ func TestEndpointSliceStore(t *testing.T) {
"kube_endpointslice_endpoints",
},
},
{
Obj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "test_endpointslice-endpoints",
},
AddressType: "IPv4",
Endpoints: []discoveryv1.Endpoint{
{
NodeName: &nodename,
Conditions: discoveryv1.EndpointConditions{
Ready: &ready,
Terminating: &terminating,
},
Hostname: &hostname,
Zone: &zone,
Addresses: addresses,
Hints: &discoveryv1.EndpointHints{
ForZones: []discoveryv1.ForZone{
{Name: "zone1"},
},
},
},
},
},
Want: `
# HELP kube_endpointslice_endpoints Endpoints attached to the endpointslice.
# TYPE kube_endpointslice_endpoints 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",hint="zone1"} 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",hint="zone1"} 1
`,

MetricNames: []string{
"kube_endpointslice_endpoints",
},
},
{
AllowAnnotationsList: []string{
"foo",
Expand Down

0 comments on commit 5968a5e

Please sign in to comment.