mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 02:34:03 +00:00
Merge pull request #11138 from feihujiang/portForwardWithoutP
kubectl port-forward accept a pod without -p
This commit is contained in:
commit
9bda3c5998
@ -576,8 +576,6 @@ _kubectl_port-forward()
|
|||||||
two_word_flags+=("-p")
|
two_word_flags+=("-p")
|
||||||
|
|
||||||
must_have_one_flag=()
|
must_have_one_flag=()
|
||||||
must_have_one_flag+=("--pod=")
|
|
||||||
must_have_one_flag+=("-p")
|
|
||||||
must_have_one_noun=()
|
must_have_one_noun=()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,16 +131,16 @@ Forward one or more local ports to a pod.
|
|||||||
.nf
|
.nf
|
||||||
|
|
||||||
// listens on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
|
// 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
|
// 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
|
// 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
|
// 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
|
.fi
|
||||||
.RE
|
.RE
|
||||||
|
@ -100,7 +100,7 @@ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve
|
|||||||
|
|
||||||
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
|
* [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
|
||||||
|
|
||||||
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
||||||
[]()
|
[]()
|
||||||
|
@ -41,7 +41,7 @@ Forward one or more local ports to a pod.
|
|||||||
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
|
### 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
|
// 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
|
// 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
|
// 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
|
// 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
|
### Options
|
||||||
@ -101,7 +101,7 @@ $ kubectl port-forward -p mypod 0:5000
|
|||||||
|
|
||||||
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
|
* [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
|
||||||
|
|
||||||
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
||||||
[]()
|
[]()
|
||||||
|
@ -31,21 +31,21 @@ import (
|
|||||||
const (
|
const (
|
||||||
portforward_example = `
|
portforward_example = `
|
||||||
// listens on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
|
// 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
|
// 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
|
// 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
|
// 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 {
|
func NewCmdPortForward(f *cmdutil.Factory) *cobra.Command {
|
||||||
cmd := &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.",
|
Short: "Forward one or more local ports to a pod.",
|
||||||
Long: "Forward one or more local ports to a pod.",
|
Long: "Forward one or more local ports to a pod.",
|
||||||
Example: portforward_example,
|
Example: portforward_example,
|
||||||
@ -55,7 +55,6 @@ func NewCmdPortForward(f *cmdutil.Factory) *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().StringP("pod", "p", "", "Pod name")
|
cmd.Flags().StringP("pod", "p", "", "Pod name")
|
||||||
cmd.MarkFlagRequired("pod")
|
|
||||||
// TODO support UID
|
// TODO support UID
|
||||||
return cmd
|
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 {
|
func RunPortForward(f *cmdutil.Factory, cmd *cobra.Command, args []string, fw portForwarder) error {
|
||||||
podName := cmdutil.GetFlagString(cmd, "pod")
|
podName := cmdutil.GetFlagString(cmd, "pod")
|
||||||
if len(podName) == 0 {
|
if len(podName) == 0 && len(args) == 0 {
|
||||||
return cmdutil.UsageError(cmd, "POD_NAME is required for port-forward")
|
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 {
|
if len(args) < 1 {
|
||||||
return cmdutil.UsageError(cmd, "at least 1 PORT is required for port-forward")
|
return cmdutil.UsageError(cmd, "at least 1 PORT is required for port-forward")
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,68 @@ func (f *fakePortForwarder) ForwardPorts(req *client.Request, config *client.Con
|
|||||||
func TestPortForward(t *testing.T) {
|
func TestPortForward(t *testing.T) {
|
||||||
version := testapi.Version()
|
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 {
|
tests := []struct {
|
||||||
name, version, podPath, pfPath, container string
|
name, version, podPath, pfPath, container string
|
||||||
pod *api.Pod
|
pod *api.Pod
|
||||||
|
@ -169,7 +169,7 @@ var _ = Describe("Kubectl client", func() {
|
|||||||
})
|
})
|
||||||
It("should support port-forward", func() {
|
It("should support port-forward", func() {
|
||||||
By("forwarding the container port to a local port")
|
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)
|
defer tryKill(cmd)
|
||||||
// This is somewhat ugly but is the only way to retrieve the port that was picked
|
// 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
|
// by the port-forward command. We don't want to hard code the port as we have no
|
||||||
|
Loading…
Reference in New Issue
Block a user