-
Notifications
You must be signed in to change notification settings - Fork 53
/
portforward.go
74 lines (69 loc) · 1.84 KB
/
portforward.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
package testutil
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func PortForward(
ctx context.Context,
service types.NamespacedName,
servicePorts []string,
restConfig *rest.Config,
scheme *runtime.Scheme,
) ([]portforward.ForwardedPort, error) {
cl, err := client.New(restConfig, client.Options{Scheme: scheme})
if err != nil {
return nil, err
}
svc := &corev1.Service{}
cl.Get(ctx, service, svc)
pods := &corev1.PodList{}
cl.List(ctx, pods, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set(svc.Spec.Selector)),
})
stopCh := make(chan struct{})
readyCh := make(chan struct{})
for _, pod := range pods.Items {
transport, upgrader, err := spdy.RoundTripperFor(restConfig)
if err != nil {
return nil, err
}
u, err := url.Parse(restConfig.Host)
u.Path = path.Join(u.Path, fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", pod.Namespace, pod.Name))
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, u)
forwarder, err := portforward.New(dialer, servicePorts, stopCh, readyCh, os.Stdout, os.Stderr)
if err != nil {
return nil, err
}
startErr := make(chan error)
go func() {
if err := forwarder.ForwardPorts(); err != nil {
<-startErr
}
}()
select {
case <-readyCh:
go func() {
<-ctx.Done()
close(stopCh)
}()
return forwarder.GetPorts()
case err := <-startErr:
return nil, fmt.Errorf("error starting port forward: %v", err)
case <-ctx.Done():
return nil, ctx.Err()
}
}
return nil, fmt.Errorf("no pods matching the service were found")
}