Skip to content

Commit be8cb0b

Browse files
committed
Refactor NodeBalancer subnet handling to support IPv6 backends and update related tests
1 parent 32239c7 commit be8cb0b

2 files changed

Lines changed: 68 additions & 44 deletions

File tree

cloud/linode/loadbalancers.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -484,28 +484,13 @@ func (l *loadbalancers) updateNodeBalancer(
484484
klog.Infof("No preexisting nodebalancer for port %v found.", port.Port)
485485
}
486486

487+
useIPv6Backends := resolveIPv6NodeBalancerBackendState(service)
487488
// Add all of the Nodes to the config
488-
subnetID := 0
489-
if options.Options.NodeBalancerBackendIPv4SubnetID != 0 {
490-
subnetID = options.Options.NodeBalancerBackendIPv4SubnetID
491-
}
492-
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range]
493-
if ok {
494-
if err = validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil {
495-
return err
496-
}
497-
}
498-
if len(options.Options.VPCNames) > 0 && !options.Options.DisableNodeBalancerVPCBackends {
499-
var id int
500-
id, err = l.getSubnetIDForSVC(ctx, service)
501-
if err != nil {
502-
sentry.CaptureError(ctx, err)
503-
return fmt.Errorf("Error getting subnet ID for service %s: %w", service.Name, err)
504-
}
505-
subnetID = id
489+
subnetID, err := l.getBackendSubnetID(ctx, service, useIPv6Backends)
490+
if err != nil {
491+
sentry.CaptureError(ctx, err)
492+
return fmt.Errorf("Error getting subnet ID for service %s: %w", service.Name, err)
506493
}
507-
508-
useIPv6Backends := resolveIPv6NodeBalancerBackendState(service)
509494
newNBNodes, err := l.buildNodeBalancerConfigNodes(service, nodes, port.NodePort, subnetID, useIPv6Backends, newNBCfg.Protocol, oldNBNodeIDs)
510495
if err != nil {
511496
sentry.CaptureError(ctx, err)
@@ -937,6 +922,7 @@ func (l *loadbalancers) getSubnetIDByVPCAndSubnetNames(ctx context.Context, vpcN
937922

938923
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {
939924
connThrottle := getConnectionThrottle(service)
925+
useIPv6Backends := resolveIPv6NodeBalancerBackendState(service)
940926

941927
label := l.GetLoadBalancerName(ctx, clusterName, service)
942928
tags := l.GetLoadBalancerTags(ctx, clusterName, service)
@@ -950,7 +936,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
950936
Type: nbType,
951937
}
952938

953-
if len(options.Options.VPCNames) > 0 && !options.Options.DisableNodeBalancerVPCBackends {
939+
if !useIPv6Backends && len(options.Options.VPCNames) > 0 && !options.Options.DisableNodeBalancerVPCBackends {
954940
createOpts.VPCs, err = l.getVPCCreateOptions(ctx, service)
955941
if err != nil {
956942
return nil, err
@@ -1151,23 +1137,9 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
11511137
configs := make([]*linodego.NodeBalancerConfigCreateOptions, 0, len(ports))
11521138
useIPv6Backends := resolveIPv6NodeBalancerBackendState(service)
11531139

1154-
subnetID := 0
1155-
if options.Options.NodeBalancerBackendIPv4SubnetID != 0 {
1156-
subnetID = options.Options.NodeBalancerBackendIPv4SubnetID
1157-
}
1158-
// Check for the NodeBalancerBackendIPv4Range annotation
1159-
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range]
1160-
if ok {
1161-
if err := validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil {
1162-
return nil, err
1163-
}
1164-
}
1165-
if len(options.Options.VPCNames) > 0 && !options.Options.DisableNodeBalancerVPCBackends {
1166-
id, err := l.getSubnetIDForSVC(ctx, service)
1167-
if err != nil {
1168-
return nil, err
1169-
}
1170-
subnetID = id
1140+
subnetID, err := l.getBackendSubnetID(ctx, service, useIPv6Backends)
1141+
if err != nil {
1142+
return nil, err
11711143
}
11721144

11731145
for _, port := range ports {
@@ -1221,12 +1193,40 @@ func (l *loadbalancers) buildNodeBalancerNodeConfigRebuildOptions(service *v1.Se
12211193
if protocol != linodego.ProtocolUDP {
12221194
nodeOptions.Mode = "accept"
12231195
}
1224-
if subnetID != 0 {
1196+
if !useIPv6Backends && subnetID != 0 {
12251197
nodeOptions.SubnetID = subnetID
12261198
}
12271199
return nodeOptions, nil
12281200
}
12291201

1202+
func (l *loadbalancers) getBackendSubnetID(ctx context.Context, service *v1.Service, useIPv6Backends bool) (int, error) {
1203+
if useIPv6Backends {
1204+
return 0, nil
1205+
}
1206+
1207+
subnetID := 0
1208+
if options.Options.NodeBalancerBackendIPv4SubnetID != 0 {
1209+
subnetID = options.Options.NodeBalancerBackendIPv4SubnetID
1210+
}
1211+
1212+
backendIPv4Range, ok := service.GetAnnotations()[annotations.NodeBalancerBackendIPv4Range]
1213+
if ok {
1214+
if err := validateNodeBalancerBackendIPv4Range(backendIPv4Range); err != nil {
1215+
return 0, err
1216+
}
1217+
}
1218+
1219+
if len(options.Options.VPCNames) > 0 && !options.Options.DisableNodeBalancerVPCBackends {
1220+
id, err := l.getSubnetIDForSVC(ctx, service)
1221+
if err != nil {
1222+
return 0, err
1223+
}
1224+
subnetID = id
1225+
}
1226+
1227+
return subnetID, nil
1228+
}
1229+
12301230
func resolveIPv6NodeBalancerBackendState(service *v1.Service) bool {
12311231
useIPv6 := getServiceBoolAnnotation(service, annotations.AnnLinodeEnableIPv6Backends)
12321232
if useIPv6 != nil {

cloud/linode/loadbalancers_test.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4280,7 +4280,7 @@ func Test_resolveIPv6NodeBalancerBackendState(t *testing.T) {
42804280
}
42814281
}
42824282

4283-
func Test_buildLoadBalancerRequestPreservesVPCConfigForIPv6Backends(t *testing.T) {
4283+
func Test_buildLoadBalancerRequestOmitsVPCConfigForIPv6Backends(t *testing.T) {
42844284
prevVPCNames := options.Options.VPCNames
42854285
prevSubnetNames := options.Options.SubnetNames
42864286
prevDisableVPC := options.Options.DisableNodeBalancerVPCBackends
@@ -4302,15 +4302,15 @@ func Test_buildLoadBalancerRequestPreservesVPCConfigForIPv6Backends(t *testing.T
43024302
annotations map[string]string
43034303
}{
43044304
{
4305-
name: "service annotation preserves VPC config for IPv6 backends",
4305+
name: "service annotation omits VPC config for IPv6 backends",
43064306
globalFlag: false,
43074307
annotations: map[string]string{
43084308
annotations.AnnLinodeDefaultProtocol: "tcp",
43094309
annotations.AnnLinodeEnableIPv6Backends: "true",
43104310
},
43114311
},
43124312
{
4313-
name: "global flag preserves VPC config for IPv6 backends",
4313+
name: "global flag omits VPC config for IPv6 backends",
43144314
globalFlag: true,
43154315
annotations: map[string]string{
43164316
annotations.AnnLinodeDefaultProtocol: "tcp",
@@ -4404,12 +4404,15 @@ func Test_buildLoadBalancerRequestPreservesVPCConfigForIPv6Backends(t *testing.T
44044404
if err := json.Unmarshal([]byte(req.Body), &createOpts); err != nil {
44054405
t.Fatalf("unable to unmarshal create request body %#v, error: %#v", req.Body, err)
44064406
}
4407-
if len(createOpts.VPCs) != 1 || createOpts.VPCs[0].SubnetID == 0 {
4408-
t.Fatalf("expected nodebalancer create request to preserve VPC config, got %#v", createOpts.VPCs)
4407+
if len(createOpts.VPCs) != 0 {
4408+
t.Fatalf("expected nodebalancer create request to omit VPC config for IPv6 backends, got %#v", createOpts.VPCs)
44094409
}
44104410
if len(createOpts.Configs) != 1 || len(createOpts.Configs[0].Nodes) != 1 {
44114411
t.Fatalf("expected a single nodebalancer config with one backend node, got %#v", createOpts.Configs)
44124412
}
4413+
if createOpts.Configs[0].Nodes[0].SubnetID != 0 {
4414+
t.Fatalf("expected IPv6 backend node to omit subnet ID, got %#v", createOpts.Configs[0].Nodes[0])
4415+
}
44134416
host, _, hostPortErr := net.SplitHostPort(createOpts.Configs[0].Nodes[0].Address)
44144417
if hostPortErr != nil {
44154418
t.Fatal(hostPortErr)
@@ -4421,6 +4424,27 @@ func Test_buildLoadBalancerRequestPreservesVPCConfigForIPv6Backends(t *testing.T
44214424
}
44224425
}
44234426

4427+
func Test_buildNodeBalancerNodeConfigRebuildOptionsOmitsSubnetIDForIPv6Backends(t *testing.T) {
4428+
lb := &loadbalancers{}
4429+
service := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "svc-test", Namespace: "default"}}
4430+
node := &v1.Node{
4431+
ObjectMeta: metav1.ObjectMeta{
4432+
Name: "node-1",
4433+
Annotations: map[string]string{
4434+
annotations.AnnLinodeNodePublicIPv6: "2600:3c06:e727:1::1/128",
4435+
},
4436+
},
4437+
}
4438+
4439+
opts, err := lb.buildNodeBalancerNodeConfigRebuildOptions(service, node, 30000, 101, true, linodego.ProtocolTCP)
4440+
if err != nil {
4441+
t.Fatal(err)
4442+
}
4443+
if opts.SubnetID != 0 {
4444+
t.Fatalf("expected IPv6 backend rebuild options to omit subnet ID, got %#v", opts)
4445+
}
4446+
}
4447+
44244448
func Test_formatNodeBalancerBackendAddress(t *testing.T) {
44254449
if got := formatNodeBalancerBackendAddress("192.168.0.10", 30000); got != "192.168.0.10:30000" {
44264450
t.Fatalf("unexpected IPv4 backend address format: %s", got)

0 commit comments

Comments
 (0)