forked from maordavidov/go-k8s-portforward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (51 loc) · 1.33 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"strings"
"time"
"github.com/maordavidov/go-k8s-portforward"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type labelFlags map[string]string
func (l *labelFlags) String() string {
return fmt.Sprintf("%v", *l)
}
func (l *labelFlags) Set(value string) error {
label := strings.SplitN(value, "=", 2)
if len(label) != 2 {
return errors.New("label must include equals sign.")
}
(*l)[label[0]] = label[1]
return nil
}
func main() {
labels := labelFlags{}
var pod string
var namespace string
var port int
var listenPort int
flag.Var(&labels, "label", "pod labels to look for")
flag.IntVar(&listenPort, "listen", 0, "port to bind to, random if 0")
flag.IntVar(&port, "port", 80, "port to forward to")
flag.StringVar(&pod, "pod", "", "pod name")
flag.StringVar(&namespace, "namespace", "default", "namespace to look for the pod in")
flag.Parse()
pf, err := portforward.NewPortForwarder(namespace, metav1.LabelSelector{
MatchLabels: labels,
}, port)
if err != nil {
log.Fatal("Error setting up port forwarder: ", err)
}
pf.Name = pod
pf.ListenPort = listenPort
_, err = pf.Start(context.Background())
if err != nil {
log.Fatal("Error starting port forward: ", err)
}
log.Printf("Started tunnel on %d\n", pf.ListenPort)
time.Sleep(60 * time.Second)
}