generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
portforward.go
136 lines (108 loc) · 3.49 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
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
package portforward
import (
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/k8s"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/transport/spdy"
toolsportforward "k8s.io/client-go/tools/portforward"
)
type PortForwardOptions struct {
PortForwarder PortForwarder
WorkspaceGroupClientMapper k8s.WorkspaceGroupClientMapper
K8sClient k8s.K8sClient
K8sAPIURL string
Namespace string
PodName string
Address []string
Ports []string
StopChannel chan struct{}
ReadyChannel chan struct{}
}
type PortForwarder interface {
ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error
}
func NewPortForwardOptions(workspaceGroupClientMapper k8s.WorkspaceGroupClientMapper, portforwarder PortForwarder) *PortForwardOptions {
p := &PortForwardOptions{
PortForwarder: portforwarder,
WorkspaceGroupClientMapper: workspaceGroupClientMapper,
}
p.Address = []string{"localhost"}
p.StopChannel = make(chan struct{}, 1)
p.ReadyChannel = make(chan struct{})
return p
}
func (o *PortForwardOptions) WithWorkspace(workspace entity.WorkspaceWithMeta) (*PortForwardOptions, error) {
o.Namespace = workspace.GetNamespaceName()
o.PodName = workspace.GetPodName()
k8sAPIURL, err := o.WorkspaceGroupClientMapper.GetK8sAPIURL(workspace.WorkspaceGroupID)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
o.K8sAPIURL = k8sAPIURL
k8sClient, err := o.WorkspaceGroupClientMapper.GetK8sClient(workspace.WorkspaceGroupID)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
o.K8sClient = k8sClient
if o.PodName == "" {
return nil, fmt.Errorf("unable to forward port because pod is not found-- workspace may not be running")
}
return o, nil
}
// TODO with stopchannel
func (o *PortForwardOptions) WithPort(port string) *PortForwardOptions {
o.Ports = []string{port}
return o
}
func (o PortForwardOptions) RunPortforward() error {
// cmd := portforward.NewCmdPortForward(tf, streams) // This command is useful to have around to go to def of kubectl cmd
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
defer signal.Stop(signals)
go func() {
<-signals
if o.StopChannel != nil {
close(o.StopChannel)
}
}()
urlStr := fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s/portforward", o.K8sAPIURL, o.Namespace, o.PodName)
url, err := url.Parse(urlStr)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = o.PortForwarder.ForwardPorts("POST", url, o)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
type DefaultPortForwarder struct {
genericclioptions.IOStreams
}
func NewDefaultPortForwarder() *DefaultPortForwarder {
return &DefaultPortForwarder{
IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
}
}
func (f *DefaultPortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
transport, upgrader, err := spdy.RoundTripperFor(opts.K8sClient.GetK8sRestConfig())
if err != nil {
return breverrors.WrapAndTrace(err)
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
fw, err := toolsportforward.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = fw.ForwardPorts()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}