diff --git a/pkg/provider/cloud.go b/pkg/provider/cloud.go index f67b982ad..a51e65238 100644 --- a/pkg/provider/cloud.go +++ b/pkg/provider/cloud.go @@ -45,6 +45,7 @@ type CloudConfig struct { LoadBalancer LoadBalancerConfig `yaml:"loadBalancer"` InstancesV2 InstancesV2Config `yaml:"instancesV2"` Namespace string `yaml:"namespace"` + InfraLabels map[string]string `yaml:"infraLabels"` } type LoadBalancerConfig struct { @@ -153,9 +154,10 @@ func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) { return nil, false } return &loadbalancer{ - namespace: c.namespace, - client: c.client, - config: c.config.LoadBalancer, + namespace: c.namespace, + client: c.client, + config: c.config.LoadBalancer, + infraLabels: c.config.InfraLabels, }, true } diff --git a/pkg/provider/loadbalancer.go b/pkg/provider/loadbalancer.go index 3aa4e3a3d..fbe3d6e31 100644 --- a/pkg/provider/loadbalancer.go +++ b/pkg/provider/loadbalancer.go @@ -21,9 +21,10 @@ const ( ) type loadbalancer struct { - namespace string - client client.Client - config LoadBalancerConfig + namespace string + client client.Client + config LoadBalancerConfig + infraLabels map[string]string } // GetLoadBalancer returns whether the specified load balancer exists, and @@ -75,7 +76,17 @@ func (lb *loadbalancer) EnsureLoadBalancer(ctx context.Context, clusterName stri "cluster.x-k8s.io/cluster-name": clusterName, } - lbService, err = lb.createLoadBalancerService(ctx, lbName, service, vmiLabels, ports) + lbLabels := map[string]string{ + "cluster.x-k8s.io/tenant-service-name": service.Name, + "cluster.x-k8s.io/tenant-service-namespace": service.Namespace, + "cluster.x-k8s.io/cluster-name": clusterName, + } + + for key, val := range lb.infraLabels { + lbLabels[key] = val + } + + lbService, err = lb.createLoadBalancerService(ctx, lbName, service, vmiLabels, lbLabels, ports) if err != nil { klog.Errorf("Failed to create LoadBalancer service: %v", err) return nil, err @@ -174,16 +185,13 @@ func (lb *loadbalancer) getLoadBalancerService(ctx context.Context, lbName strin return &service, nil } -func (lb *loadbalancer) createLoadBalancerService(ctx context.Context, lbName string, service *corev1.Service, vmiLabels map[string]string, ports []corev1.ServicePort) (*corev1.Service, error) { +func (lb *loadbalancer) createLoadBalancerService(ctx context.Context, lbName string, service *corev1.Service, vmiLabels map[string]string, lbLabels map[string]string, ports []corev1.ServicePort) (*corev1.Service, error) { lbService := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: lbName, Namespace: lb.namespace, Annotations: service.Annotations, - Labels: map[string]string{ - "cluster.x-k8s.io/tenant-service-name": service.Name, - "cluster.x-k8s.io/tenant-service-namespace": service.Namespace, - }, + Labels: lbLabels, }, Spec: corev1.ServiceSpec{ Ports: ports, diff --git a/pkg/provider/loadbalancer_test.go b/pkg/provider/loadbalancer_test.go index 6066a7726..4ba676308 100644 --- a/pkg/provider/loadbalancer_test.go +++ b/pkg/provider/loadbalancer_test.go @@ -133,6 +133,7 @@ func generateInfraService(tenantSvc *corev1.Service, ports []corev1.ServicePort) Labels: map[string]string{ "cluster.x-k8s.io/tenant-service-name": tenantSvc.Name, "cluster.x-k8s.io/tenant-service-namespace": tenantSvc.Namespace, + "cluster.x-k8s.io/cluster-name": clusterName, }, Annotations: tenantSvc.Annotations, },