diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index b29dec888fd..7e800ab4817 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -576,8 +576,6 @@ _kubectl_port-forward() two_word_flags+=("-p") must_have_one_flag=() - must_have_one_flag+=("--pod=") - must_have_one_flag+=("-p") must_have_one_noun=() } diff --git a/docs/man/man1/kubectl-port-forward.1 b/docs/man/man1/kubectl-port-forward.1 index 6d52308dd3e..6c543d7921d 100644 --- a/docs/man/man1/kubectl-port-forward.1 +++ b/docs/man/man1/kubectl-port-forward.1 @@ -131,16 +131,16 @@ Forward one or more local ports to a pod. .nf // 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 +$ kubectl port\-forward mypod 5000 6000 // listens on port 8888 locally, forwarding to 5000 in the pod -$ kubectl port\-forward \-p mypod 8888:5000 +$ kubectl port\-forward mypod 8888:5000 // listens on a random port locally, forwarding to 5000 in the pod -$ kubectl port\-forward \-p mypod :5000 +$ kubectl port\-forward mypod :5000 // listens on a random port locally, forwarding to 5000 in the pod -$ kubectl port\-forward \-p mypod 0:5000 +$ kubectl port\-forward mypod 0:5000 .fi .RE diff --git a/docs/user-guide/kubectl/kubectl_patch.md b/docs/user-guide/kubectl/kubectl_patch.md index 351e3147f9e..1fa55839f9d 100644 --- a/docs/user-guide/kubectl/kubectl_patch.md +++ b/docs/user-guide/kubectl/kubectl_patch.md @@ -100,7 +100,7 @@ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-08-05 23:27:50.885807613 +0000 UTC +###### Auto generated by spf13/cobra at 2015-08-07 09:20:43.877286912 +0000 UTC [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_patch.md?pixel)]() diff --git a/docs/user-guide/kubectl/kubectl_port-forward.md b/docs/user-guide/kubectl/kubectl_port-forward.md index 7c3814a1ec9..84cc12ccbe5 100644 --- a/docs/user-guide/kubectl/kubectl_port-forward.md +++ b/docs/user-guide/kubectl/kubectl_port-forward.md @@ -41,7 +41,7 @@ Forward one or more local ports to a pod. Forward one or more local ports to a pod. ``` -kubectl port-forward -p POD_NAME [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] +kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] ``` ### Examples @@ -49,16 +49,16 @@ kubectl port-forward -p POD_NAME [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REM ``` // 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 +$ kubectl port-forward mypod 5000 6000 // listens on port 8888 locally, forwarding to 5000 in the pod -$ kubectl port-forward -p mypod 8888:5000 +$ kubectl port-forward mypod 8888:5000 // listens on a random port locally, forwarding to 5000 in the pod -$ kubectl port-forward -p mypod :5000 +$ kubectl port-forward mypod :5000 // listens on a random port locally, forwarding to 5000 in the pod -$ kubectl port-forward -p mypod 0:5000 +$ kubectl port-forward mypod 0:5000 ``` ### Options @@ -101,7 +101,7 @@ $ kubectl port-forward -p mypod 0:5000 * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-08-05 14:22:30.874544642 +0000 UTC +###### Auto generated by spf13/cobra at 2015-08-07 09:20:43.879885036 +0000 UTC [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_port-forward.md?pixel)]() diff --git a/pkg/kubectl/cmd/portforward.go b/pkg/kubectl/cmd/portforward.go index 196d8f2a321..e6c9b0f1dae 100644 --- a/pkg/kubectl/cmd/portforward.go +++ b/pkg/kubectl/cmd/portforward.go @@ -31,21 +31,21 @@ import ( const ( 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 +$ kubectl port-forward mypod 5000 6000 // listens on port 8888 locally, forwarding to 5000 in the pod -$ kubectl port-forward -p mypod 8888:5000 +$ kubectl port-forward mypod 8888:5000 // listens on a random port locally, forwarding to 5000 in the pod -$ kubectl port-forward -p mypod :5000 +$ kubectl port-forward mypod :5000 // listens on a random port locally, forwarding to 5000 in the pod -$ kubectl port-forward -p mypod 0:5000` +$ kubectl port-forward mypod 0:5000` ) func NewCmdPortForward(f *cmdutil.Factory) *cobra.Command { cmd := &cobra.Command{ - Use: "port-forward -p POD_NAME [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]", + Use: "port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]", Short: "Forward one or more local ports to a pod.", Long: "Forward one or more local ports to a pod.", Example: portforward_example, @@ -55,7 +55,6 @@ func NewCmdPortForward(f *cmdutil.Factory) *cobra.Command { }, } cmd.Flags().StringP("pod", "p", "", "Pod name") - cmd.MarkFlagRequired("pod") // TODO support UID return cmd } @@ -76,9 +75,17 @@ func (*defaultPortForwarder) ForwardPorts(req *client.Request, config *client.Co func RunPortForward(f *cmdutil.Factory, cmd *cobra.Command, args []string, fw portForwarder) error { podName := cmdutil.GetFlagString(cmd, "pod") - if len(podName) == 0 { - return cmdutil.UsageError(cmd, "POD_NAME is required for port-forward") + if len(podName) == 0 && len(args) == 0 { + return cmdutil.UsageError(cmd, "POD is required for port-forward") } + + if len(podName) != 0 { + printDeprecationWarning("port-forward POD", "-p POD") + } else { + podName = args[0] + args = args[1:] + } + if len(args) < 1 { return cmdutil.UsageError(cmd, "at least 1 PORT is required for port-forward") } diff --git a/pkg/kubectl/cmd/portforward_test.go b/pkg/kubectl/cmd/portforward_test.go index 9a7ce55aaf5..f626c7b253a 100644 --- a/pkg/kubectl/cmd/portforward_test.go +++ b/pkg/kubectl/cmd/portforward_test.go @@ -41,6 +41,68 @@ func (f *fakePortForwarder) ForwardPorts(req *client.Request, config *client.Con func TestPortForward(t *testing.T) { version := testapi.Version() + tests := []struct { + name, version, podPath, pfPath, container string + pod *api.Pod + pfErr bool + }{ + { + name: "pod portforward", + version: version, + podPath: "/api/" + version + "/namespaces/test/pods/foo", + pfPath: "/api/" + version + "/namespaces/test/pods/foo/portforward", + pod: execPod(), + }, + { + name: "pod portforward error", + version: version, + podPath: "/api/" + version + "/namespaces/test/pods/foo", + pfPath: "/api/" + version + "/namespaces/test/pods/foo/portforward", + pod: execPod(), + pfErr: true, + }, + } + for _, test := range tests { + f, tf, codec := NewAPIFactory() + tf.Client = &client.FakeRESTClient{ + Codec: codec, + Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) { + switch p, m := req.URL.Path, req.Method; { + case p == test.podPath && m == "GET": + body := objBody(codec, test.pod) + return &http.Response{StatusCode: 200, Body: body}, nil + default: + // Ensures no GET is performed when deleting by name + t.Errorf("%s: unexpected request: %#v\n%#v", test.name, req.URL, req) + return nil, nil + } + }), + } + tf.Namespace = "test" + tf.ClientConfig = &client.Config{Version: test.version} + ff := &fakePortForwarder{} + if test.pfErr { + ff.pfErr = fmt.Errorf("pf error") + } + cmd := &cobra.Command{} + cmd.Flags().StringP("pod", "p", "", "Pod name") + err := RunPortForward(f, cmd, []string{"foo", ":5000", ":1000"}, ff) + + if test.pfErr && err != ff.pfErr { + t.Errorf("%s: Unexpected exec error: %v", test.name, err) + } + if !test.pfErr && ff.req.URL().Path != test.pfPath { + t.Errorf("%s: Did not get expected path for portforward request", test.name) + } + if !test.pfErr && err != nil { + t.Errorf("%s: Unexpected error: %v", test.name, err) + } + } +} + +func TestPortForwardWithPFlag(t *testing.T) { + version := testapi.Version() + tests := []struct { name, version, podPath, pfPath, container string pod *api.Pod diff --git a/test/e2e/kubectl.go b/test/e2e/kubectl.go index 39df40e6a86..de28ba3b4a3 100644 --- a/test/e2e/kubectl.go +++ b/test/e2e/kubectl.go @@ -169,7 +169,7 @@ var _ = Describe("Kubectl client", func() { }) It("should support port-forward", func() { By("forwarding the container port to a local port") - cmd := kubectlCmd("port-forward", fmt.Sprintf("--namespace=%v", ns), "-p", simplePodName, fmt.Sprintf(":%d", simplePodPort)) + cmd := kubectlCmd("port-forward", fmt.Sprintf("--namespace=%v", ns), simplePodName, fmt.Sprintf(":%d", simplePodPort)) defer tryKill(cmd) // This is somewhat ugly but is the only way to retrieve the port that was picked // by the port-forward command. We don't want to hard code the port as we have no