-
Notifications
You must be signed in to change notification settings - Fork 148
/
tunnel.go
356 lines (309 loc) · 10.6 KB
/
tunnel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package k8s
// Forked from https://github.com/gruntwork-io/terratest/blob/v0.38.8/modules/k8s/tunnel.go
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"github.com/defenseunicorns/zarf/cli/types"
"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/message"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)
// Global lock to synchronize port selections
var globalMutex sync.Mutex
const (
PodResource = "pod"
SvcResource = "svc"
ZarfRegistry = "REGISTRY"
ZarfLogging = "LOGGING"
ZarfGit = "GIT"
)
const (
PortRegistry = iota + 45001
PortLogging
PortGit
)
// makeLabels is a helper to format a map of label key and value pairs into a single string for use as a selector.
func makeLabels(labels map[string]string) string {
var out []string
for key, value := range labels {
out = append(out, fmt.Sprintf("%s=%s", key, value))
}
return strings.Join(out, ",")
}
// Tunnel is the main struct that configures and manages port forwading tunnels to Kubernetes resources.
type Tunnel struct {
out io.Writer
autoOpen bool
localPort int
remotePort int
namespace string
resourceType string
resourceName string
urlSuffix string
stopChan chan struct{}
readyChan chan struct{}
spinner *message.Spinner
}
// NewTunnel will create a new Tunnel struct
// Note that if you use 0 for the local port, an open port on the host system
// will be selected automatically, and the Tunnel struct will be updated with the selected port.
func NewTunnel(namespace, resourceType, resourceName string, local, remote int) *Tunnel {
message.Debugf("tunnel.NewTunnel(%s, %s, %s, %v, %v)", namespace, resourceType, resourceName, local, remote)
return &Tunnel{
out: ioutil.Discard,
localPort: local,
remotePort: remote,
namespace: namespace,
resourceType: resourceType,
resourceName: resourceName,
stopChan: make(chan struct{}, 1),
readyChan: make(chan struct{}, 1),
}
}
func NewZarfTunnel() *Tunnel {
return NewTunnel(ZarfNamespace, SvcResource, "", 0, 0)
}
func (tunnel *Tunnel) EnableAutoOpen() {
tunnel.autoOpen = true
}
func (tunnel *Tunnel) AddSpinner(spinner *message.Spinner) {
tunnel.spinner = spinner
}
func (tunnel *Tunnel) Connect(target string, blocking bool) {
message.Debugf("tunnel.Connect(%s, %v)", target, blocking)
switch strings.ToUpper(target) {
case ZarfRegistry:
tunnel.resourceName = "zarf-docker-registry"
tunnel.localPort = PortRegistry
tunnel.remotePort = 5000
case ZarfLogging:
tunnel.resourceName = "zarf-loki-stack-grafana"
tunnel.localPort = PortLogging
tunnel.remotePort = 3000
case ZarfGit:
tunnel.resourceName = "zarf-gitea-http"
tunnel.localPort = PortGit
tunnel.remotePort = 3000
default:
if target != "" {
if err := tunnel.checkForZarfConnectLabel(target); err != nil {
message.Errorf(err, "Problem looking for a zarf connect label in the cluster")
}
}
if tunnel.resourceName == "" {
message.Fatalf(nil, "Ensure a resource name is provided")
}
if tunnel.remotePort < 1 {
message.Fatal(nil, "A remote port must be specified to connect to.")
}
}
// On error abort
if url, err := tunnel.Establish(); err != nil {
message.Fatal(err, "Unable to establish the tunnel")
} else if blocking {
// Otherwise, if this is blocking it is coming from a user request so try to open the URL, but ignore errors
if tunnel.autoOpen {
switch runtime.GOOS {
case "linux":
_ = exec.Command("xdg-open", url).Start()
case "windows":
_ = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
_ = exec.Command("open", url).Start()
}
}
// Since this blocking, set the defer now so it closes properly on sigterm
defer tunnel.Close()
// Keep this open until an interrupt signal is received
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(0)
}()
for {
runtime.Gosched()
}
}
}
// Endpoint returns the tunnel endpoint
func (tunnel *Tunnel) Endpoint() string {
message.Debug("tunnel.Endpoint()")
return fmt.Sprintf("localhost:%d", tunnel.localPort)
}
// Close disconnects a tunnel connection by closing the StopChan, thereby stopping the goroutine.
func (tunnel *Tunnel) Close() {
message.Debug("tunnel.Close()")
close(tunnel.stopChan)
}
func (tunnel *Tunnel) checkForZarfConnectLabel(name string) error {
message.Debugf("tunnel.checkForZarfConnectLabel(%s)", name)
matches, err := GetServicesByLabel("", config.ZarfConnectLabelName, name)
if err != nil {
return fmt.Errorf("unable to lookup the service: %w", err)
}
if len(matches.Items) > 0 {
// If there is a match, use the first one as these are supposed to be unique
svc := matches.Items[0]
// Reset based on the matched params
tunnel.resourceType = SvcResource
tunnel.resourceName = svc.Name
tunnel.namespace = svc.Namespace
// Only support a service with a single port
tunnel.remotePort = svc.Spec.Ports[0].TargetPort.IntValue()
// Add the url suffix too
tunnel.urlSuffix = svc.Annotations[config.ZarfConnectAnnotationUrl]
message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, tunnel.remotePort)
}
return nil
}
// Establish opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct.
func (tunnel *Tunnel) Establish() (string, error) {
message.Debug("tunnel.Establish()")
var spinner *message.Spinner
spinnerMessage := fmt.Sprintf("Creating a port forwarding tunnel for resource %s/%s in namespace %s routing local port %d to remote port %d",
tunnel.resourceType,
tunnel.resourceName,
tunnel.namespace,
tunnel.localPort,
tunnel.remotePort,
)
if tunnel.spinner != nil {
spinner = tunnel.spinner
spinner.Updatef(spinnerMessage)
} else {
spinner = message.NewProgressSpinner(spinnerMessage)
defer spinner.Stop()
}
// Find the pod to port forward to
podName, err := tunnel.getAttachablePodForResource()
if err != nil {
return "", fmt.Errorf("unable to find pod attached to given resource: %w", err)
}
spinner.Debugf("Selected pod %s to open port forward to", podName)
clientset := getClientset()
// Build url to the port forward endpoint
// example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward
postEndpoint := clientset.CoreV1().RESTClient().Post()
namespace := tunnel.namespace
portForwardCreateURL := postEndpoint.
Resource("pods").
Namespace(namespace).
Name(podName).
SubResource("portforward").
URL()
spinner.Debugf("Using URL %s to create portforward", portForwardCreateURL)
restConfig := getRestConfig()
// Construct the spdy client required by the client-go portforward library
transport, upgrader, err := spdy.RoundTripperFor(restConfig)
if err != nil {
return "", fmt.Errorf("unable to create the spdy client %w", err)
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", portForwardCreateURL)
// If the local-port is 0, get an available port before continuing. We do this here instead of relying on the
// underlying port-forwarder library, because the port-forwarder library does not expose the selected local port in a
// machine-readable manner.
// Synchronize on the global lock to avoid race conditions with concurrently selecting the same available port,
// since there is a brief moment between `GetAvailablePort` and `forwarder.ForwardPorts` where the selected port
// is available for selection again.
if tunnel.localPort == 0 {
spinner.Debugf("Requested local port is 0. Selecting an open port on host system")
tunnel.localPort, err = GetAvailablePort()
if err != nil {
return "", fmt.Errorf("unable to find an available port: %w", err)
}
spinner.Debugf("Selected port %d", tunnel.localPort)
globalMutex.Lock()
defer globalMutex.Unlock()
}
// Construct a new PortForwarder struct that manages the instructed port forward tunnel
ports := []string{fmt.Sprintf("%d:%d", tunnel.localPort, tunnel.remotePort)}
portforwarder, err := portforward.New(dialer, ports, tunnel.stopChan, tunnel.readyChan, tunnel.out, tunnel.out)
if err != nil {
return "", fmt.Errorf("unable to create the port forward: %w", err)
}
// Open the tunnel in a goroutine so that it is available in the background. Report errors to the main goroutine via
// a new channel.
errChan := make(chan error)
go func() {
errChan <- portforwarder.ForwardPorts()
}()
// Wait for an error or the tunnel to be ready
select {
case err = <-errChan:
if tunnel.spinner == nil {
spinner.Stop()
}
return "", fmt.Errorf("unable to start the tunnel: %w", err)
case <-portforwarder.Ready:
url := fmt.Sprintf("http://%s:%v%s", config.IPV4Localhost, tunnel.localPort, tunnel.urlSuffix)
msg := fmt.Sprintf("Creating port forwarding tunnel available at %s", url)
if tunnel.spinner == nil {
spinner.Successf(msg)
} else {
spinner.Updatef(msg)
}
return url, nil
}
}
// GetAvailablePort retrieves an available port on the host machine. This delegates the port selection to the golang net
// library by starting a server and then checking the port that the server is using.
func GetAvailablePort() (int, error) {
message.Debug("tunnel.GetAvailablePort()")
l, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
defer func(l net.Listener) {
// ignore this error because it won't help us to tell the user
_ = l.Close()
}(l)
_, p, err := net.SplitHostPort(l.Addr().String())
if err != nil {
return 0, err
}
port, err := strconv.Atoi(p)
if err != nil {
return 0, err
}
return port, err
}
// getAttachablePodForResource will find a pod that can be port forwarded to the provided resource type and return
// the name.
func (tunnel *Tunnel) getAttachablePodForResource() (string, error) {
message.Debug("tunnel.GettAttachablePodForResource()")
switch tunnel.resourceType {
case PodResource:
return tunnel.resourceName, nil
case SvcResource:
return tunnel.getAttachablePodForService()
default:
return "", fmt.Errorf("unknown resource type: %s", tunnel.resourceType)
}
}
// getAttachablePodForServiceE will find an active pod associated with the Service and return the pod name.
func (tunnel *Tunnel) getAttachablePodForService() (string, error) {
message.Debug("tunnel.getAttachablePodForService()")
service, err := GetService(tunnel.namespace, tunnel.resourceName)
if err != nil {
return "", fmt.Errorf("unable to find the service: %w", err)
}
selectorLabelsOfPods := makeLabels(service.Spec.Selector)
servicePods := WaitForPodsAndContainers(types.ZarfContainerTarget{
Namespace: tunnel.namespace,
Selector: selectorLabelsOfPods,
}, false)
return servicePods[0], nil
}