Skip to content

Commit 30684d3

Browse files
committed
Fix IPv6 handling for loadbalancer addresses
Signed-off-by: Brad Davidson <[email protected]>
1 parent 0136cf2 commit 30684d3

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

pkg/agent/config/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,15 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
456456
if err != nil {
457457
return nil, pkgerrors.WithMessage(err, "failed to retrieve configuration from server")
458458
}
459+
460+
nodeName, nodeIPs, err := util.GetHostnameAndIPs(envInfo.NodeName, envInfo.NodeIP.Value())
461+
if err != nil {
462+
return nil, pkgerrors.WithMessage(err, "failed to get node name and addresses")
463+
}
464+
459465
// If the supervisor and externally-facing apiserver are not on the same port, tell the proxy where to find the apiserver.
460466
if controlConfig.SupervisorPort != controlConfig.HTTPSPort {
461-
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(envInfo.NodeIP.Value())))
462-
if err := proxy.SetAPIServerPort(controlConfig.HTTPSPort, isIPv6); err != nil {
467+
if err := proxy.SetAPIServerPort(controlConfig.HTTPSPort, utilsnet.IsIPv6(nodeIPs[0])); err != nil {
463468
return nil, pkgerrors.WithMessagef(err, "failed to set apiserver port to %d", controlConfig.HTTPSPort)
464469
}
465470
}
@@ -499,11 +504,6 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
499504
newNodePasswordFile := filepath.Join(nodeConfigPath, "password")
500505
upgradeOldNodePasswordPath(oldNodePasswordFile, newNodePasswordFile)
501506

