Skip to content

Commit baf1b10

Browse files
Merge pull request #531 from linode/ipv6-ingress-fix
[fix] the enable-ipv6-ingress annotation to override the global flag if it's set
2 parents a33f2af + 4b77169 commit baf1b10

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

cloud/linode/loadbalancers.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/client-go/tools/clientcmd"
2626
cloudprovider "k8s.io/cloud-provider"
2727
"k8s.io/klog/v2"
28+
"k8s.io/utils/ptr"
2829

2930
"github.com/linode/linode-cloud-controller-manager/cloud/annotations"
3031
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
@@ -622,7 +623,8 @@ func (l *loadbalancers) deleteUnusedConfigs(ctx context.Context, nbConfigs []lin
622623
// shouldPreserveNodeBalancer determines whether a NodeBalancer should be deleted based on the
623624
// service's preserve annotation.
624625
func (l *loadbalancers) shouldPreserveNodeBalancer(service *v1.Service) bool {
625-
return getServiceBoolAnnotation(service, annotations.AnnLinodeLoadBalancerPreserve)
626+
shouldPreserve := getServiceBoolAnnotation(service, annotations.AnnLinodeLoadBalancerPreserve)
627+
return shouldPreserve != nil && *shouldPreserve
626628
}
627629

628630
// EnsureLoadBalancerDeleted deletes the specified loadbalancer if it exists.
@@ -1423,14 +1425,13 @@ func makeLoadBalancerStatus(service *v1.Service, nb *linodego.NodeBalancer) *v1.
14231425
}
14241426

14251427
// Return hostname-only if annotation is set or environment variable is set
1426-
if getServiceBoolAnnotation(service, annotations.AnnLinodeHostnameOnlyIngress) {
1427-
return &v1.LoadBalancerStatus{
1428-
Ingress: []v1.LoadBalancerIngress{ingress},
1429-
}
1430-
}
1431-
1432-
if val := envBoolOptions("LINODE_HOSTNAME_ONLY_INGRESS"); val {
1428+
useHostnameOnly := getServiceBoolAnnotation(service, annotations.AnnLinodeHostnameOnlyIngress)
1429+
if useHostnameOnly == nil {
1430+
val := envBoolOptions("LINODE_HOSTNAME_ONLY_INGRESS")
14331431
klog.Infof("LINODE_HOSTNAME_ONLY_INGRESS: (%v)", val)
1432+
useHostnameOnly = ptr.To(envBoolOptions("LINODE_HOSTNAME_ONLY_INGRESS"))
1433+
}
1434+
if *useHostnameOnly {
14341435
return &v1.LoadBalancerStatus{
14351436
Ingress: []v1.LoadBalancerIngress{ingress},
14361437
}
@@ -1440,11 +1441,14 @@ func makeLoadBalancerStatus(service *v1.Service, nb *linodego.NodeBalancer) *v1.
14401441
klog.V(4).Infof("NodeBalancer (%d) is using frontend VPC address type", nb.ID)
14411442
}
14421443

1443-
// Check for per-service IPv6 annotation first, then fall back to global setting
1444-
useIPv6 := getServiceBoolAnnotation(service, annotations.AnnLinodeEnableIPv6Ingress) || options.Options.EnableIPv6ForLoadBalancers
1444+
// Check for per-service IPv6 annotation first, then fall back to global setting if not set
1445+
useIPv6 := getServiceBoolAnnotation(service, annotations.AnnLinodeEnableIPv6Ingress)
1446+
if useIPv6 == nil {
1447+
useIPv6 = ptr.To(options.Options.EnableIPv6ForLoadBalancers)
1448+
}
14451449

14461450
// When IPv6 is enabled (either per-service or globally), include both IPv4 and IPv6
1447-
if useIPv6 && nb.IPv6 != nil && *nb.IPv6 != "" {
1451+
if *useIPv6 && nb.IPv6 != nil && *nb.IPv6 != "" {
14481452
ingresses := []v1.LoadBalancerIngress{
14491453
{
14501454
Hostname: *nb.Hostname,
@@ -1482,13 +1486,13 @@ func getServiceNn(service *v1.Service) string {
14821486
return fmt.Sprintf("%s/%s", service.Namespace, service.Name)
14831487
}
14841488

1485-
func getServiceBoolAnnotation(service *v1.Service, name string) bool {
1489+
func getServiceBoolAnnotation(service *v1.Service, name string) *bool {
14861490
value, ok := service.GetAnnotations()[name]
14871491
if !ok {
1488-
return false
1492+
return nil
14891493
}
14901494
boolValue, err := strconv.ParseBool(value)
1491-
return err == nil && boolValue
1495+
return ptr.To(err == nil && boolValue)
14921496
}
14931497

14941498
// validateNodeBalancerBackendIPv4Range validates the NodeBalancerBackendIPv4Range

cloud/linode/loadbalancers_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,23 @@ func testMakeLoadBalancerStatusWithIPv6(t *testing.T, client *linodego.Client, _
45644564
annotations.AnnLinodeEnableIPv6Ingress, expectedStatus, status)
45654565
}
45664566

4567+
// Set the global flag to true and set the annotation to false
4568+
options.Options.EnableIPv6ForLoadBalancers = true
4569+
svc.Annotations[annotations.AnnLinodeEnableIPv6Ingress] = "false"
4570+
expectedStatus = &v1.LoadBalancerStatus{
4571+
Ingress: []v1.LoadBalancerIngress{{
4572+
Hostname: hostname,
4573+
IP: ipv4,
4574+
}},
4575+
}
4576+
4577+
// Expect the annotation to take precedence over the global flag, resulting in only the IPv4 address being included
4578+
status = makeLoadBalancerStatus(svc, nb)
4579+
if !reflect.DeepEqual(status, expectedStatus) {
4580+
t.Errorf("expected status with %s=false annotation to be %#v; got %#v",
4581+
annotations.AnnLinodeEnableIPv6Ingress, expectedStatus, status)
4582+
}
4583+
45674584
// Reset the flag to its default value
45684585
options.Options.EnableIPv6ForLoadBalancers = false
45694586
}

0 commit comments

Comments
 (0)