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"
|
2015-11-09 21:22:42 +00:00
|
|
|
"io"
|
2015-09-27 00:00:39 +00:00
|
|
|
"net/url"
|
2015-01-08 20:41:38 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/spf13/cobra"
|
2016-09-07 20:29:57 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-10-21 22:24:05 +00:00
|
|
|
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
|
2016-02-12 18:58:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/restclient"
|
2015-08-13 19:01:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/portforward"
|
2015-09-27 00:00:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
|
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"
|
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
|
2016-09-07 20:29:57 +00:00
|
|
|
PodClient coreclient.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 (
|
2016-10-07 22:24:42 +00:00
|
|
|
portforward_example = templates.Examples(`
|
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
|
|
|
|
kubectl port-forward mypod 5000 6000
|
2015-02-20 21:28:43 +00:00
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
# Listen on port 8888 locally, forwarding to 5000 in the pod
|
|
|
|
kubectl port-forward 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
|
|
|
|
kubectl port-forward mypod :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
|
|
|
|
kubectl port-forward mypod 0:5000`)
|
2015-02-20 21:28:43 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 00:18:39 +00:00
|
|
|
func NewCmdPortForward(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Command {
|
2016-08-04 06:31:23 +00:00
|
|
|
opts := &PortForwardOptions{
|
|
|
|
PortForwarder: &defaultPortForwarder{
|
|
|
|
cmdOut: cmdOut,
|
|
|
|
cmdErr: cmdErr,
|
|
|
|
},
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
cmd := &cobra.Command{
|
2015-07-13 08:25:22 +00:00
|
|
|
Use: "port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]",
|
2016-06-07 17:38:04 +00:00
|
|
|
Short: "Forward one or more local ports to a pod",
|
2015-03-26 03:00:12 +00:00
|
|
|
Long: "Forward one or more local ports to a pod.",
|
2015-02-20 21:28:43 +00:00
|
|
|
Example: portforward_example,
|
2015-01-08 20:41:38 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-08-04 06:31:23 +00:00
|
|
|
if err := opts.Complete(f, cmd, args, cmdOut, cmdErr); err != nil {
|
|
|
|
cmdutil.CheckErr(err)
|
|
|
|
}
|
|
|
|
if err := opts.Validate(); err != nil {
|
|
|
|
cmdutil.CheckErr(cmdutil.UsageError(cmd, err.Error()))
|
|
|
|
}
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().StringP("pod", "p", "", "Pod name")
|
|
|
|
// 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 {
|
|
|
|
cmdOut, cmdErr io.Writer
|
|
|
|
}
|
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 {
|
|
|
|
dialer, err := remotecommand.NewExecutor(opts.Config, method, url)
|
2015-09-27 00:00:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-08 12:31:15 +00:00
|
|
|
fw, err := portforward.New(dialer, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.cmdOut, f.cmdErr)
|
2015-05-04 17:13:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fw.ForwardPorts()
|
|
|
|
}
|
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
// Complete completes all the required options for port-forward cmd.
|
2016-10-13 00:18:39 +00:00
|
|
|
func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, cmdOut io.Writer, cmdErr io.Writer) error {
|
2016-08-04 06:31:23 +00:00
|
|
|
var err error
|
|
|
|
o.PodName = cmdutil.GetFlagString(cmd, "pod")
|
|
|
|
if len(o.PodName) == 0 && len(args) == 0 {
|
2015-07-13 08:25:22 +00:00
|
|
|
return cmdutil.UsageError(cmd, "POD is required for port-forward")
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-07-13 08:25:22 +00:00
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
if len(o.PodName) != 0 {
|
2015-07-13 08:25:22 +00:00
|
|
|
printDeprecationWarning("port-forward POD", "-p POD")
|
2016-08-04 06:31:23 +00:00
|
|
|
o.Ports = args
|
2015-07-13 08:25:22 +00:00
|
|
|
} else {
|
2016-08-04 06:31:23 +00:00
|
|
|
o.PodName = args[0]
|
|
|
|
o.Ports = args[1:]
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
o.Namespace, _, err = f.DefaultNamespace()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
clientset, err := f.ClientSet()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-07 20:29:57 +00:00
|
|
|
o.PodClient = clientset.Core()
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
o.Config, err = f.ClientConfig()
|
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 {
|
|
|
|
return fmt.Errorf("pod name must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-07 20:29:57 +00:00
|
|
|
pod, err := o.PodClient.Pods(o.Namespace).Get(o.PodName)
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2016-08-04 06:31:23 +00:00
|
|
|
if pod.Status.Phase != api.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
|
|
|
}
|