Skip to content

Commit b105be5

Browse files
glennprattRahul Sharma
authored andcommitted
Add option for global NodeBalancer tags
1 parent 162a835 commit b105be5

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

cloud/linode/cloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var Options struct {
4242
BGPNodeSelector string
4343
IpHolderSuffix string
4444
LinodeExternalNetwork *net.IPNet
45+
NodeBalancerTags []string
4546
}
4647

4748
type linodeCloud struct {

cloud/linode/loadbalancers.go

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

615+
tags = append(tags, Options.NodeBalancerTags...)
616+
615617
tagStr, ok := service.GetAnnotations()[annotations.AnnLinodeLoadBalancerTags]
616618
if ok {
617619
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{

deploy/chart/templates/daemonset.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ spec:
6565
{{- end}}
6666
- --load-balancer-type={{ required "A valid .Values.sharedIPLoadBalancing.loadBalancerType is required for shared IP load-balancing" .Values.sharedIPLoadBalancing.loadBalancerType }}
6767
{{- end }}
68+
{{- if .Values.nodeBalancerTags }}
69+
- --nodebalancer-tags={{ join " " .Values.nodeBalancerTags }}
70+
{{- end }}
6871
volumeMounts:
6972
- mountPath: /etc/kubernetes
7073
name: k8s

deploy/chart/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ env:
6464
# - name: EXAMPLE_ENV_VAR
6565
# value: "true"
6666

67+
# Linode tags to apply to all NodeBalancers
68+
nodeBalancerTags: []
69+
6770
# This section adds the ability to pass volumes to the CCM DaemonSet
6871
volumes:
6972
# - name: test-volume
@@ -74,3 +77,4 @@ volumes:
7477
volumeMounts:
7578
# - mountPath: /tmp/
7679
# name: test-volume
80+

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func main() {
8686
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)")
8787
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")
8888
command.Flags().StringVar(&linode.Options.IpHolderSuffix, "ip-holder-suffix", "", "suffix to append to the ip holder name when using shared IP fail-over with BGP (e.g. ip-holder-suffix=my-cluster-name")
89+
command.Flags().StringSliceVar(&linode.Options.NodeBalancerTags, "nodebalancer-tags", []string{}, "Linode tags to apply to all NodeBalancers")
8990

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

0 commit comments

Comments
 (0)