|
4 | 4 | "fmt" |
5 | 5 | "io" |
6 | 6 | "net" |
| 7 | + "strings" |
7 | 8 | "sync" |
8 | 9 |
|
9 | 10 | "k8s.io/klog/v2" |
@@ -49,7 +50,12 @@ func (t *tunnelManager) setupTunnels(containerName string) error { |
49 | 50 | defer t.mu.Unlock() |
50 | 51 | // There is one IP per Service and a tunnel per Service Port |
51 | 52 | for containerPort, hostPort := range portmaps { |
52 | | - tun := NewTunnel(ipv4, containerPort, "localhost", hostPort) |
| 53 | + parts := strings.Split(containerPort, "/") |
| 54 | + if len(parts) != 2 { |
| 55 | + return fmt.Errorf("expected format port/protocol for container port, got %s", containerPort) |
| 56 | + } |
| 57 | + |
| 58 | + tun := NewTunnel(ipv4, parts[0], parts[1], "localhost", hostPort) |
53 | 59 | // TODO check if we can leak tunnels |
54 | 60 | err = tun.Start() |
55 | 61 | if err != nil { |
@@ -95,22 +101,24 @@ type tunnel struct { |
95 | 101 | listener net.Listener |
96 | 102 | localIP string |
97 | 103 | localPort string |
| 104 | + protocol string |
98 | 105 | remoteIP string // address:Port |
99 | 106 | remotePort string |
100 | 107 | } |
101 | 108 |
|
102 | | -func NewTunnel(localIP, localPort, remoteIP, remotePort string) *tunnel { |
| 109 | +func NewTunnel(localIP, localPort, protocol, remoteIP, remotePort string) *tunnel { |
103 | 110 | return &tunnel{ |
104 | 111 | localIP: localIP, |
105 | 112 | localPort: localPort, |
| 113 | + protocol: protocol, |
106 | 114 | remoteIP: remoteIP, |
107 | 115 | remotePort: remotePort, |
108 | 116 | } |
109 | 117 | } |
110 | 118 |
|
111 | 119 | func (t *tunnel) Start() error { |
112 | 120 | klog.Infof("Starting tunnel on %s", net.JoinHostPort(t.localIP, t.localPort)) |
113 | | - ln, err := net.Listen("tcp", net.JoinHostPort(t.localIP, t.localPort)) |
| 121 | + ln, err := net.Listen(t.protocol, net.JoinHostPort(t.localIP, t.localPort)) |
114 | 122 | if err != nil { |
115 | 123 | return err |
116 | 124 | } |
@@ -144,7 +152,7 @@ func (t *tunnel) Stop() error { |
144 | 152 | } |
145 | 153 |
|
146 | 154 | func (t *tunnel) handleConnection(local net.Conn) error { |
147 | | - remote, err := net.Dial("tcp", net.JoinHostPort(t.remoteIP, t.remotePort)) |
| 155 | + remote, err := net.Dial(t.protocol, net.JoinHostPort(t.remoteIP, t.remotePort)) |
148 | 156 | if err != nil { |
149 | 157 | return fmt.Errorf("can't connect to server %q: %v", net.JoinHostPort(t.remoteIP, t.remotePort), err) |
150 | 158 | } |
|
0 commit comments