Skip to content

Commit 91e50d0

Browse files
committed
fix udp loadbalancer on mac
Change-Id: I24c5e8334af622feb4dedb13b63e0446c1747329
1 parent 78fa17a commit 91e50d0

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

pkg/loadbalancer/tunnel.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (t *tunnelManager) removeTunnels(containerName string) error {
9999
// tunnel listens on localIP:localPort and proxies the connection to remoteIP:remotePort
100100
type tunnel struct {
101101
listener net.Listener
102+
udpConn *net.UDPConn
102103
localIP string
103104
localPort string
104105
protocol string
@@ -118,35 +119,55 @@ func NewTunnel(localIP, localPort, protocol, remoteIP, remotePort string) *tunne
118119

119120
func (t *tunnel) Start() error {
120121
klog.Infof("Starting tunnel on %s", net.JoinHostPort(t.localIP, t.localPort))
121-
ln, err := net.Listen(t.protocol, net.JoinHostPort(t.localIP, t.localPort))
122-
if err != nil {
123-
return err
124-
}
125-
126-
t.listener = ln
127-
128-
go func() {
129-
for {
130-
conn, err := ln.Accept()
122+
if t.protocol == "udp" {
123+
localAddrStr := net.JoinHostPort(t.localIP, t.localPort)
124+
udpAddr, err := net.ResolveUDPAddr("udp", localAddrStr)
125+
if err != nil {
126+
return err
127+
}
128+
conn, err := net.ListenUDP("udp", udpAddr)
129+
t.udpConn = conn
130+
go func() {
131+
err = t.handleConnection(conn)
131132
if err != nil {
132-
klog.Infof("unexpected error listening: %v", err)
133-
return
134-
} else {
135-
go func() {
136-
err := t.handleConnection(conn)
137-
if err != nil {
138-
klog.Infof("unexpected error on connection: %v", err)
139-
}
140-
}()
133+
klog.Infof("unexpected error on connection: %v", err)
141134
}
135+
}()
136+
137+
} else {
138+
ln, err := net.Listen(t.protocol, net.JoinHostPort(t.localIP, t.localPort))
139+
if err != nil {
140+
return err
142141
}
143-
}()
142+
143+
t.listener = ln
144+
145+
go func() {
146+
for {
147+
conn, err := ln.Accept()
148+
if err != nil {
149+
klog.Infof("unexpected error listening: %v", err)
150+
return
151+
} else {
152+
go func() {
153+
err := t.handleConnection(conn)
154+
if err != nil {
155+
klog.Infof("unexpected error on connection: %v", err)
156+
}
157+
}()
158+
}
159+
}
160+
}()
161+
}
144162
return nil
145163
}
146164

147165
func (t *tunnel) Stop() error {
148166
if t.listener != nil {
149-
return t.listener.Close()
167+
_ = t.listener.Close()
168+
}
169+
if t.udpConn != nil {
170+
_ = t.udpConn.Close()
150171
}
151172
return nil
152173
}

0 commit comments

Comments
 (0)