-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathtunnel.go
546 lines (477 loc) · 16.8 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package cluster contains Zarf-specific cluster management functions.
package cluster
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
)
// Zarf specific connect strings
const (
ZarfRegistry = "REGISTRY"
ZarfGit = "GIT"
ZarfInjector = "INJECTOR"
ZarfInjectorName = "zarf-injector"
ZarfInjectorPort = 5000
ZarfRegistryName = "zarf-docker-registry"
ZarfRegistryPort = 5000
ZarfGitServerName = "zarf-gitea-http"
ZarfGitServerPort = 3000
)
// TunnelInfo is a struct that contains the necessary info to create a new Tunnel
type TunnelInfo struct {
localPort int
remotePort int
namespace string
resourceType string
resourceName string
urlSuffix string
}
// NewTunnelInfo returns a new TunnelInfo object for connecting to a cluster
func NewTunnelInfo(namespace, resourceType, resourceName, urlSuffix string, localPort, remotePort int) TunnelInfo {
return TunnelInfo{
namespace: namespace,
resourceType: resourceType,
resourceName: resourceName,
urlSuffix: urlSuffix,
localPort: localPort,
remotePort: remotePort,
}
}
// PrintConnectTable will print a table of all Zarf connect matches found in the cluster.
func (c *Cluster) PrintConnectTable(ctx context.Context) error {
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{{
Operator: metav1.LabelSelectorOpExists,
Key: config.ZarfConnectLabelName,
}},
})
if err != nil {
return err
}
serviceList, err := c.Clientset.CoreV1().Services("").List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return err
}
connections := make(types.ConnectStrings)
for _, svc := range serviceList.Items {
name := svc.Labels[config.ZarfConnectLabelName]
// Add the connectString for processing later in the deployment.
connections[name] = types.ConnectString{
Description: svc.Annotations[config.ZarfConnectAnnotationDescription],
URL: svc.Annotations[config.ZarfConnectAnnotationURL],
}
}
message.PrintConnectStringTable(connections)
return nil
}
// Connect will establish a tunnel to the specified target.
func (c *Cluster) Connect(ctx context.Context, target string) (*Tunnel, error) {
var err error
zt := TunnelInfo{
namespace: ZarfNamespaceName,
resourceType: SvcResource,
}
switch strings.ToUpper(target) {
case ZarfRegistry:
zt.resourceName = ZarfRegistryName
zt.remotePort = ZarfRegistryPort
zt.urlSuffix = `/v2/_catalog`
case ZarfGit:
zt.resourceName = ZarfGitServerName
zt.remotePort = ZarfGitServerPort
case ZarfInjector:
zt.resourceName = ZarfInjectorName
zt.remotePort = ZarfInjectorPort
default:
if target != "" {
if zt, err = c.checkForZarfConnectLabel(ctx, target); err != nil {
return nil, fmt.Errorf("problem looking for a zarf connect label in the cluster: %s", err.Error())
}
}
if zt.resourceName == "" {
return nil, fmt.Errorf("missing resource name")
}
if zt.remotePort < 1 {
return nil, fmt.Errorf("missing remote port")
}
}
return c.ConnectTunnelInfo(ctx, zt)
}
// ConnectTunnelInfo connects to the cluster with the provided TunnelInfo
func (c *Cluster) ConnectTunnelInfo(ctx context.Context, zt TunnelInfo) (*Tunnel, error) {
tunnel, err := c.NewTunnel(zt.namespace, zt.resourceType, zt.resourceName, zt.urlSuffix, zt.localPort, zt.remotePort)
if err != nil {
return nil, err
}
_, err = tunnel.Connect(ctx)
if err != nil {
return nil, err
}
return tunnel, nil
}
// ConnectToZarfRegistryEndpoint determines if a registry endpoint is in cluster, and if so opens a tunnel to connect to it
func (c *Cluster) ConnectToZarfRegistryEndpoint(ctx context.Context, registryInfo types.RegistryInfo) (string, *Tunnel, error) {
registryEndpoint := registryInfo.Address
var err error
var tunnel *Tunnel
if registryInfo.InternalRegistry {
// Establish a registry tunnel to send the images to the zarf registry
if tunnel, err = c.NewTunnel(ZarfNamespaceName, SvcResource, ZarfRegistryName, "", 0, ZarfRegistryPort); err != nil {
return "", tunnel, err
}
} else {
serviceList, err := c.Clientset.CoreV1().Services("").List(ctx, metav1.ListOptions{})
if err != nil {
return "", nil, err
}
svc, port, err := serviceInfoFromNodePortURL(serviceList.Items, registryInfo.Address)
// If this is a service (no error getting svcInfo), create a port-forward tunnel to that resource
if err == nil {
if tunnel, err = c.NewTunnel(svc.Namespace, SvcResource, svc.Name, "", 0, port); err != nil {
return "", tunnel, err
}
}
}
if tunnel != nil {
_, err = tunnel.Connect(ctx)
if err != nil {
return "", tunnel, err
}
registryEndpoint = tunnel.Endpoint()
}
return registryEndpoint, tunnel, nil
}
// checkForZarfConnectLabel looks in the cluster for a connect name that matches the target
func (c *Cluster) checkForZarfConnectLabel(ctx context.Context, name string) (TunnelInfo, error) {
var err error
var zt TunnelInfo
message.Debugf("Looking for a Zarf Connect Label in the cluster")
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
config.ZarfConnectLabelName: name,
},
})
if err != nil {
return TunnelInfo{}, err
}
listOpts := metav1.ListOptions{LabelSelector: selector.String()}
serviceList, err := c.Clientset.CoreV1().Services("").List(ctx, listOpts)
if err != nil {
return TunnelInfo{}, err
}
if len(serviceList.Items) > 0 {
// If there is a match, use the first one as these are supposed to be unique.
svc := serviceList.Items[0]
// Reset based on the matched params.
zt.resourceType = SvcResource
zt.resourceName = svc.Name
zt.namespace = svc.Namespace
// Only support a service with a single port.
zt.remotePort = svc.Spec.Ports[0].TargetPort.IntValue()
// if targetPort == 0, look for Port (which is required)
if zt.remotePort == 0 {
// TODO: Need a check for if container port is not found
remotePort, err := c.findPodContainerPort(ctx, svc)
if err != nil {
return TunnelInfo{}, err
}
zt.remotePort = remotePort
}
// Add the url suffix too.
zt.urlSuffix = svc.Annotations[config.ZarfConnectAnnotationURL]
message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, zt.remotePort)
} else {
return zt, fmt.Errorf("no matching services found for %s", name)
}
return zt, nil
}
func (c *Cluster) findPodContainerPort(ctx context.Context, svc corev1.Service) (int, error) {
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: svc.Spec.Selector})
if err != nil {
return 0, err
}
podList, err := c.Clientset.CoreV1().Pods(svc.Namespace).List(ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return 0, err
}
for _, pod := range podList.Items {
// Find the matching name on the port in the pod
for _, container := range pod.Spec.Containers {
for _, port := range container.Ports {
if port.Name == svc.Spec.Ports[0].TargetPort.String() {
return int(port.ContainerPort), nil
}
}
}
}
return 0, nil
}
// TODO: Refactor to use netip.AddrPort instead of a string for nodePortURL.
func serviceInfoFromNodePortURL(services []corev1.Service, nodePortURL string) (corev1.Service, int, error) {
// Attempt to parse as normal, if this fails add a scheme to the URL (docker registries don't use schemes)
parsedURL, err := url.Parse(nodePortURL)
if err != nil {
parsedURL, err = url.Parse("scheme://" + nodePortURL)
if err != nil {
return corev1.Service{}, 0, err
}
}
// Match hostname against localhost ip/hostnames
hostname := parsedURL.Hostname()
if hostname != helpers.IPV4Localhost && hostname != "localhost" {
return corev1.Service{}, 0, fmt.Errorf("node port services should be on localhost")
}
// Get the node port from the nodeportURL.
nodePort, err := strconv.Atoi(parsedURL.Port())
if err != nil {
return corev1.Service{}, 0, err
}
if nodePort < 30000 || nodePort > 32767 {
return corev1.Service{}, 0, fmt.Errorf("node port services should use the port range 30000-32767")
}
for _, svc := range services {
if svc.Spec.Type == "NodePort" {
for _, port := range svc.Spec.Ports {
if int(port.NodePort) == nodePort {
return svc, int(port.Port), nil
}
}
}
}
return corev1.Service{}, 0, fmt.Errorf("no matching node port services found")
}
// Global lock to synchronize port selections.
var globalMutex sync.Mutex
// Zarf Tunnel Configuration Constants.
const (
PodResource = "pod"
SvcResource = "svc"
)
// Tunnel is the main struct that configures and manages port forwarding tunnels to Kubernetes resources.
type Tunnel struct {
clientset kubernetes.Interface
restConfig *rest.Config
out io.Writer
localPort int
remotePort int
namespace string
resourceType string
resourceName string
urlSuffix string
attempt int
stopChan chan struct{}
readyChan chan struct{}
errChan chan error
}
// 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 (c *Cluster) NewTunnel(namespace, resourceType, resourceName, urlSuffix string, local, remote int) (*Tunnel, error) {
return &Tunnel{
clientset: c.Clientset,
restConfig: c.RestConfig,
out: io.Discard,
localPort: local,
remotePort: remote,
namespace: namespace,
resourceType: resourceType,
resourceName: resourceName,
urlSuffix: urlSuffix,
stopChan: make(chan struct{}, 1),
readyChan: make(chan struct{}, 1),
}, nil
}
// Wrap takes a function that returns an error and wraps it to check for tunnel errors as well.
func (tunnel *Tunnel) Wrap(function func() error) error {
var err error
funcErrChan := make(chan error)
go func() {
funcErrChan <- function()
}()
select {
case err = <-funcErrChan:
return err
case err = <-tunnel.ErrChan():
return err
}
}
// Connect will establish a tunnel to the specified target.
func (tunnel *Tunnel) Connect(ctx context.Context) (string, error) {
url, err := tunnel.establish(ctx)
// Try to establish the tunnel up to 3 times.
if err != nil {
tunnel.attempt++
// If we have exceeded the number of attempts, exit with an error.
if tunnel.attempt > 3 {
return "", fmt.Errorf("unable to establish tunnel after 3 attempts: %w", err)
}
// Otherwise, retry the connection but delay increasing intervals between attempts.
delay := tunnel.attempt * 10
message.Debugf("%s", err.Error())
message.Debugf("Delay creating tunnel, waiting %d seconds...", delay)
timer := time.NewTimer(0)
defer timer.Stop()
select {
case <-ctx.Done():
return "", ctx.Err()
case <-timer.C:
url, err = tunnel.Connect(ctx)
if err != nil {
return "", err
}
timer.Reset(time.Duration(delay) * time.Second)
}
}
return url, nil
}
// Endpoint returns the tunnel ip address and port (i.e. for docker registries)
func (tunnel *Tunnel) Endpoint() string {
return fmt.Sprintf("%s:%d", helpers.IPV4Localhost, tunnel.localPort)
}
// ErrChan returns the tunnel's error channel
func (tunnel *Tunnel) ErrChan() chan error {
return tunnel.errChan
}
// HTTPEndpoint returns the tunnel endpoint as a HTTP URL string.
func (tunnel *Tunnel) HTTPEndpoint() string {
return fmt.Sprintf("http://%s", tunnel.Endpoint())
}
// FullURL returns the tunnel endpoint as a HTTP URL string with the urlSuffix appended.
func (tunnel *Tunnel) FullURL() string {
return fmt.Sprintf("%s%s", tunnel.HTTPEndpoint(), tunnel.urlSuffix)
}
// Close disconnects a tunnel connection by closing the StopChan, thereby stopping the goroutine.
func (tunnel *Tunnel) Close() {
close(tunnel.stopChan)
}
// establish opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct.
func (tunnel *Tunnel) establish(ctx context.Context) (string, error) {
var err error
// Track this locally as we may need to retry if the tunnel fails.
localPort := tunnel.localPort
// 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 localPort == 0 {
message.Debugf("Requested local port is 0. Selecting an open port on host system")
localPort, err = helpers.GetAvailablePort()
if err != nil {
return "", fmt.Errorf("unable to find an available port: %w", err)
}
message.Debugf("Selected port %d", localPort)
globalMutex.Lock()
defer globalMutex.Unlock()
}
msg := fmt.Sprintf("Opening tunnel %d -> %d for %s/%s in namespace %s",
localPort,
tunnel.remotePort,
tunnel.resourceType,
tunnel.resourceName,
tunnel.namespace,
)
message.Debugf(msg)
// Find the pod to port forward to
podName, err := tunnel.getAttachablePodForResource(ctx)
if err != nil {
return "", fmt.Errorf("unable to find pod attached to given resource: %w", err)
}
message.Debugf("Selected pod %s to open port forward to", podName)
// Build url to the port forward endpoint.
// Example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward.
postEndpoint := tunnel.clientset.CoreV1().RESTClient().Post()
namespace := tunnel.namespace
portForwardCreateURL := postEndpoint.
Resource("pods").
Namespace(namespace).
Name(podName).
SubResource("portforward").
URL()
message.Debugf("Using URL %s to create portforward", portForwardCreateURL)
// Construct the spdy client required by the client-go portforward library.
transport, upgrader, err := spdy.RoundTripperFor(tunnel.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)
// Construct a new PortForwarder struct that manages the instructed port forward tunnel.
ports := []string{fmt.Sprintf("%d:%d", 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:
return "", fmt.Errorf("unable to start the tunnel: %w", err)
case <-portforwarder.Ready:
// Store for endpoint output
tunnel.localPort = localPort
url := tunnel.FullURL()
// Store the error channel to listen for errors
tunnel.errChan = errChan
message.Debugf("Creating port forwarding tunnel at %s", url)
return url, nil
}
}
// getAttachablePodForResource will find a pod that can be port forwarded to the provided resource type and return
// the name.
func (tunnel *Tunnel) getAttachablePodForResource(ctx context.Context) (string, error) {
switch tunnel.resourceType {
case PodResource:
return tunnel.resourceName, nil
case SvcResource:
return tunnel.getAttachablePodForService(ctx)
default:
return "", fmt.Errorf("unknown resource type: %s", tunnel.resourceType)
}
}
// getAttachablePodForService will find an active pod associated with the Service and return the pod name.
func (tunnel *Tunnel) getAttachablePodForService(ctx context.Context) (string, error) {
service, err := tunnel.clientset.CoreV1().Services(tunnel.namespace).Get(ctx, tunnel.resourceName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("unable to find the service: %w", err)
}
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: service.Spec.Selector})
if err != nil {
return "", err
}
listOpt := metav1.ListOptions{
LabelSelector: selector.String(),
FieldSelector: fmt.Sprintf("status.phase=%s", corev1.PodRunning),
}
podList, err := tunnel.clientset.CoreV1().Pods(tunnel.namespace).List(ctx, listOpt)
if err != nil {
return "", err
}
if len(podList.Items) < 1 {
return "", fmt.Errorf("no pods found for service %s", tunnel.resourceName)
}
return podList.Items[0].Name, nil
}