-
Notifications
You must be signed in to change notification settings - Fork 0
/
port-forward.go
144 lines (117 loc) · 3.33 KB
/
port-forward.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
package k8s
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
apimachinerytypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func PortForward(ctx context.Context, kcfg *rest.Config, name apimachinerytypes.NamespacedName, port int) (chan struct{}, error) {
kcfg.GroupVersion = &schema.GroupVersion{Group: "api", Version: "v1"}
kcfg.APIPath = "/"
kcfg.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
rc, err := rest.RESTClientFor(kcfg)
if err != nil {
return nil, err
}
req := rc.Post().
Resource("pods").
Namespace(name.Namespace).
Name(name.Name).
SubResource("portforward")
transport, upgrader, err := spdy.RoundTripperFor(kcfg)
if err != nil {
return nil, err
}
stopChan := make(chan struct{})
readyChan := make(chan struct{})
host := kcfg.Host
match, err := regexp.MatchString(`https://0\.0\.0\.0:\d{1,4}`, host)
if err != nil {
return nil, err
}
if match {
host = strings.Replace(host, "https://0.0.0.0", "127.0.0.1", -1)
} else {
host = strings.Replace(host, "https://", "", -1)
}
u := req.URL()
u.Host = host
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", u)
fw, err := portforward.New(dialer, []string{fmt.Sprintf("%v:%v", port, port)}, stopChan, readyChan, os.Stdout, os.Stderr)
if err != nil {
return nil, err
}
go func() {
err = fw.ForwardPorts()
}()
<-readyChan
return stopChan, nil
}
func PortForwardService(ctx context.Context, k8sC client.Client, kcfg *rest.Config, name apimachinerytypes.NamespacedName, port int) (chan struct{}, error) {
svc := &corev1.Service{}
err := k8sC.Get(ctx, name, svc)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: svc.Spec.Selector,
})
if err != nil {
return nil, err
}
pods := &corev1.PodList{}
err = k8sC.List(ctx, pods, &client.ListOptions{
LabelSelector: selector,
})
if err != nil {
return nil, err
}
if len(pods.Items) < 1 {
return nil, errors.New("no pods discovered for service")
}
podName := apimachinerytypes.NamespacedName{
Name: pods.Items[0].Name,
Namespace: pods.Items[0].Namespace,
}
return PortForward(ctx, kcfg, podName, port)
}
func PortForwardDeployment(ctx context.Context, k8sC client.Client, kcfg *rest.Config, name apimachinerytypes.NamespacedName, port int) (chan struct{}, error) {
dep := &appsv1.Deployment{}
err := k8sC.Get(ctx, name, dep)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(dep.Spec.Selector)
if err != nil {
return nil, err
}
pods := &corev1.PodList{}
err = k8sC.List(ctx, pods, &client.ListOptions{
LabelSelector: selector,
})
if err != nil {
return nil, err
}
if len(pods.Items) < 1 {
return nil, errors.New("no pods discovered for deployment")
}
podName := apimachinerytypes.NamespacedName{
Name: pods.Items[0].Name,
Namespace: pods.Items[0].Namespace,
}
return PortForward(ctx, kcfg, podName, port)
}