From cac1903d63f471d966cebf8fbd8232e8888d24b6 Mon Sep 17 00:00:00 2001 From: Tarun Chinmai Sekar Date: Sun, 5 Nov 2023 16:42:30 -0800 Subject: [PATCH] Support adding tags to nodebalancers --- cloud/linode/fake_linode_test.go | 1 + cloud/linode/loadbalancers.go | 12 ++++++++++++ cloud/linode/loadbalancers_test.go | 10 +++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index cf3e7d4c..a5d0c879 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -251,6 +251,7 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { Region: nbco.Region, IPv4: &ip, Hostname: &hostname, + Tags: nbco.Tags, } if nbco.ClientConnThrottle != nil { diff --git a/cloud/linode/loadbalancers.go b/cloud/linode/loadbalancers.go index b3e4cda2..a07c388e 100644 --- a/cloud/linode/loadbalancers.go +++ b/cloud/linode/loadbalancers.go @@ -47,6 +47,7 @@ const ( annLinodeNodeBalancerID = "service.beta.kubernetes.io/linode-loadbalancer-nodebalancer-id" annLinodeHostnameOnlyIngress = "service.beta.kubernetes.io/linode-loadbalancer-hostname-only-ingress" + annLinodeLoadBalancerTags = "service.beta.kubernetes.io/linode-loadbalancer-tags" ) type lbNotFoundError struct { @@ -480,15 +481,26 @@ func (l *loadbalancers) getNodeBalancerByID(ctx context.Context, service *v1.Ser return nb, nil } +func (l *loadbalancers) getLoadbalancerTags(ctx context.Context, service *v1.Service) []string { + tagStr, ok := getServiceAnnotation(service, annLinodeLoadBalancerTags) + if ok { + return strings.Split(tagStr, ",") + } else { + return []string{} + } +} + 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) createOpts := linodego.NodeBalancerCreateOptions{ Label: &label, Region: l.zone, ClientConnThrottle: &connThrottle, Configs: configs, + Tags: tags, } return l.client.CreateNodeBalancer(ctx, createOpts) } diff --git a/cloud/linode/loadbalancers_test.go b/cloud/linode/loadbalancers_test.go index f4ad2c6f..8586b53c 100644 --- a/cloud/linode/loadbalancers_test.go +++ b/cloud/linode/loadbalancers_test.go @@ -201,7 +201,8 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI) { Name: randString(10), UID: "foobar123", Annotations: map[string]string{ - annLinodeThrottle: "15", + annLinodeThrottle: "15", + annLinodeLoadBalancerTags: "fake,test,yolo", }, }, Spec: v1.ServiceSpec{ @@ -264,6 +265,13 @@ func testCreateNodeBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI) { t.Logf("actual: %v", nb.ClientConnThrottle) } + expectedTags := []string{"fake", "test", "yolo"} + if !reflect.DeepEqual(nb.Tags, expectedTags) { + t.Error("unexpected Tags") + t.Logf("expected: %v", expectedTags) + t.Logf("actual: %v", nb.Tags) + } + defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc) }() }