From b0aa2c718ccad1b03a16c8b86de8cb9ae4ae0603 Mon Sep 17 00:00:00 2001 From: Roberto Bonafiglia Date: Tue, 10 Oct 2023 12:34:54 +0200 Subject: [PATCH] Use IPv6 in case is the first configured IP with dualstack Signed-off-by: Roberto Bonafiglia --- pkg/agent/config/config.go | 2 +- pkg/agent/run.go | 2 +- pkg/daemons/agent/agent_linux.go | 19 +++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/agent/config/config.go b/pkg/agent/config/config.go index b3f107324d24..62eb616d4857 100644 --- a/pkg/agent/config/config.go +++ b/pkg/agent/config/config.go @@ -353,7 +353,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N // If the supervisor and externally-facing apiserver are not on the same port, tell the proxy where to find the apiserver. if controlConfig.SupervisorPort != controlConfig.HTTPSPort { - _, isIPv6, _ := util.GetFirstString([]string{envInfo.NodeIP.String()}) + isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{envInfo.NodeIP.String()}[0])) if err := proxy.SetAPIServerPort(ctx, controlConfig.HTTPSPort, isIPv6); err != nil { return nil, errors.Wrapf(err, "failed to setup access to API Server port %d on at %s", controlConfig.HTTPSPort, proxy.SupervisorURL()) } diff --git a/pkg/agent/run.go b/pkg/agent/run.go index 58fd2ed52e3a..7b8a9c6ade6e 100644 --- a/pkg/agent/run.go +++ b/pkg/agent/run.go @@ -277,7 +277,7 @@ func createProxyAndValidateToken(ctx context.Context, cfg *cmds.Agent) (proxy.Pr if err := os.MkdirAll(agentDir, 0700); err != nil { return nil, err } - _, isIPv6, _ := util.GetFirstString([]string{cfg.NodeIP.String()}) + isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP.String()}[0])) proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, isIPv6) if err != nil { diff --git a/pkg/daemons/agent/agent_linux.go b/pkg/daemons/agent/agent_linux.go index ead7507aee73..06df2106449d 100644 --- a/pkg/daemons/agent/agent_linux.go +++ b/pkg/daemons/agent/agent_linux.go @@ -4,6 +4,7 @@ package agent import ( + "net" "os" "path/filepath" "strings" @@ -13,8 +14,8 @@ import ( "github.com/k3s-io/k3s/pkg/util" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" - "k8s.io/apimachinery/pkg/util/net" "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes" + utilsnet "k8s.io/utils/net" ) const socketPrefix = "unix://" @@ -32,8 +33,8 @@ func createRootlessConfig(argsMap map[string]string, controllers map[string]bool func kubeProxyArgs(cfg *config.Agent) map[string]string { bindAddress := "127.0.0.1" - _, IPv6only, _ := util.GetFirstString([]string{cfg.NodeIP}) - if IPv6only { + isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP}[0])) + if isIPv6 { bindAddress = "::1" } argsMap := map[string]string{ @@ -53,8 +54,8 @@ func kubeProxyArgs(cfg *config.Agent) map[string]string { func kubeletArgs(cfg *config.Agent) map[string]string { bindAddress := "127.0.0.1" - _, IPv6only, _ := util.GetFirstString([]string{cfg.NodeIP}) - if IPv6only { + isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP}[0])) + if isIPv6 { bindAddress = "::1" } argsMap := map[string]string{ @@ -122,9 +123,11 @@ func kubeletArgs(cfg *config.Agent) map[string]string { if cfg.NodeName != "" { argsMap["hostname-override"] = cfg.NodeName } - defaultIP, err := net.ChooseHostInterface() - if err != nil || defaultIP.String() != cfg.NodeIP { - argsMap["node-ip"] = cfg.NodeIP + if nodeIPs := util.JoinIPs(cfg.NodeIPs); nodeIPs != "" { + dualStack, err := utilsnet.IsDualStackIPs(cfg.NodeIPs) + if err == nil && !dualStack { + argsMap["node-ip"] = cfg.NodeIP + } } kubeletRoot, runtimeRoot, controllers := cgroups.CheckCgroups() if !controllers["cpu"] {