2015-01-08 20:41:38 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
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 cmd
|
|
|
|
|
|
|
|
import (
|
2016-08-04 06:31:23 +00:00
|
|
|
"fmt"
|
2017-07-07 21:54:34 +00:00
|
|
|
"net/http"
|
2015-09-27 00:00:39 +00:00
|
|
|
"net/url"
|
2015-01-08 20:41:38 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-02-14 17:02:57 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-02-13 17:10:02 +00:00
|
|
|
"time"
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/spf13/cobra"
|
2016-09-07 20:29:57 +00:00
|
|
|
|
2018-08-03 11:47:05 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-08-02 12:10:03 +00:00
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
2018-08-03 11:47:05 +00:00
|
|
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
2017-01-19 18:27:59 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-01-27 15:28:10 +00:00
|
|
|
"k8s.io/client-go/tools/portforward"
|
2017-07-07 22:22:39 +00:00
|
|
|
"k8s.io/client-go/transport/spdy"
|
2016-10-07 22:24:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
2015-08-05 22:03:47 +00:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
2018-05-08 13:02:34 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2018-05-17 17:04:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
|
2018-02-14 17:02:57 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/util"
|
2017-07-07 04:04:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
2015-01-08 20:41:38 +00:00
|
|
|
)
|
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
// PortForwardOptions contains all the options for running the port-forward cli command.
|
|
|
|
type PortForwardOptions struct {
|
|
|
|
Namespace string
|
|
|
|
PodName string
|
2016-09-07 20:29:57 +00:00
|
|
|
RESTClient *restclient.RESTClient
|
2016-08-04 06:31:23 +00:00
|
|
|
Config *restclient.Config
|
2018-08-03 11:47:05 +00:00
|
|
|
PodClient corev1client.PodsGetter
|
2016-08-04 06:31:23 +00:00
|
|
|
Ports []string
|
|
|
|
PortForwarder portForwarder
|
2016-08-08 12:31:15 +00:00
|
|
|
StopChannel chan struct{}
|
|
|
|
ReadyChannel chan struct{}
|
2016-08-04 06:31:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
var (
|
2018-02-13 17:10:02 +00:00
|
|
|
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 rerun of the command is needed
|
|
|
|
to resume forwarding.`))
|
|
|
|
|
2017-02-16 03:47:00 +00:00
|
|
|
portforwardExample = templates.Examples(i18n.T(`
|
2016-05-20 17:49:56 +00:00
|
|
|
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
|
2018-02-13 17:10:02 +00:00
|
|
|
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
|
2015-02-20 21:28:43 +00:00
|
|
|
|
add port-forward examples for sevice
add port-forward examples for sevice
```
$ kubectl port-forward --help
........
Examples:
# 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 ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the
service
kubectl port-forward service/myservice 5000 6000
# Listen on port 8888 locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod 8888:5000
# Listen on a random port locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod :5000
........
```
<!-- Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, read our contributor guidelines https://git.k8s.io/community/contributors/guide#your-first-contribution and developer guide https://git.k8s.io/community/contributors/devel/development.md#development-guide
2. If you want *faster* PR reviews, read how: https://git.k8s.io/community/contributors/guide/pull-requests.md#best-practices-for-faster-reviews
3. Follow the instructions for writing a release note: https://git.k8s.io/community/contributors/guide/release-notes.md
4. If the PR is unfinished, see how to mark it: https://git.k8s.io/community/contributors/guide/pull-requests.md#marking-unfinished-pull-requests
-->
**What this PR does / why we need it**:
add port-forward examples for sevice
**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #
**Special notes for your reviewer**:
**Release note**:
<!-- Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->
```release-note
add port-forward examples for sevice
```
2018-06-05 15:04:44 +00:00
|
|
|
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the service
|
|
|
|
kubectl port-forward service/myservice 5000 6000
|
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
# Listen on port 8888 locally, forwarding to 5000 in the pod
|
2018-02-13 17:10:02 +00:00
|
|
|
kubectl port-forward pod/mypod 8888:5000
|
2015-02-20 21:28:43 +00:00
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
# Listen on a random port locally, forwarding to 5000 in the pod
|
2018-02-13 17:10:02 +00:00
|
|
|
kubectl port-forward pod/mypod :5000`))
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Amount of time to wait until at least one pod is running
|
|
|
|
defaultPodPortForwardWaitTimeout = 60 * time.Second
|
2015-02-20 21:28:43 +00:00
|
|
|
)
|
|
|
|
|
2018-05-08 13:02:34 +00:00
|
|
|
func NewCmdPortForward(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
|
2016-08-04 06:31:23 +00:00
|
|
|
opts := &PortForwardOptions{
|
|
|
|
PortForwarder: &defaultPortForwarder{
|
2018-05-08 13:02:34 +00:00
|
|
|
IOStreams: streams,
|
2016-08-04 06:31:23 +00:00
|
|
|
},
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
cmd := &cobra.Command{
|
2018-02-13 17:10:02 +00:00
|
|
|
Use: "port-forward TYPE/NAME [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]",
|
2017-10-11 06:26:02 +00:00
|
|
|
DisableFlagsInUseLine: true,
|
2017-01-25 01:00:32 +00:00
|
|
|
Short: i18n.T("Forward one or more local ports to a pod"),
|
2018-02-13 17:10:02 +00:00
|
|
|
Long: portforwardLong,
|
2017-02-16 03:47:00 +00:00
|
|
|
Example: portforwardExample,
|
2015-01-08 20:41:38 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2017-03-17 13:55:46 +00:00
|
|
|
if err := opts.Complete(f, cmd, args); err != nil {
|
2016-08-04 06:31:23 +00:00
|
|
|
cmdutil.CheckErr(err)
|
|
|
|
}
|
|
|
|
if err := opts.Validate(); err != nil {
|
2017-06-14 21:14:42 +00:00
|
|
|
cmdutil.CheckErr(cmdutil.UsageErrorf(cmd, "%v", err.Error()))
|
2016-08-04 06:31:23 +00:00
|
|
|
}
|
|
|
|
if err := opts.RunPortForward(); err != nil {
|
|
|
|
cmdutil.CheckErr(err)
|
2015-11-09 21:22:42 +00:00
|
|
|
}
|
2015-03-09 22:08:16 +00:00
|
|
|
},
|
|
|
|
}
|
2018-02-13 17:10:02 +00:00
|
|
|
cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodPortForwardWaitTimeout)
|
2015-03-09 22:08:16 +00:00
|
|
|
// TODO support UID
|
|
|
|
return cmd
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-05-04 17:13:55 +00:00
|
|
|
type portForwarder interface {
|
2016-08-08 12:31:15 +00:00
|
|
|
ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error
|
2015-05-04 17:13:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:22:42 +00:00
|
|
|
type defaultPortForwarder struct {
|
2018-05-08 13:02:34 +00:00
|
|
|
genericclioptions.IOStreams
|
2015-11-09 21:22:42 +00:00
|
|
|
}
|
2015-05-04 17:13:55 +00:00
|
|
|
|
2016-08-08 12:31:15 +00:00
|
|
|
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
|
2017-07-07 22:22:39 +00:00
|
|
|
transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
|
2015-09-27 00:00:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-07 22:22:39 +00:00
|
|
|
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
|
2018-05-08 13:02:34 +00:00
|
|
|
fw, err := portforward.New(dialer, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
|
2015-05-04 17:13:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fw.ForwardPorts()
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:02:57 +00:00
|
|
|
// 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.
|
2018-08-03 11:47:05 +00:00
|
|
|
func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod corev1.Pod) ([]string, error) {
|
2018-02-14 17:02:57 +00:00
|
|
|
var translated []string
|
|
|
|
for _, port := range ports {
|
|
|
|
// port is in the form of [LOCAL PORT]:REMOTE PORT
|
|
|
|
parts := strings.Split(port, ":")
|
|
|
|
input := parts[0]
|
|
|
|
if len(parts) == 2 {
|
|
|
|
input = parts[1]
|
|
|
|
}
|
|
|
|
portnum, err := strconv.Atoi(input)
|
|
|
|
if err != nil {
|
|
|
|
return ports, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
if int32(portnum) != containerPort {
|
|
|
|
translated = append(translated, fmt.Sprintf("%s:%d", parts[0], containerPort))
|
|
|
|
} else {
|
|
|
|
translated = append(translated, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return translated, nil
|
|
|
|
}
|
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
// Complete completes all the required options for port-forward cmd.
|
2017-03-17 13:55:46 +00:00
|
|
|
func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
2016-08-04 06:31:23 +00:00
|
|
|
var err error
|
2018-02-13 17:10:02 +00:00
|
|
|
if len(args) < 2 {
|
|
|
|
return cmdutil.UsageErrorf(cmd, "TYPE/NAME and list of ports are required for port-forward")
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-07-13 08:25:22 +00:00
|
|
|
|
2018-05-24 13:33:36 +00:00
|
|
|
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
|
2018-02-13 17:10:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2018-02-13 17:10:02 +00:00
|
|
|
builder := f.NewBuilder().
|
2018-08-02 12:10:03 +00:00
|
|
|
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
|
2018-02-13 17:10:02 +00:00
|
|
|
ContinueOnError().
|
|
|
|
NamespaceParam(o.Namespace).DefaultNamespace()
|
|
|
|
|
|
|
|
getPodTimeout, err := cmdutil.GetPodRunningTimeoutFlag(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return cmdutil.UsageErrorf(cmd, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceName := args[0]
|
|
|
|
builder.ResourceNames("pods", resourceName)
|
|
|
|
|
|
|
|
obj, err := builder.Do().Object()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2018-05-17 17:04:35 +00:00
|
|
|
forwardablePod, err := polymorphichelpers.AttachablePodForObjectFn(f, obj, getPodTimeout)
|
2018-02-13 17:10:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o.PodName = forwardablePod.Name
|
2018-02-14 17:02:57 +00:00
|
|
|
|
|
|
|
// handle service port mapping to target port if needed
|
|
|
|
switch t := obj.(type) {
|
2018-08-03 11:47:05 +00:00
|
|
|
case *corev1.Service:
|
2018-08-02 12:10:03 +00:00
|
|
|
o.Ports, err = translateServicePortToTargetPort(args[1:], *t, *forwardablePod)
|
2018-02-14 17:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
o.Ports = args[1:]
|
|
|
|
}
|
2018-02-13 17:10:02 +00:00
|
|
|
|
2018-08-03 11:47:05 +00:00
|
|
|
clientset, err := f.KubernetesClientSet()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-17 17:04:35 +00:00
|
|
|
|
2018-08-03 11:47:05 +00:00
|
|
|
o.PodClient = clientset.CoreV1()
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2018-05-16 14:54:42 +00:00
|
|
|
o.Config, err = f.ToRESTConfig()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-07 20:29:57 +00:00
|
|
|
o.RESTClient, err = f.RESTClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2016-08-08 12:31:15 +00:00
|
|
|
o.StopChannel = make(chan struct{}, 1)
|
|
|
|
o.ReadyChannel = make(chan struct{})
|
2016-08-04 06:31:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates all the required options for port-forward cmd.
|
|
|
|
func (o PortForwardOptions) Validate() error {
|
|
|
|
if len(o.PodName) == 0 {
|
2018-02-13 17:10:02 +00:00
|
|
|
return fmt.Errorf("pod name or resource type/name must be specified")
|
2016-08-04 06:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(o.Ports) < 1 {
|
|
|
|
return fmt.Errorf("at least 1 PORT is required for port-forward")
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
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")
|
2016-08-04 06:31:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunPortForward implements all the necessary functionality for port-forward cmd.
|
|
|
|
func (o PortForwardOptions) RunPortForward() error {
|
2016-12-07 13:26:33 +00:00
|
|
|
pod, err := o.PodClient.Pods(o.Namespace).Get(o.PodName, metav1.GetOptions{})
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2018-08-03 11:47:05 +00:00
|
|
|
if pod.Status.Phase != corev1.PodRunning {
|
2016-08-08 12:31:15 +00:00
|
|
|
return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase)
|
2016-08-04 06:31:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 22:08:16 +00:00
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, os.Interrupt)
|
|
|
|
defer signal.Stop(signals)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-signals
|
2016-08-08 12:31:15 +00:00
|
|
|
if o.StopChannel != nil {
|
|
|
|
close(o.StopChannel)
|
|
|
|
}
|
2015-03-09 22:08:16 +00:00
|
|
|
}()
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
req := o.RESTClient.Post().
|
2015-05-04 17:13:55 +00:00
|
|
|
Resource("pods").
|
2016-08-04 06:31:23 +00:00
|
|
|
Namespace(o.Namespace).
|
2015-05-04 17:13:55 +00:00
|
|
|
Name(pod.Name).
|
|
|
|
SubResource("portforward")
|
2015-03-09 22:08:16 +00:00
|
|
|
|
2016-08-08 12:31:15 +00:00
|
|
|
return o.PortForwarder.ForwardPorts("POST", req.URL(), o)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|