-
Notifications
You must be signed in to change notification settings - Fork 923
/
portforward.go
456 lines (391 loc) · 13.9 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
Copyright 2014 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 portforward
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/kubernetes/scheme"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/polymorphichelpers"
"k8s.io/kubectl/pkg/util"
"k8s.io/kubectl/pkg/util/completion"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
// PortForwardOptions contains all the options for running the port-forward cli command.
type PortForwardOptions struct {
Namespace string
PodName string
RESTClient restclient.Interface
Config *restclient.Config
PodClient corev1client.PodsGetter
Address []string
Ports []string
PortForwarder portForwarder
StopChannel chan struct{}
ReadyChannel chan struct{}
}
var (
portforwardLong = templates.LongDesc(i18n.T(`
Forward one or more local ports to a pod.
Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.
If there are multiple pods matching the criteria, a pod will be selected automatically. The
forwarding session ends when the selected pod terminates, and a rerun of the command is needed
to resume forwarding.`))
portforwardExample = templates.Examples(i18n.T(`
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
kubectl port-forward pod/mypod 5000 6000
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
kubectl port-forward deployment/mydeployment 5000 6000
# Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by the service
kubectl port-forward service/myservice 8443:https
# Listen on port 8888 locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod 8888:5000
# Listen on port 8888 on all addresses, forwarding to 5000 in the pod
kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000
# Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000
# Listen on a random port locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod :5000`))
)
const (
// Amount of time to wait until at least one pod is running
defaultPodPortForwardWaitTimeout = 60 * time.Second
)
func NewCmdPortForward(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
opts := NewDefaultPortForwardOptions(streams)
cmd := &cobra.Command{
Use: "port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]",
DisableFlagsInUseLine: true,
Short: i18n.T("Forward one or more local ports to a pod"),
Long: portforwardLong,
Example: portforwardExample,
ValidArgsFunction: completion.ResourceAndPortCompletionFunc(f),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(opts.Complete(f, cmd, args))
cmdutil.CheckErr(opts.Validate())
cmdutil.CheckErr(opts.RunPortForward())
},
}
cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodPortForwardWaitTimeout)
cmd.Flags().StringSliceVar(&opts.Address, "address", []string{"localhost"}, "Addresses to listen on (comma separated). Only accepts IP addresses or localhost as a value. When localhost is supplied, kubectl will try to bind on both 127.0.0.1 and ::1 and will fail if neither of these addresses are available to bind.")
// TODO support UID
return cmd
}
func NewDefaultPortForwardOptions(streams genericiooptions.IOStreams) *PortForwardOptions {
return &PortForwardOptions{
PortForwarder: &defaultPortForwarder{
IOStreams: streams,
},
}
}
type portForwarder interface {
ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error
}
type defaultPortForwarder struct {
genericiooptions.IOStreams
}
func createDialer(method string, url *url.URL, opts PortForwardOptions) (httpstream.Dialer, error) {
transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
if err != nil {
return nil, err
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
if !cmdutil.PortForwardWebsockets.IsDisabled() {
tunnelingDialer, err := portforward.NewSPDYOverWebsocketDialer(url, opts.Config)
if err != nil {
return nil, err
}
// First attempt tunneling (websocket) dialer, then fallback to spdy dialer.
dialer = portforward.NewFallbackDialer(tunnelingDialer, dialer, func(err error) bool {
return httpstream.IsUpgradeFailure(err) || httpstream.IsHTTPSProxyError(err)
})
}
return dialer, nil
}
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
dialer, err := createDialer(method, url, opts)
if err != nil {
return err
}
fw, err := portforward.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
if err != nil {
return err
}
return fw.ForwardPorts()
}
// splitPort splits port string which is in form of [LOCAL PORT]:REMOTE PORT
// and returns local and remote ports separately
func splitPort(port string) (local, remote string) {
parts := strings.Split(port, ":")
if len(parts) == 2 {
return parts[0], parts[1]
}
return parts[0], parts[0]
}
// Translates service port to target port
// It rewrites ports as needed if the Service port declares targetPort.
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// the port.
func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod corev1.Pod) ([]string, error) {
var translated []string
for _, port := range ports {
localPort, remotePort := splitPort(port)
portnum, err := strconv.Atoi(remotePort)
if err != nil {
svcPort, err := util.LookupServicePortNumberByName(svc, remotePort)
if err != nil {
return nil, err
}
portnum = int(svcPort)
if localPort == remotePort {
localPort = strconv.Itoa(portnum)
}
}
containerPort, err := util.LookupContainerPortNumberByServicePort(svc, pod, int32(portnum))
if err != nil {
// can't resolve a named port, or Service did not declare this port, return an error
return nil, err
}
// convert the resolved target port back to a string
remotePort = strconv.Itoa(int(containerPort))
if localPort != remotePort {
translated = append(translated, fmt.Sprintf("%s:%s", localPort, remotePort))
} else {
translated = append(translated, remotePort)
}
}
return translated, nil
}
// convertPodNamedPortToNumber converts named ports into port numbers
// It returns an error when a named port can't be found in the pod containers
func convertPodNamedPortToNumber(ports []string, pod corev1.Pod) ([]string, error) {
var converted []string
for _, port := range ports {
localPort, remotePort := splitPort(port)
if remotePort == "" {
return nil, fmt.Errorf("remote port cannot be empty")
}
containerPortStr := remotePort
_, err := strconv.Atoi(remotePort)
if err != nil {
containerPort, err := util.LookupContainerPortNumberByName(pod, remotePort)
if err != nil {
return nil, err
}
containerPortStr = strconv.Itoa(int(containerPort))
}
if localPort != remotePort {
converted = append(converted, fmt.Sprintf("%s:%s", localPort, containerPortStr))
} else {
converted = append(converted, containerPortStr)
}
}
return converted, nil
}
func checkUDPPorts(udpOnlyPorts sets.Set[int], ports []string, obj metav1.Object) error {
for _, port := range ports {
_, remotePort := splitPort(port)
portNum, err := strconv.Atoi(remotePort)
if err != nil {
switch v := obj.(type) {
case *corev1.Service:
svcPort, err := util.LookupServicePortNumberByName(*v, remotePort)
if err != nil {
return err
}
portNum = int(svcPort)
case *corev1.Pod:
ctPort, err := util.LookupContainerPortNumberByName(*v, remotePort)
if err != nil {
return err
}
portNum = int(ctPort)
default:
return fmt.Errorf("unknown object: %v", obj)
}
}
if udpOnlyPorts.Has(portNum) {
return fmt.Errorf("UDP protocol is not supported for %s", remotePort)
}
}
return nil
}
// checkUDPPortInService returns an error if remote port in Service is a UDP port
// TODO: remove this check after #47862 is solved
func checkUDPPortInService(ports []string, svc *corev1.Service) error {
udpPorts := sets.New[int]()
tcpPorts := sets.New[int]()
for _, port := range svc.Spec.Ports {
portNum := int(port.Port)
switch port.Protocol {
case corev1.ProtocolUDP:
udpPorts.Insert(portNum)
case corev1.ProtocolTCP:
tcpPorts.Insert(portNum)
}
}
return checkUDPPorts(udpPorts.Difference(tcpPorts), ports, svc)
}
// checkUDPPortInPod returns an error if remote port in Pod is a UDP port
// TODO: remove this check after #47862 is solved
func checkUDPPortInPod(ports []string, pod *corev1.Pod) error {
udpPorts := sets.New[int]()
tcpPorts := sets.New[int]()
for _, ct := range pod.Spec.Containers {
for _, ctPort := range ct.Ports {
portNum := int(ctPort.ContainerPort)
switch ctPort.Protocol {
case corev1.ProtocolUDP:
udpPorts.Insert(portNum)
case corev1.ProtocolTCP:
tcpPorts.Insert(portNum)
}
}
}
return checkUDPPorts(udpPorts.Difference(tcpPorts), ports, pod)
}
// Complete completes all the required options for port-forward cmd.
func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
var err error
if len(args) < 2 {
return cmdutil.UsageErrorf(cmd, "TYPE/NAME and list of ports are required for port-forward")
}
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
builder := f.NewBuilder().
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
ContinueOnError().
NamespaceParam(o.Namespace).DefaultNamespace()
getPodTimeout, err := cmdutil.GetPodRunningTimeoutFlag(cmd)
if err != nil {
return cmdutil.UsageErrorf(cmd, "%s", err.Error())
}
resourceName := args[0]
builder.ResourceNames("pods", resourceName)
obj, err := builder.Do().Object()
if err != nil {
return err
}
forwardablePod, err := polymorphichelpers.AttachablePodForObjectFn(f, obj, getPodTimeout)
if err != nil {
return err
}
o.PodName = forwardablePod.Name
// handle service port mapping to target port if needed
switch t := obj.(type) {
case *corev1.Service:
err = checkUDPPortInService(args[1:], t)
if err != nil {
return err
}
o.Ports, err = translateServicePortToTargetPort(args[1:], *t, *forwardablePod)
if err != nil {
return err
}
default:
err = checkUDPPortInPod(args[1:], forwardablePod)
if err != nil {
return err
}
o.Ports, err = convertPodNamedPortToNumber(args[1:], *forwardablePod)
if err != nil {
return err
}
}
clientset, err := f.KubernetesClientSet()
if err != nil {
return err
}
o.PodClient = clientset.CoreV1()
o.Config, err = f.ToRESTConfig()
if err != nil {
return err
}
o.RESTClient, err = f.RESTClient()
if err != nil {
return err
}
o.StopChannel = make(chan struct{}, 1)
o.ReadyChannel = make(chan struct{})
return nil
}
// Validate validates all the required options for port-forward cmd.
func (o PortForwardOptions) Validate() error {
if len(o.PodName) == 0 {
return fmt.Errorf("pod name or resource type/name must be specified")
}
if len(o.Ports) < 1 {
return fmt.Errorf("at least 1 PORT is required for port-forward")
}
if o.PortForwarder == nil || o.PodClient == nil || o.RESTClient == nil || o.Config == nil {
return fmt.Errorf("client, client config, restClient, and portforwarder must be provided")
}
return nil
}
// Deprecated: Use RunPortForwardContext instead, which allows canceling.
// RunPortForward implements all the necessary functionality for port-forward cmd.
func (o PortForwardOptions) RunPortForward() error {
return o.RunPortForwardContext(context.Background())
}
// RunPortForwardContext implements all the necessary functionality for port-forward cmd.
// It ends portforwarding when an error is received from the backend, or an os.Interrupt
// signal is received, or the provided context is done.
func (o PortForwardOptions) RunPortForwardContext(ctx context.Context) error {
pod, err := o.PodClient.Pods(o.Namespace).Get(ctx, o.PodName, metav1.GetOptions{})
if err != nil {
return err
}
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
defer signal.Stop(signals)
returnCtx, returnCtxCancel := context.WithCancel(ctx)
defer returnCtxCancel()
go func() {
select {
case <-signals:
case <-returnCtx.Done():
}
if o.StopChannel != nil {
close(o.StopChannel)
}
}()
req := o.RESTClient.Post().
Resource("pods").
Namespace(o.Namespace).
Name(pod.Name).
SubResource("portforward")
return o.PortForwarder.ForwardPorts("POST", req.URL(), o)
}