-
Notifications
You must be signed in to change notification settings - Fork 5
/
forward.go
250 lines (198 loc) · 5.83 KB
/
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
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
package forward
import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)
/*************************************/
/**** Inspiration ********************/
/*************************************/
// https://github.com/gianarb/kube-port-forward
type PortForwardAServiceRequest struct {
RestConfig *rest.Config
Service v1.Service
LocalPort int
ServicePort int
Streams genericclioptions.IOStreams
StopCh <-chan struct{}
ReadyCh chan struct{}
}
type ServiceMapping struct {
Port int
Namespace string
Identifier string
Protocol string
}
func Forward(services map[string]ServiceMapping) {
config, err := config()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
stream := genericclioptions.IOStreams{
// Typically the forwarding is noisy, we can quiet that here.
// In: os.Stdin,
// Out: os.Stdout,
ErrOut: os.Stderr,
}
// Create a signal sniffing channel and notify on SIGINT or SIGTERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var syncGroup sync.WaitGroup
syncGroup.Add(1)
go func() {
// sig := <-sigs
<-sigs
fmt.Println()
fmt.Print("Bye...")
syncGroup.Done()
os.Exit(0)
}()
var readyChannels []chan struct{}
var servicesNotFound []string
for _, mapping := range services {
readyCh := make(chan struct{})
readyChannels = append(readyChannels, readyCh)
stopCh := make(<-chan struct{})
go func(srv string, m ServiceMapping, r chan struct{}, s <-chan struct{}) (err error) {
var clientset *kubernetes.Clientset
var opts metav1.GetOptions
var svc *v1.Service
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
return
}
svc, err = clientset.CoreV1().Services(m.Namespace).Get(context.TODO(), srv, opts)
if err != nil {
fmt.Println(err)
close(r)
servicesNotFound = append(servicesNotFound, srv)
return
}
err = PortForwardAService(config, PortForwardAServiceRequest{
RestConfig: config,
Service: *svc,
LocalPort: m.Port,
ServicePort: m.Port,
Streams: stream,
StopCh: s,
ReadyCh: r,
})
if err != nil {
fmt.Println(err)
return
}
return
}(mapping.Identifier, mapping, readyCh, stopCh)
}
for i := range readyChannels {
for range readyChannels[i] {
<-readyChannels[i]
}
}
printTable(services, servicesNotFound)
syncGroup.Wait()
}
func printTable(services map[string]ServiceMapping, servicesNotFound []string) {
t := table.NewWriter()
for service, mapping := range services {
if !contains(servicesNotFound, mapping.Identifier) {
t.AppendRow(table.Row{service, fmt.Sprintf("%s://localhost:%d", mapping.Protocol, mapping.Port)})
}
}
t.SetStyle(table.StyleLight)
colorBOnW := text.Colors{text.BgWhite, text.FgBlack}
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "Service", Colors: text.Colors{text.FgYellow}, ColorsHeader: colorBOnW},
{Name: "URL", Colors: text.Colors{text.FgHiRed}, ColorsHeader: colorBOnW},
})
t.SetCaption("Monitoring Resources... ^C to exit\n")
fmt.Println(t.Render())
t.SetColumnConfigs([]table.ColumnConfig{})
}
// You have to portforward directly to a pod, the service is the abstraction.
func PortForwardAService(config *rest.Config, req PortForwardAServiceRequest) (err error) {
var podName string
var pods *v1.PodList
var listOptions metav1.ListOptions
var set labels.Set
var dialer httpstream.Dialer
var fw *portforward.PortForwarder
var path string
var hostIP string
var clientset *kubernetes.Clientset
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
return
}
set = labels.Set(req.Service.Spec.Selector)
listOptions = metav1.ListOptions{LabelSelector: set.AsSelector().String()}
pods, err = clientset.CoreV1().Pods(req.Service.Namespace).List(context.TODO(), listOptions)
for _, pod := range pods.Items {
podName = pod.Name
}
if podName == "" || err != nil {
err = fmt.Errorf("could not locate pod for service: %s", req.Service.Name)
return
}
path = fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", req.Service.Namespace, podName)
hostIP = strings.TrimLeft(req.RestConfig.Host, "htps:/")
transport, upgrader, err := spdy.RoundTripperFor(req.RestConfig)
if err != nil {
return err
}
dialer = spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, &url.URL{Scheme: "https", Path: path, Host: hostIP})
fw, err = portforward.New(dialer, []string{fmt.Sprintf("%d:%d", req.LocalPort, req.ServicePort)}, req.StopCh, req.ReadyCh, req.Streams.Out, req.Streams.ErrOut)
if err != nil {
return err
}
return fw.ForwardPorts()
}
/*************************************/
/**** Helpers and Initialization *****/
/*************************************/
func config() (*rest.Config, error) {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
return clientcmd.BuildConfigFromFlags("", *kubeconfig)
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}