Skip to content

Commit 70fd339

Browse files
committed
Add option for global NodeBalancer tags
1 parent 6a4e482 commit 70fd339

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

cloud/linode/cloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var Options struct {
3737
VPCName string
3838
LoadBalancerType string
3939
BGPNodeSelector string
40+
NodeBalancerTags []string
4041
}
4142

4243
// vpcDetails is set when VPCName options flag is set.

cloud/linode/loadbalancers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ func (l *loadbalancers) GetLoadBalancerTags(_ context.Context, clusterName strin
599599
tags = append(tags, clusterName)
600600
}
601601

602+
tags = append(tags, Options.NodeBalancerTags...)
603+
602604
tagStr, ok := service.GetAnnotations()[annotations.AnnLinodeLoadBalancerTags]
603605
if ok {
604606
return append(tags, strings.Split(tagStr, ",")...)

cloud/linode/loadbalancers_test.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func TestCCMLoadBalancers(t *testing.T) {
150150
name: "Create Load Balancer With Invalid Firewall ACL - NO Allow Or Deny",
151151
f: testCreateNodeBalanceWithNoAllowOrDenyList,
152152
},
153+
{
154+
name: "Create Load Balancer With Global Tags set",
155+
f: testCreateNodeBalancerWithGlobalTags,
156+
},
153157
{
154158
name: "Update Load Balancer - Add Node",
155159
f: testUpdateLoadBalancerAddNode,
@@ -274,7 +278,7 @@ func stubService(fake *fake.Clientset, service *v1.Service) {
274278
_, _ = fake.CoreV1().Services("").Create(context.TODO(), service, metav1.CreateOptions{})
275279
}
276280

277-
func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI, annMap map[string]string) error {
281+
func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI, annMap map[string]string, expectedTags []string) error {
278282
svc := &v1.Service{
279283
ObjectMeta: metav1.ObjectMeta{
280284
Name: randString(),
@@ -341,7 +345,9 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI, a
341345
t.Logf("actual: %v", nb.ClientConnThrottle)
342346
}
343347

344-
expectedTags := []string{"linodelb", "fake", "test", "yolo"}
348+
if len(expectedTags) == 0 {
349+
expectedTags = []string{"linodelb", "fake", "test", "yolo"}
350+
}
345351
if !reflect.DeepEqual(nb.Tags, expectedTags) {
346352
t.Error("unexpected Tags")
347353
t.Logf("expected: %v", expectedTags)
@@ -366,7 +372,7 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI, a
366372
}
367373

368374
func testCreateNodeBalancerWithOutFirewall(t *testing.T, client *linodego.Client, f *fakeAPI) {
369-
err := testCreateNodeBalancer(t, client, f, nil)
375+
err := testCreateNodeBalancer(t, client, f, nil, nil)
370376
if err != nil {
371377
t.Fatalf("expected a nil error, got %v", err)
372378
}
@@ -377,7 +383,7 @@ func testCreateNodeBalanceWithNoAllowOrDenyList(t *testing.T, client *linodego.C
377383
annotations.AnnLinodeCloudFirewallACL: `{}`,
378384
}
379385

380-
err := testCreateNodeBalancer(t, client, f, annotations)
386+
err := testCreateNodeBalancer(t, client, f, annotations, nil)
381387
if err == nil || !stderrors.Is(err, firewall.ErrInvalidFWConfig) {
382388
t.Fatalf("expected a %v error, got %v", firewall.ErrInvalidFWConfig, err)
383389
}
@@ -395,7 +401,7 @@ func testCreateNodeBalanceWithBothAllowOrDenyList(t *testing.T, client *linodego
395401
}`,
396402
}
397403

398-
err := testCreateNodeBalancer(t, client, f, annotations)
404+
err := testCreateNodeBalancer(t, client, f, annotations, nil)
399405
if err == nil || !stderrors.Is(err, firewall.ErrInvalidFWConfig) {
400406
t.Fatalf("expected a %v error, got %v", firewall.ErrInvalidFWConfig, err)
401407
}
@@ -410,7 +416,7 @@ func testCreateNodeBalancerWithAllowList(t *testing.T, client *linodego.Client,
410416
}`,
411417
}
412418

413-
err := testCreateNodeBalancer(t, client, f, annotations)
419+
err := testCreateNodeBalancer(t, client, f, annotations, nil)
414420
if err != nil {
415421
t.Fatalf("expected a non-nil error, got %v", err)
416422
}
@@ -425,7 +431,7 @@ func testCreateNodeBalancerWithDenyList(t *testing.T, client *linodego.Client, f
425431
}`,
426432
}
427433

428-
err := testCreateNodeBalancer(t, client, f, annotations)
434+
err := testCreateNodeBalancer(t, client, f, annotations, nil)
429435
if err != nil {
430436
t.Fatalf("expected a non-nil error, got %v", err)
431437
}
@@ -435,7 +441,7 @@ func testCreateNodeBalancerWithFirewall(t *testing.T, client *linodego.Client, f
435441
annotations := map[string]string{
436442
annotations.AnnLinodeCloudFirewallID: "123",
437443
}
438-
err := testCreateNodeBalancer(t, client, f, annotations)
444+
err := testCreateNodeBalancer(t, client, f, annotations, nil)
439445
if err != nil {
440446
t.Fatalf("expected a nil error, got %v", err)
441447
}
@@ -446,12 +452,25 @@ func testCreateNodeBalancerWithInvalidFirewall(t *testing.T, client *linodego.Cl
446452
annotations.AnnLinodeCloudFirewallID: "qwerty",
447453
}
448454
expectedError := "strconv.Atoi: parsing \"qwerty\": invalid syntax"
449-
err := testCreateNodeBalancer(t, client, f, annotations)
455+
err := testCreateNodeBalancer(t, client, f, annotations, nil)
450456
if err.Error() != expectedError {
451457
t.Fatalf("expected a %s error, got %v", expectedError, err)
452458
}
453459
}
454460

461+
func testCreateNodeBalancerWithGlobalTags(t *testing.T, client *linodego.Client, f *fakeAPI) {
462+
original := Options.NodeBalancerTags
463+
defer func() {
464+
Options.NodeBalancerTags = original
465+
}()
466+
Options.NodeBalancerTags = []string{"foobar"}
467+
expectedTags := []string{"linodelb", "foobar", "fake", "test", "yolo"}
468+
err := testCreateNodeBalancer(t, client, f, nil, expectedTags)
469+
if err != nil {
470+
t.Fatalf("expected a nil error, got %v", err)
471+
}
472+
}
473+
455474
func testUpdateLoadBalancerAddNode(t *testing.T, client *linodego.Client, f *fakeAPI) {
456475
svc := &v1.Service{
457476
ObjectMeta: metav1.ObjectMeta{

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func main() {
8282
command.Flags().StringVar(&linode.Options.VPCName, "vpc-name", "", "vpc name whose routes will be managed by route-controller")
8383
command.Flags().StringVar(&linode.Options.LoadBalancerType, "load-balancer-type", "nodebalancer", "configures which type of load-balancing to use for LoadBalancer Services (options: nodebalancer, cilium-bgp)")
8484
command.Flags().StringVar(&linode.Options.BGPNodeSelector, "bgp-node-selector", "", "node selector to use to perform shared IP fail-over with BGP (e.g. cilium-bgp-peering=true")
85+
command.Flags().StringSliceVar(&linode.Options.NodeBalancerTags, "nodebalancer-tags", []string{}, "tags to apply to all Nodebalancers")
8586

8687
// Set static flags
8788
command.Flags().VisitAll(func(fl *pflag.Flag) {

0 commit comments

Comments
 (0)