2015-01-08 20:41:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/portforward"
|
2015-03-04 00:24:29 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
2015-01-08 20:41:38 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
const (
|
2015-03-11 17:22:08 +00:00
|
|
|
portforward_example = `
|
|
|
|
// listens on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
|
|
|
|
$ kubectl port-forward -p mypod 5000 6000
|
2015-02-20 21:28:43 +00:00
|
|
|
|
2015-03-11 17:22:08 +00:00
|
|
|
// listens on port 8888 locally, forwarding to 5000 in the pod
|
2015-02-20 21:28:43 +00:00
|
|
|
$ kubectl port-forward -p mypod 8888:5000
|
|
|
|
|
2015-03-11 17:22:08 +00:00
|
|
|
// listens on a random port locally, forwarding to 5000 in the pod
|
2015-02-20 21:28:43 +00:00
|
|
|
$ kubectl port-forward -p mypod :5000
|
|
|
|
|
2015-03-11 17:22:08 +00:00
|
|
|
// listens on a random port locally, forwarding to 5000 in the pod
|
|
|
|
$ kubectl port-forward -p mypod 0:5000`
|
2015-02-20 21:28:43 +00:00
|
|
|
)
|
|
|
|
|
2015-01-08 20:41:38 +00:00
|
|
|
func (f *Factory) NewCmdPortForward() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2015-03-11 17:22:08 +00:00
|
|
|
Use: "port-forward -p POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]",
|
2015-03-26 03:00:12 +00:00
|
|
|
Short: "Forward one or more local ports to a pod.",
|
|
|
|
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) {
|
2015-03-09 22:08:16 +00:00
|
|
|
err := RunPortForward(f, cmd, args)
|
2015-03-04 00:24:29 +00:00
|
|
|
util.CheckErr(err)
|
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-03-09 22:08:16 +00:00
|
|
|
func RunPortForward(f *Factory, cmd *cobra.Command, args []string) error {
|
|
|
|
podName := util.GetFlagString(cmd, "pod")
|
|
|
|
if len(podName) == 0 {
|
2015-03-11 17:22:08 +00:00
|
|
|
return util.UsageError(cmd, "POD is required for exec")
|
2015-03-09 22:08:16 +00:00
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-03-09 22:08:16 +00:00
|
|
|
if len(args) < 1 {
|
2015-03-11 17:22:08 +00:00
|
|
|
return util.UsageError(cmd, "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
|
|
|
|
2015-03-14 10:45:18 +00:00
|
|
|
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
|
|
|
|
2015-03-14 10:45:18 +00:00
|
|
|
client, err := f.Client()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-03-09 22:08:16 +00:00
|
|
|
pod, err := client.Pods(namespace).Get(podName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-03-09 22:08:16 +00:00
|
|
|
if pod.Status.Phase != api.PodRunning {
|
|
|
|
glog.Fatalf("Unable to execute command because pod is not running. Current status=%v", pod.Status.Phase)
|
|
|
|
}
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-03-14 10:45:18 +00:00
|
|
|
config, err := f.ClientConfig()
|
2015-03-09 22:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-08 20:41:38 +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)
|
|
|
|
|
|
|
|
stopCh := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
<-signals
|
|
|
|
close(stopCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req := client.RESTClient.Get().
|
|
|
|
Prefix("proxy").
|
|
|
|
Resource("minions").
|
2015-04-02 12:52:03 +00:00
|
|
|
Name(pod.Spec.Host).
|
2015-03-09 22:08:16 +00:00
|
|
|
Suffix("portForward", namespace, podName)
|
|
|
|
|
|
|
|
pf, err := portforward.New(req, config, args, stopCh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
2015-03-09 22:08:16 +00:00
|
|
|
|
|
|
|
return pf.ForwardPorts()
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|