Skip to content

Commit

Permalink
Fix ipv6 sysctl required by non-ipv6 LoadBalancer service
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Jul 22, 2024
1 parent 97d00b1 commit 1f601d7
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions pkg/cloudprovider/servicelb.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,31 @@ func (k *k3s) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) {
return nil, err
}
sourceRanges := strings.Join(sourceRangesSet.StringSlice(), ",")
securityContext := &core.PodSecurityContext{}

for _, ipFamily := range svc.Spec.IPFamilies {
if ipFamily == core.IPv6Protocol && sourceRanges == "0.0.0.0/0" {
// The upstream default load-balancer source range only includes IPv4, even if the service is IPv6-only or dual-stack.
// If using the default range, and IPv6 is enabled, also allow IPv6.
sourceRanges += ",::/0"
switch ipFamily {
case core.IPv4Protocol:
securityContext.Sysctls = append(securityContext.Sysctls, core.Sysctl{Name: "net.ipv4.ip_forward", Value: "1"})
case core.IPv6Protocol:
securityContext.Sysctls = append(securityContext.Sysctls, core.Sysctl{Name: "net.ipv6.conf.all.forwarding", Value: "1"})
if sourceRanges == "0.0.0.0/0" {
// The upstream default load-balancer source range only includes IPv4, even if the service is IPv6-only or dual-stack.
// If using the default range, and IPv6 is enabled, also allow IPv6.
sourceRanges += ",::/0"
}
}
}

// When the service has requested only local traffic, we use node IPs and the
// service NodePorts as the destination, instead of the service ClusterIP and ports.
// As we don't know what address families all the nodes support, and don't want to
// go figure that out every time we update the daemonset, we assume they support
// dual-stack operation, even if the service itself is not dual-stack.
if localTraffic {
securityContext.Sysctls = []core.Sysctl{
{Name: "net.ipv4.ip_forward", Value: "1"},
{Name: "net.ipv6.conf.all.forwarding", Value: "1"},
}
}

Expand Down Expand Up @@ -478,12 +497,7 @@ func (k *k3s) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) {
PriorityClassName: priorityClassName,
ServiceAccountName: "svclb",
AutomountServiceAccountToken: utilsptr.To(false),
SecurityContext: &core.PodSecurityContext{
Sysctls: []core.Sysctl{
{Name: "net.ipv4.ip_forward", Value: "1"},
{Name: "net.ipv6.conf.all.forwarding", Value: "1"},
},
},
SecurityContext: securityContext,
Tolerations: []core.Toleration{
{
Key: util.MasterRoleLabelKey,
Expand Down

0 comments on commit 1f601d7

Please sign in to comment.