From 77ca7120306de137bc8985f90811f4037d69b6d3 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Fri, 1 Dec 2023 21:33:14 -0700 Subject: [PATCH] add cluster name as a tag to nodebalancers --- cloud/linode/loadbalancers.go | 17 +++++++++-------- cloud/linode/loadbalancers_test.go | 11 ++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cloud/linode/loadbalancers.go b/cloud/linode/loadbalancers.go index 01c6de4a..fe6eeda5 100644 --- a/cloud/linode/loadbalancers.go +++ b/cloud/linode/loadbalancers.go @@ -238,7 +238,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri klog.Infof("created new NodeBalancer (%d) for service (%s)", nb.ID, serviceNn) case nil: - if err = l.updateNodeBalancer(ctx, service, nodes, nb); err != nil { + if err = l.updateNodeBalancer(ctx, clusterName, service, nodes, nb); err != nil { sentry.CaptureError(ctx, err) return nil, err } @@ -262,7 +262,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri } //nolint:funlen -func (l *loadbalancers) updateNodeBalancer(ctx context.Context, service *v1.Service, nodes []*v1.Node, nb *linodego.NodeBalancer) (err error) { +func (l *loadbalancers) updateNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node, nb *linodego.NodeBalancer) (err error) { if len(nodes) == 0 { return fmt.Errorf("%w: service %s", errNoNodesAvailable, getServiceNn(service)) } @@ -279,7 +279,7 @@ func (l *loadbalancers) updateNodeBalancer(ctx context.Context, service *v1.Serv } } - tags := l.getLoadBalancerTags(ctx, service) + tags := l.getLoadBalancerTags(ctx, clusterName, service) if !reflect.DeepEqual(nb.Tags, tags) { update := nb.GetUpdateOptions() update.Tags = &tags @@ -391,7 +391,7 @@ func (l *loadbalancers) UpdateLoadBalancer(ctx context.Context, clusterName stri } } - return l.updateNodeBalancer(ctx, serviceWithStatus, nodes, nb) + return l.updateNodeBalancer(ctx, clusterName, serviceWithStatus, nodes, nb) } // Delete any NodeBalancer configs for ports that no longer exist on the Service @@ -504,19 +504,20 @@ func (l *loadbalancers) getNodeBalancerByID(ctx context.Context, service *v1.Ser return nb, nil } -func (l *loadbalancers) getLoadBalancerTags(_ context.Context, service *v1.Service) []string { +func (l *loadbalancers) getLoadBalancerTags(_ context.Context, clusterName string, service *v1.Service) []string { + tags := []string{clusterName} tagStr, ok := getServiceAnnotation(service, annLinodeLoadBalancerTags) if ok { - return strings.Split(tagStr, ",") + return append(tags, strings.Split(tagStr, ",")...) } - return []string{} + return tags } func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) { connThrottle := getConnectionThrottle(service) label := l.GetLoadBalancerName(ctx, clusterName, service) - tags := l.getLoadBalancerTags(ctx, service) + tags := l.getLoadBalancerTags(ctx, clusterName, service) createOpts := linodego.NodeBalancerCreateOptions{ Label: &label, Region: l.zone, diff --git a/cloud/linode/loadbalancers_test.go b/cloud/linode/loadbalancers_test.go index 489a2e7d..acc6095d 100644 --- a/cloud/linode/loadbalancers_test.go +++ b/cloud/linode/loadbalancers_test.go @@ -277,7 +277,7 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI) { t.Logf("actual: %v", nb.ClientConnThrottle) } - expectedTags := []string{"fake", "test", "yolo"} + expectedTags := []string{"linodelb", "fake", "test", "yolo"} if !reflect.DeepEqual(nb.Tags, expectedTags) { t.Error("unexpected Tags") t.Logf("expected: %v", expectedTags) @@ -470,10 +470,11 @@ func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fak lb := &loadbalancers{client, "us-west", nil} fakeClientset := fake.NewSimpleClientset() lb.kubeClient = fakeClientset + clusterName := "linodelb" - defer lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc) + defer lb.EnsureLoadBalancerDeleted(context.TODO(), clusterName, svc) - lbStatus, err := lb.EnsureLoadBalancer(context.TODO(), "linodelb", svc, nodes) + lbStatus, err := lb.EnsureLoadBalancer(context.TODO(), clusterName, svc, nodes) if err != nil { t.Errorf("EnsureLoadBalancer returned an error: %s", err) } @@ -485,7 +486,7 @@ func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fak annLinodeLoadBalancerTags: testTags, }) - err = lb.UpdateLoadBalancer(context.TODO(), "linodelb", svc, nodes) + err = lb.UpdateLoadBalancer(context.TODO(), clusterName, svc, nodes) if err != nil { t.Fatalf("UpdateLoadBalancer returned an error while updated annotations: %s", err) } @@ -495,7 +496,7 @@ func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fak t.Fatalf("failed to get NodeBalancer by status: %v", err) } - expectedTags := strings.Split(testTags, ",") + expectedTags := append([]string{clusterName}, strings.Split(testTags, ",")...) observedTags := nb.Tags if !reflect.DeepEqual(expectedTags, observedTags) {