Merge pull request #11140 from mercedes-benz/export-external-ip

Add LoadBalancerIP to discovered services
This commit is contained in:
Frederic Branczyk 2022-08-12 10:48:59 +02:00 committed by GitHub
commit 0683348121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 1 deletions

View File

@ -153,6 +153,7 @@ const (
servicePortNumberLabel = metaLabelPrefix + "service_port_number" servicePortNumberLabel = metaLabelPrefix + "service_port_number"
servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol" servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol"
serviceClusterIPLabel = metaLabelPrefix + "service_cluster_ip" serviceClusterIPLabel = metaLabelPrefix + "service_cluster_ip"
serviceLoadBalancerIP = metaLabelPrefix + "service_loadbalancer_ip"
serviceExternalNameLabel = metaLabelPrefix + "service_external_name" serviceExternalNameLabel = metaLabelPrefix + "service_external_name"
serviceType = metaLabelPrefix + "service_type" serviceType = metaLabelPrefix + "service_type"
) )
@ -201,6 +202,10 @@ func (s *Service) buildService(svc *apiv1.Service) *targetgroup.Group {
labelSet[serviceClusterIPLabel] = lv(svc.Spec.ClusterIP) labelSet[serviceClusterIPLabel] = lv(svc.Spec.ClusterIP)
} }
if svc.Spec.Type == apiv1.ServiceTypeLoadBalancer {
labelSet[serviceLoadBalancerIP] = lv(svc.Spec.LoadBalancerIP)
}
tg.Targets = append(tg.Targets, labelSet) 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) { func TestServiceDiscoveryAdd(t *testing.T) {
n, c := makeDiscovery(RoleService, NamespaceDiscovery{}) 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{}) c.CoreV1().Services(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{})
obj = makeExternalService() obj = makeExternalService()
c.CoreV1().Services(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) 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{ expectedRes: map[string]*targetgroup.Group{
"svc/default/testservice": { "svc/default/testservice": {
Targets: []model.LabelSet{ Targets: []model.LabelSet{
@ -143,6 +166,24 @@ func TestServiceDiscoveryAdd(t *testing.T) {
}, },
Source: "svc/default/testservice-external", 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) }.Run(t)
} }

View File

@ -1678,6 +1678,7 @@ Available meta labels:
* `__meta_kubernetes_service_annotation_<annotationname>`: Each annotation from the service object. * `__meta_kubernetes_service_annotation_<annotationname>`: Each annotation from the service object.
* `__meta_kubernetes_service_annotationpresent_<annotationname>`: "true" for each annotation of the service object. * `__meta_kubernetes_service_annotationpresent_<annotationname>`: "true" for each annotation of the service object.
* `__meta_kubernetes_service_cluster_ip`: The cluster IP address of the service. (Does not apply to services of type ExternalName) * `__meta_kubernetes_service_cluster_ip`: The cluster IP address of the service. (Does not apply to services of type ExternalName)
* `__meta_kubernetes_service_loadbalancer_ip`: The IP address of the loadbalancer. (Applies to services of type LoadBalancer)
* `__meta_kubernetes_service_external_name`: The DNS name of the service. (Applies to services of type ExternalName) * `__meta_kubernetes_service_external_name`: The DNS name of the service. (Applies to services of type ExternalName)
* `__meta_kubernetes_service_label_<labelname>`: Each label from the service object. * `__meta_kubernetes_service_label_<labelname>`: Each label from the service object.
* `__meta_kubernetes_service_labelpresent_<labelname>`: `true` for each label of the service object. * `__meta_kubernetes_service_labelpresent_<labelname>`: `true` for each label of the service object.