Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add id custom labels to infra Load balancers #206

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/provider/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
26 changes: 17 additions & 9 deletions pkg/provider/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down