Browse Source

Merge pull request #5571 from simonpasquier/extend-k8s-endpoint-metadata

discovery/kubernetes: add node name and hostname to endpoints
pull/5573/head
Frederic Branczyk 6 years ago committed by GitHub
parent
commit
04f22700b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      discovery/kubernetes/endpoints.go
  2. 13
      discovery/kubernetes/endpoints_test.go
  3. 2
      docs/configuration/configuration.md

9
discovery/kubernetes/endpoints.go

@ -192,6 +192,8 @@ func endpointsSourceFromNamespaceAndName(namespace, name string) string {
const (
endpointsNameLabel = metaLabelPrefix + "endpoints_name"
endpointNodeName = metaLabelPrefix + "endpoint_node_name"
endpointHostname = metaLabelPrefix + "endpoint_hostname"
endpointReadyLabel = metaLabelPrefix + "endpoint_ready"
endpointPortNameLabel = metaLabelPrefix + "endpoint_port_name"
endpointPortProtocolLabel = metaLabelPrefix + "endpoint_port_protocol"
@ -230,6 +232,13 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
target[model.LabelName(endpointAddressTargetNameLabel)] = lv(addr.TargetRef.Name)
}
if addr.NodeName != nil {
target[model.LabelName(endpointNodeName)] = lv(*addr.NodeName)
}
if addr.Hostname != "" {
target[model.LabelName(endpointHostname)] = lv(addr.Hostname)
}
pod := e.resolvePodRef(addr.TargetRef)
if pod == nil {
// This target is not a Pod, so don't continue with Pod specific logic.

13
discovery/kubernetes/endpoints_test.go

@ -25,6 +25,7 @@ import (
)
func makeEndpoints() *v1.Endpoints {
var nodeName = "foobar"
return &v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: "testendpoints",
@ -34,7 +35,9 @@ func makeEndpoints() *v1.Endpoints {
{
Addresses: []v1.EndpointAddress{
{
IP: "1.2.3.4",
IP: "1.2.3.4",
Hostname: "testendpoint1",
NodeName: &nodeName,
},
},
Ports: []v1.EndpointPort{
@ -83,6 +86,8 @@ func TestEndpointsDiscoveryBeforeRun(t *testing.T) {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:9000",
"__meta_kubernetes_endpoint_hostname": "testendpoint1",
"__meta_kubernetes_endpoint_node_name": "foobar",
"__meta_kubernetes_endpoint_port_name": "testport",
"__meta_kubernetes_endpoint_port_protocol": "TCP",
"__meta_kubernetes_endpoint_ready": "true",
@ -369,6 +374,8 @@ func TestEndpointsDiscoveryWithService(t *testing.T) {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:9000",
"__meta_kubernetes_endpoint_hostname": "testendpoint1",
"__meta_kubernetes_endpoint_node_name": "foobar",
"__meta_kubernetes_endpoint_port_name": "testport",
"__meta_kubernetes_endpoint_port_protocol": "TCP",
"__meta_kubernetes_endpoint_ready": "true",
@ -435,6 +442,8 @@ func TestEndpointsDiscoveryWithServiceUpdate(t *testing.T) {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:9000",
"__meta_kubernetes_endpoint_hostname": "testendpoint1",
"__meta_kubernetes_endpoint_node_name": "foobar",
"__meta_kubernetes_endpoint_port_name": "testport",
"__meta_kubernetes_endpoint_port_protocol": "TCP",
"__meta_kubernetes_endpoint_ready": "true",
@ -545,6 +554,8 @@ func TestEndpointsDiscoveryNamespaces(t *testing.T) {
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:9000",
"__meta_kubernetes_endpoint_hostname": "testendpoint1",
"__meta_kubernetes_endpoint_node_name": "foobar",
"__meta_kubernetes_endpoint_port_name": "testport",
"__meta_kubernetes_endpoint_port_protocol": "TCP",
"__meta_kubernetes_endpoint_ready": "true",

2
docs/configuration/configuration.md

@ -778,6 +778,8 @@ Available meta labels:
* `__meta_kubernetes_endpoints_name`: The names of the endpoints object.
* For all targets discovered directly from the endpoints list (those not additionally inferred
from underlying pods), the following labels are attached:
* `__meta_kubernetes_endpoint_hostname`: Hostname of the endpoint.
* `__meta_kubernetes_endpoint_node_name`: Name of the node hosting the endpoint.
* `__meta_kubernetes_endpoint_ready`: Set to `true` or `false` for the endpoint's ready state.
* `__meta_kubernetes_endpoint_port_name`: Name of the endpoint port.
* `__meta_kubernetes_endpoint_port_protocol`: Protocol of the endpoint port.

Loading…
Cancel
Save