Commit 095ecdb0 authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Fix issue with local traffic policy for single-stack services on dual-stack nodes.

Just enable IP forwarding for all address families regardless of service address families. Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent e8950a0a
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/util/retry" "k8s.io/client-go/util/retry"
...@@ -320,10 +321,8 @@ func (k *k3s) patchStatus(svc *core.Service, previousStatus, newStatus *core.Loa ...@@ -320,10 +321,8 @@ func (k *k3s) patchStatus(svc *core.Service, previousStatus, newStatus *core.Loa
// If at least one node has External IPs available, only external IPs are returned. // If at least one node has External IPs available, only external IPs are returned.
// If no nodes have External IPs set, the Internal IPs of all nodes running pods are returned. // If no nodes have External IPs set, the Internal IPs of all nodes running pods are returned.
func (k *k3s) podIPs(pods []*core.Pod, svc *core.Service, readyNodes map[string]bool) ([]string, error) { func (k *k3s) podIPs(pods []*core.Pod, svc *core.Service, readyNodes map[string]bool) ([]string, error) {
// Go doesn't have sets so we stuff things into a map of bools and then get lists of keys extIPs := sets.Set[string]{}
// to determine the unique set of IPs in use by pods. intIPs := sets.Set[string]{}
extIPs := map[string]bool{}
intIPs := map[string]bool{}
for _, pod := range pods { for _, pod := range pods {
if pod.Spec.NodeName == "" || pod.Status.PodIP == "" { if pod.Spec.NodeName == "" || pod.Status.PodIP == "" {
...@@ -345,25 +344,18 @@ func (k *k3s) podIPs(pods []*core.Pod, svc *core.Service, readyNodes map[string] ...@@ -345,25 +344,18 @@ func (k *k3s) podIPs(pods []*core.Pod, svc *core.Service, readyNodes map[string]
for _, addr := range node.Status.Addresses { for _, addr := range node.Status.Addresses {
if addr.Type == core.NodeExternalIP { if addr.Type == core.NodeExternalIP {
extIPs[addr.Address] = true extIPs.Insert(addr.Address)
} else if addr.Type == core.NodeInternalIP { } else if addr.Type == core.NodeInternalIP {
intIPs[addr.Address] = true intIPs.Insert(addr.Address)
} }
} }
} }
keys := func(addrs map[string]bool) (ips []string) {
for k := range addrs {
ips = append(ips, k)
}
return ips
}
var ips []string var ips []string
if len(extIPs) > 0 { if extIPs.Len() > 0 {
ips = keys(extIPs) ips = extIPs.UnsortedList()
} else { } else {
ips = keys(intIPs) ips = intIPs.UnsortedList()
} }
ips, err := filterByIPFamily(ips, svc) ips, err := filterByIPFamily(ips, svc)
...@@ -443,18 +435,11 @@ func (k *k3s) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) { ...@@ -443,18 +435,11 @@ func (k *k3s) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) {
} }
sourceRanges := strings.Join(sourceRangesSet.StringSlice(), ",") sourceRanges := strings.Join(sourceRangesSet.StringSlice(), ",")
var sysctls []core.Sysctl
for _, ipFamily := range svc.Spec.IPFamilies { for _, ipFamily := range svc.Spec.IPFamilies {
switch ipFamily { if ipFamily == core.IPv6Protocol && sourceRanges == "0.0.0.0/0" {
case core.IPv4Protocol:
sysctls = append(sysctls, core.Sysctl{Name: "net.ipv4.ip_forward", Value: "1"})
case core.IPv6Protocol:
sysctls = append(sysctls, core.Sysctl{Name: "net.ipv6.conf.all.forwarding", Value: "1"})
// The upstream default load-balancer source range only includes IPv4, even if the service is IPv6-only or dual-stack. // 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. // If using the default range, and IPv6 is enabled, also allow IPv6.
if sourceRanges == "0.0.0.0/0" { sourceRanges += ",::/0"
sourceRanges += ",::/0"
}
} }
} }
...@@ -490,7 +475,10 @@ func (k *k3s) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) { ...@@ -490,7 +475,10 @@ func (k *k3s) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) {
ServiceAccountName: "svclb", ServiceAccountName: "svclb",
AutomountServiceAccountToken: utilsptr.To(false), AutomountServiceAccountToken: utilsptr.To(false),
SecurityContext: &core.PodSecurityContext{ SecurityContext: &core.PodSecurityContext{
Sysctls: sysctls, Sysctls: []core.Sysctl{
{Name: "net.ipv4.ip_forward", Value: "1"},
{Name: "net.ipv6.conf.all.forwarding", Value: "1"},
},
}, },
Tolerations: []core.Toleration{ Tolerations: []core.Toleration{
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment