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 cluster name as a tag to nodebalancers #151

Merged
merged 1 commit into from
Dec 4, 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
17 changes: 9 additions & 8 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions cloud/linode/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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) {
Expand Down
Loading