Skip to content

Commit

Permalink
Merge pull request #151 from luthermonson/clustername-tag
Browse files Browse the repository at this point in the history
add cluster name as a tag to nodebalancers
  • Loading branch information
srust authored Dec 4, 2023
2 parents 8148850 + 77ca712 commit af0a0b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
17 changes: 9 additions & 8 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,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 @@ -263,7 +263,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 @@ -280,7 +280,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 @@ -392,7 +392,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 @@ -505,19 +505,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 @@ -282,7 +282,7 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI, f
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 @@ -500,10 +500,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 @@ -515,7 +516,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 @@ -525,7 +526,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

0 comments on commit af0a0b4

Please sign in to comment.