@@ -2,15 +2,16 @@ package loadbalancer
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"net"
7
6
"net/url"
8
7
"os"
9
8
"path/filepath"
9
+ "strconv"
10
10
"strings"
11
11
"time"
12
12
13
13
"github.com/inetaf/tcpproxy"
14
+ "github.com/k3s-io/k3s/pkg/util"
14
15
"github.com/k3s-io/k3s/pkg/util/metrics"
15
16
"github.com/k3s-io/k3s/pkg/version"
16
17
"github.com/sirupsen/logrus"
@@ -40,14 +41,11 @@ var (
40
41
// New contstructs a new LoadBalancer instance. The default server URL, and
41
42
// currently active servers, are stored in a file within the dataDir.
42
43
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"
45
45
if isIPv6 {
46
- localAddress = fmt .Sprintf ("[::1]:%d" , lbServerPort )
47
- } else {
48
- localAddress = fmt .Sprintf ("127.0.0.1:%d" , lbServerPort )
46
+ bindAddress = "::1"
49
47
}
50
- listener , err := config . Listen (ctx , "tcp" , localAddress )
48
+ listener , err := util . ListenWithLoopback (ctx , bindAddress , strconv . Itoa ( lbServerPort ) )
51
49
defer func () {
52
50
if _err != nil {
53
51
logrus .Warnf ("Error starting load balancer: %s" , _err )
@@ -67,11 +65,11 @@ func New(ctx context.Context, dataDir, serviceName, defaultServerURL string, lbS
67
65
68
66
// Set explicit port from scheme
69
67
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")
75
73
}
76
74
}
77
75
0 commit comments