502-
nodeName, nodeIPs, err := util.GetHostnameAndIPs(envInfo.NodeName, envInfo.NodeIP.Value())
503-
if err != nil {
504-
return nil, err
505-
}
506-
507507
// If there is a VPN, we must overwrite NodeIP and flannel interface
508508
var vpnInfo vpn.VPNInfo
509509
if envInfo.VPNAuth != "" {

pkg/agent/loadbalancer/loadbalancer.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package loadbalancer
22

33
import (
44
"context"
5-
"fmt"
65
"net"
76
"net/url"
87
"os"
98
"path/filepath"
9+
"strconv"
1010
"strings"
1111
"time"
1212

1313
"github.com/inetaf/tcpproxy"
14+
"github.com/k3s-io/k3s/pkg/util"
1415
"github.com/k3s-io/k3s/pkg/util/metrics"
1516
"github.com/k3s-io/k3s/pkg/version"
1617
"github.com/sirupsen/logrus"
@@ -40,14 +41,11 @@ var (
4041
// New contstructs a new LoadBalancer instance. The default server URL, and
4142
// currently active servers, are stored in a file within the dataDir.
4243
func New(ctx context.Context, dataDir, serviceName, defaultServerURL string, lbServerPort int, isIPv6 bool) (_lb *LoadBalancer, _err error) {
43-
config := net.ListenConfig{Control: reusePort}
44-
var localAddress string
44+
bindAddress := "127.0.0.1"
4545
if isIPv6 {
46-
localAddress = fmt.Sprintf("[::1]:%d", lbServerPort)
47-
} else {
48-
localAddress = fmt.Sprintf("127.0.0.1:%d", lbServerPort)
46+
bindAddress = "::1"
4947
}
50-
listener, err := config.Listen(ctx, "tcp", localAddress)
48+
listener, err := util.ListenWithLoopback(ctx, bindAddress, strconv.Itoa(lbServerPort))
5149
defer func() {
5250
if _err != nil {
5351
logrus.Warnf("Error starting load balancer: %s", _err)
@@ -67,11 +65,11 @@ func New(ctx context.Context, dataDir, serviceName, defaultServerURL string, lbS
6765

6866
// Set explicit port from scheme
6967
if serverURL.Port() == "" {
70-
if strings.ToLower(serverURL.Scheme) == "http" {
71-
serverURL.Host += ":80"
72-
}
73-
if strings.ToLower(serverURL.Scheme) == "https" {
74-
serverURL.Host += ":443"
68+
switch strings.ToLower(serverURL.Scheme) {
69+
case "http":
70+
serverURL.Host = net.JoinHostPort(serverURL.Hostname(), "80")
71+
case "https":
72+
serverURL.Host = net.JoinHostPort(serverURL.Hostname(), "443")
7573
}
7674
}
7775

pkg/agent/run.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,13 @@ func createProxyAndValidateToken(ctx context.Context, cfg *cmds.Agent) (proxy.Pr
339339
if err := os.MkdirAll(agentDir, 0700); err != nil {
340340
return nil, err
341341
}
342-
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(cfg.NodeIP.Value())))
343342

344-
proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, isIPv6)
343+
_, nodeIPs, err := util.GetHostnameAndIPs(cfg.NodeName, cfg.NodeIP.Value())
344+
if err != nil {
345+
return nil, pkgerrors.WithMessage(err, "failed to get node name and addresses")
346+
}
347+
348+
proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, utilsnet.IsIPv6(nodeIPs[0]))
345349
if err != nil {
346350
return nil, err
347351
}

pkg/cluster/cluster.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ package cluster
22

33
import (
44
"context"
5+
"net"
56
"net/url"
67
"strings"
78
"time"
89

10+
"github.com/k3s-io/k3s/pkg/cli/cmds"
911
"github.com/k3s-io/k3s/pkg/clientaccess"
1012
"github.com/k3s-io/k3s/pkg/cluster/managed"
1113
"github.com/k3s-io/k3s/pkg/daemons/config"
1214
"github.com/k3s-io/k3s/pkg/daemons/executor"
1315
"github.com/k3s-io/k3s/pkg/etcd"
1416
"github.com/k3s-io/k3s/pkg/metrics"
17+
"github.com/k3s-io/k3s/pkg/util"
1518
"github.com/k3s-io/kine/pkg/endpoint"
1619
pkgerrors "github.com/pkg/errors"
1720
"github.com/sirupsen/logrus"
@@ -116,8 +119,13 @@ func (c *Cluster) startEtcdProxy(ctx context.Context) error {
116119
if err != nil {
117120
return err
118121
}
119-
defaultURL.Host = defaultURL.Hostname() + ":2379"
120-
etcdProxy, err := etcd.NewETCDProxy(ctx, c.config.SupervisorPort, c.config.DataDir, defaultURL.String(), utilsnet.IsIPv6CIDR(c.config.ServiceIPRanges[0]))
122+
_, nodeIPs, err := util.GetHostnameAndIPs(cmds.AgentConfig.NodeName, cmds.AgentConfig.NodeIP.Value())
123+
if err != nil {
124+
pkgerrors.WithMessage(err, "failed to get node name and addresses")
125+
}
126+
127+
defaultURL.Host = net.JoinHostPort(defaultURL.Hostname(), "2379")
128+
etcdProxy, err := etcd.NewETCDProxy(ctx, c.config.SupervisorPort, c.config.DataDir, defaultURL.String(), utilsnet.IsIPv6(nodeIPs[0]))
121129
if err != nil {
122130
return err
123131
}

pkg/daemons/agent/agent.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ func writeKubeletConfig(path string, config *kubeletconfig.KubeletConfiguration)
163163

164164
func defaultKubeletConfig(cfg *daemonconfig.Agent) (*kubeletconfig.KubeletConfiguration, error) {
165165
bindAddress := "127.0.0.1"
166-
isIPv6 := utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP}[0]))
167-
if isIPv6 {
166+
if utilsnet.IsIPv6(net.ParseIP([]string{cfg.NodeIP}[0])) {
168167
bindAddress = "::1"
169168
}
170169

0 commit comments

Comments
 (0)