/
portforward.go
133 lines (112 loc) · 3.4 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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
internalapi "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
)
var runtimePortForwardCommand = &cli.Command{
Name: "port-forward",
Usage: "Forward local port to a pod",
ArgsUsage: "POD-ID [LOCAL_PORT:]REMOTE_PORT",
Flags: []cli.Flag{
&cli.StringFlag{
Name: transportFlag,
Aliases: []string{"r"},
Value: transportSpdy,
Usage: fmt.Sprintf("Transport protocol to use, one of: %s|%s", transportSpdy, transportWebsocket),
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
return cli.ShowSubcommandHelp(c)
}
runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
return err
}
var opts = portforwardOptions{
id: c.Args().Get(0),
ports: c.Args().Tail(),
transport: c.String(transportFlag),
}
if err = PortForward(runtimeClient, opts); err != nil {
return fmt.Errorf("port forward: %w", err)
}
return nil
},
}
// PortForward sends an PortForwardRequest to server, and parses the returned PortForwardResponse
func PortForward(client internalapi.RuntimeService, opts portforwardOptions) error {
if opts.id == "" {
return fmt.Errorf("ID cannot be empty")
}
request := &pb.PortForwardRequest{
PodSandboxId: opts.id,
}
logrus.Debugf("PortForwardRequest: %v", request)
r, err := client.PortForward(context.TODO(), request)
logrus.Debugf("PortForwardResponse; %v", r)
if err != nil {
return err
}
portforwardURL := r.Url
URL, err := url.Parse(portforwardURL)
if err != nil {
return err
}
if URL.Host == "" {
URL.Host = kubeletURLHost
}
if URL.Scheme == "" {
URL.Scheme = kubeletURLSchema
}
logrus.Debugf("PortForward URL: %v", URL)
dialer, err := getDialer(opts.transport, URL)
if err != nil {
return fmt.Errorf("get dialer: %w", err)
}
readyChan := make(chan struct{})
logrus.Debugf("Ports to forword: %v", opts.ports)
pf, err := portforward.New(dialer, opts.ports, SetupInterruptSignalHandler(), readyChan, os.Stdout, os.Stderr)
if err != nil {
return err
}
return pf.ForwardPorts()
}
func getDialer(transport string, url *url.URL) (exec httpstream.Dialer, err error) {
config := &rest.Config{TLSClientConfig: rest.TLSClientConfig{Insecure: true}}
switch transport {
case transportSpdy:
tr, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
return nil, fmt.Errorf("get SPDY round tripper: %w", err)
}
return spdy.NewDialer(upgrader, &http.Client{Transport: tr}, "POST", url), nil
case transportWebsocket:
return portforward.NewSPDYOverWebsocketDialer(url, config)
default:
return nil, fmt.Errorf("unknown transport: %s", transport)
}
}