add loadbalancerip to service labels

Signed-off-by: Karl Piplies <karl.piplies@mercedes-benz.com>
This commit is contained in:
Karl Piplies 2022-08-10 12:20:43 +02:00
parent d91621890a
commit 3782cb40d5
2 changed files with 47 additions and 1 deletions

View file

@ -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)
}

View file

@ -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)
}