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

discovery/kubernetes: add node name and hostname to endpoints
This commit is contained in:
Frederic Branczyk 2019-05-16 20:19:29 +02:00 committed by GitHub
commit 04f22700b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View file

@ -192,6 +192,8 @@ func endpointsSourceFromNamespaceAndName(namespace, name string) string {
const ( const (
endpointsNameLabel = metaLabelPrefix + "endpoints_name" endpointsNameLabel = metaLabelPrefix + "endpoints_name"
endpointNodeName = metaLabelPrefix + "endpoint_node_name"
endpointHostname = metaLabelPrefix + "endpoint_hostname"
endpointReadyLabel = metaLabelPrefix + "endpoint_ready" endpointReadyLabel = metaLabelPrefix + "endpoint_ready"
endpointPortNameLabel = metaLabelPrefix + "endpoint_port_name" endpointPortNameLabel = metaLabelPrefix + "endpoint_port_name"
endpointPortProtocolLabel = metaLabelPrefix + "endpoint_port_protocol" 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) 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) pod := e.resolvePodRef(addr.TargetRef)
if pod == nil { if pod == nil {
// This target is not a Pod, so don't continue with Pod specific logic. // This target is not a Pod, so don't continue with Pod specific logic.

View file

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

View file

@ -778,6 +778,8 @@ Available meta labels:
* `__meta_kubernetes_endpoints_name`: The names of the endpoints object. * `__meta_kubernetes_endpoints_name`: The names of the endpoints object.
* For all targets discovered directly from the endpoints list (those not additionally inferred * For all targets discovered directly from the endpoints list (those not additionally inferred
from underlying pods), the following labels are attached: 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_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_name`: Name of the endpoint port.
* `__meta_kubernetes_endpoint_port_protocol`: Protocol of the endpoint port. * `__meta_kubernetes_endpoint_port_protocol`: Protocol of the endpoint port.