Skip to content

Commit 77ca712

Browse files
committed
add cluster name as a tag to nodebalancers
1 parent 4084b5e commit 77ca712

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

cloud/linode/loadbalancers.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri
238238
klog.Infof("created new NodeBalancer (%d) for service (%s)", nb.ID, serviceNn)
239239

240240
case nil:
241-
if err = l.updateNodeBalancer(ctx, service, nodes, nb); err != nil {
241+
if err = l.updateNodeBalancer(ctx, clusterName, service, nodes, nb); err != nil {
242242
sentry.CaptureError(ctx, err)
243243
return nil, err
244244
}
@@ -262,7 +262,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri
262262
}
263263

264264
//nolint:funlen
265-
func (l *loadbalancers) updateNodeBalancer(ctx context.Context, service *v1.Service, nodes []*v1.Node, nb *linodego.NodeBalancer) (err error) {
265+
func (l *loadbalancers) updateNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node, nb *linodego.NodeBalancer) (err error) {
266266
if len(nodes) == 0 {
267267
return fmt.Errorf("%w: service %s", errNoNodesAvailable, getServiceNn(service))
268268
}
@@ -279,7 +279,7 @@ func (l *loadbalancers) updateNodeBalancer(ctx context.Context, service *v1.Serv
279279
}
280280
}
281281

282-
tags := l.getLoadBalancerTags(ctx, service)
282+
tags := l.getLoadBalancerTags(ctx, clusterName, service)
283283
if !reflect.DeepEqual(nb.Tags, tags) {
284284
update := nb.GetUpdateOptions()
285285
update.Tags = &tags
@@ -391,7 +391,7 @@ func (l *loadbalancers) UpdateLoadBalancer(ctx context.Context, clusterName stri
391391
}
392392
}
393393

394-
return l.updateNodeBalancer(ctx, serviceWithStatus, nodes, nb)
394+
return l.updateNodeBalancer(ctx, clusterName, serviceWithStatus, nodes, nb)
395395
}
396396

397397
// 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
504504
return nb, nil
505505
}
506506

507-
func (l *loadbalancers) getLoadBalancerTags(_ context.Context, service *v1.Service) []string {
507+
func (l *loadbalancers) getLoadBalancerTags(_ context.Context, clusterName string, service *v1.Service) []string {
508+
tags := []string{clusterName}
508509
tagStr, ok := getServiceAnnotation(service, annLinodeLoadBalancerTags)
509510
if ok {
510-
return strings.Split(tagStr, ",")
511+
return append(tags, strings.Split(tagStr, ",")...)
511512
}
512-
return []string{}
513+
return tags
513514
}
514515

515516
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {
516517
connThrottle := getConnectionThrottle(service)
517518

518519
label := l.GetLoadBalancerName(ctx, clusterName, service)
519-
tags := l.getLoadBalancerTags(ctx, service)
520+
tags := l.getLoadBalancerTags(ctx, clusterName, service)
520521
createOpts := linodego.NodeBalancerCreateOptions{
521522
Label: &label,
522523
Region: l.zone,

cloud/linode/loadbalancers_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI) {
277277
t.Logf("actual: %v", nb.ClientConnThrottle)
278278
}
279279

280-
expectedTags := []string{"fake", "test", "yolo"}
280+
expectedTags := []string{"linodelb", "fake", "test", "yolo"}
281281
if !reflect.DeepEqual(nb.Tags, expectedTags) {
282282
t.Error("unexpected Tags")
283283
t.Logf("expected: %v", expectedTags)
@@ -470,10 +470,11 @@ func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fak
470470
lb := &loadbalancers{client, "us-west", nil}
471471
fakeClientset := fake.NewSimpleClientset()
472472
lb.kubeClient = fakeClientset
473+
clusterName := "linodelb"
473474

474-
defer lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc)
475+
defer lb.EnsureLoadBalancerDeleted(context.TODO(), clusterName, svc)
475476

476-
lbStatus, err := lb.EnsureLoadBalancer(context.TODO(), "linodelb", svc, nodes)
477+
lbStatus, err := lb.EnsureLoadBalancer(context.TODO(), clusterName, svc, nodes)
477478
if err != nil {
478479
t.Errorf("EnsureLoadBalancer returned an error: %s", err)
479480
}
@@ -485,7 +486,7 @@ func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fak
485486
annLinodeLoadBalancerTags: testTags,
486487
})
487488

488-
err = lb.UpdateLoadBalancer(context.TODO(), "linodelb", svc, nodes)
489+
err = lb.UpdateLoadBalancer(context.TODO(), clusterName, svc, nodes)
489490
if err != nil {
490491
t.Fatalf("UpdateLoadBalancer returned an error while updated annotations: %s", err)
491492
}
@@ -495,7 +496,7 @@ func testUpdateLoadBalancerAddTags(t *testing.T, client *linodego.Client, _ *fak
495496
t.Fatalf("failed to get NodeBalancer by status: %v", err)
496497
}
497498

498-
expectedTags := strings.Split(testTags, ",")
499+
expectedTags := append([]string{clusterName}, strings.Split(testTags, ",")...)
499500
observedTags := nb.Tags
500501

501502
if !reflect.DeepEqual(expectedTags, observedTags) {

0 commit comments

Comments
 (0)