Fix IPv6 primary node-ip handling

I should have caught `[]string{cfg.NodeIP}[0]` and `[]string{envInfo.NodeIP.String()}[0]` in code review...

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/10521/head
Brad Davidson 2024-07-13 22:57:53 +00:00 committed by Brad Davidson
parent 9841517457
commit 118acabec2
4 changed files with 23 additions and 13 deletions

View File

@ -370,10 +370,9 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to retrieve configuration from server") return nil, errors.Wrap(err, "failed to retrieve configuration from server")
} }
// If the supervisor and externally-facing apiserver are not on the same port, tell the proxy where to find the apiserver. // 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 { if controlConfig.SupervisorPort != controlConfig.HTTPSPort {
isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{envInfo.NodeIP.String()}[0])) isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(envInfo.NodeIP)))
if err := proxy.SetAPIServerPort(controlConfig.HTTPSPort, isIPv6); err != nil { if err := proxy.SetAPIServerPort(controlConfig.HTTPSPort, isIPv6); err != nil {
return nil, errors.Wrapf(err, "failed to set apiserver port to %d", controlConfig.HTTPSPort) return nil, errors.Wrapf(err, "failed to set apiserver port to %d", controlConfig.HTTPSPort)
} }

View File

@ -322,7 +322,7 @@ func createProxyAndValidateToken(ctx context.Context, cfg *cmds.Agent) (proxy.Pr
if err := os.MkdirAll(agentDir, 0700); err != nil { if err := os.MkdirAll(agentDir, 0700); err != nil {
return nil, err return nil, err
} }
isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP.String()}[0])) isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(cfg.NodeIP)))
proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, isIPv6) proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, isIPv6)
if err != nil { if err != nil {

View File

@ -34,8 +34,7 @@ func createRootlessConfig(argsMap map[string]string, controllers map[string]bool
func kubeProxyArgs(cfg *config.Agent) map[string]string { func kubeProxyArgs(cfg *config.Agent) map[string]string {
bindAddress := "127.0.0.1" bindAddress := "127.0.0.1"
isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP}[0])) if utilsnet.IsIPv6(net.ParseIP(cfg.NodeIP)) {
if isIPv6 {
bindAddress = "::1" bindAddress = "::1"
} }
argsMap := map[string]string{ argsMap := map[string]string{
@ -67,8 +66,7 @@ func kubeProxyArgs(cfg *config.Agent) map[string]string {
func kubeletArgs(cfg *config.Agent) map[string]string { func kubeletArgs(cfg *config.Agent) map[string]string {
bindAddress := "127.0.0.1" bindAddress := "127.0.0.1"
isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP}[0])) if utilsnet.IsIPv6(net.ParseIP(cfg.NodeIP)) {
if isIPv6 {
bindAddress = "::1" bindAddress = "::1"
} }
argsMap := map[string]string{ argsMap := map[string]string{

View File

@ -4,6 +4,7 @@
package agent package agent
import ( import (
"net"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -11,8 +12,8 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/util" "github.com/k3s-io/k3s/pkg/util"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/net"
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes" "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
utilsnet "k8s.io/utils/net"
) )
const ( const (
@ -21,8 +22,7 @@ const (
func kubeProxyArgs(cfg *config.Agent) map[string]string { func kubeProxyArgs(cfg *config.Agent) map[string]string {
bindAddress := "127.0.0.1" bindAddress := "127.0.0.1"
_, IPv6only, _ := util.GetFirstString([]string{cfg.NodeIP}) if utilsnet.IsIPv6(net.ParseIP(cfg.NodeIP)) {
if IPv6only {
bindAddress = "::1" bindAddress = "::1"
} }
argsMap := map[string]string{ argsMap := map[string]string{
@ -95,9 +95,22 @@ func kubeletArgs(cfg *config.Agent) map[string]string {
if cfg.NodeName != "" { if cfg.NodeName != "" {
argsMap["hostname-override"] = cfg.NodeName argsMap["hostname-override"] = cfg.NodeName
} }
defaultIP, err := net.ChooseHostInterface()
if err != nil || defaultIP.String() != cfg.NodeIP { // If the embedded CCM is disabled, don't assume that dual-stack node IPs are safe.
argsMap["node-ip"] = cfg.NodeIP // When using an external CCM, the user wants dual-stack node IPs, they will need to set the node-ip kubelet arg directly.
// This should be fine since most cloud providers have their own way of finding node IPs that doesn't depend on the kubelet
// setting them.
if cfg.DisableCCM {
dualStack, err := utilsnet.IsDualStackIPs(cfg.NodeIPs)
if err == nil && !dualStack {
argsMap["node-ip"] = cfg.NodeIP
}
} else {
// Cluster is using the embedded CCM, we know that the feature-gate will be enabled there as well.
argsMap["feature-gates"] = util.AddFeatureGate(argsMap["feature-gates"], "CloudDualStackNodeIPs=true")
if nodeIPs := util.JoinIPs(cfg.NodeIPs); nodeIPs != "" {
argsMap["node-ip"] = util.JoinIPs(cfg.NodeIPs)
}
} }
argsMap["node-labels"] = strings.Join(cfg.NodeLabels, ",") argsMap["node-labels"] = strings.Join(cfg.NodeLabels, ",")