From 3782cb40d582d49b264230c6bd1307ee2df44c07 Mon Sep 17 00:00:00 2001 From: Karl Piplies Date: Wed, 10 Aug 2022 12:20:43 +0200 Subject: [PATCH] add loadbalancerip to service labels Signed-off-by: Karl Piplies --- discovery/kubernetes/service.go | 5 ++++ discovery/kubernetes/service_test.go | 43 +++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/discovery/kubernetes/service.go b/discovery/kubernetes/service.go index 3db48ab6e..44ebcbc19 100644 --- a/discovery/kubernetes/service.go +++ b/discovery/kubernetes/service.go @@ -153,6 +153,7 @@ const ( servicePortNumberLabel = metaLabelPrefix + "service_port_number" servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol" serviceClusterIPLabel = metaLabelPrefix + "service_cluster_ip" + serviceLoadBalancerIP = metaLabelPrefix + "service_loadbalancer_ip" serviceExternalNameLabel = metaLabelPrefix + "service_external_name" serviceType = metaLabelPrefix + "service_type" ) @@ -201,6 +202,10 @@ func (s *Service) buildService(svc *apiv1.Service) *targetgroup.Group { labelSet[serviceClusterIPLabel] = lv(svc.Spec.ClusterIP) } + if svc.Spec.Type == apiv1.ServiceTypeLoadBalancer { + labelSet[serviceLoadBalancerIP] = lv(svc.Spec.LoadBalancerIP) + } + tg.Targets = append(tg.Targets, labelSet) } diff --git a/discovery/kubernetes/service_test.go b/discovery/kubernetes/service_test.go index 4aeddfad7..dde3aaea5 100644 --- a/discovery/kubernetes/service_test.go +++ b/discovery/kubernetes/service_test.go @@ -96,6 +96,27 @@ func makeExternalService() *v1.Service { } } +func makeLoadBalancerService() *v1.Service { + return &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testservice-loadbalancer", + Namespace: "default", + }, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{ + { + Name: "testport", + Protocol: v1.ProtocolTCP, + Port: int32(31900), + }, + }, + Type: v1.ServiceTypeLoadBalancer, + LoadBalancerIP: "127.0.0.1", + ClusterIP: "10.0.0.1", + }, + } +} + func TestServiceDiscoveryAdd(t *testing.T) { n, c := makeDiscovery(RoleService, NamespaceDiscovery{}) @@ -106,8 +127,10 @@ func TestServiceDiscoveryAdd(t *testing.T) { c.CoreV1().Services(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) obj = makeExternalService() c.CoreV1().Services(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) + obj = makeLoadBalancerService() + c.CoreV1().Services(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) }, - expectedMaxItems: 2, + expectedMaxItems: 3, expectedRes: map[string]*targetgroup.Group{ "svc/default/testservice": { Targets: []model.LabelSet{ @@ -143,6 +166,24 @@ func TestServiceDiscoveryAdd(t *testing.T) { }, Source: "svc/default/testservice-external", }, + "svc/default/testservice-loadbalancer": { + Targets: []model.LabelSet{ + { + "__meta_kubernetes_service_port_protocol": "TCP", + "__address__": "testservice-loadbalancer.default.svc:31900", + "__meta_kubernetes_service_type": "LoadBalancer", + "__meta_kubernetes_service_port_name": "testport", + "__meta_kubernetes_service_port_number": "31900", + "__meta_kubernetes_service_cluster_ip": "10.0.0.1", + "__meta_kubernetes_service_loadbalancer_ip": "127.0.0.1", + }, + }, + Labels: model.LabelSet{ + "__meta_kubernetes_service_name": "testservice-loadbalancer", + "__meta_kubernetes_namespace": "default", + }, + Source: "svc/default/testservice-loadbalancer", + }, }, }.Run(t) }