Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
9bda3c59
Commit
9bda3c59
authored
Aug 10, 2015
by
Alex Robinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #11138 from feihujiang/portForwardWithoutP
kubectl port-forward accept a pod without -p
parents
91b0787f
90f4c79b
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
89 additions
and
22 deletions
+89
-22
kubectl
contrib/completions/bash/kubectl
+0
-2
kubectl-port-forward.1
docs/man/man1/kubectl-port-forward.1
+4
-4
kubectl_patch.md
docs/user-guide/kubectl/kubectl_patch.md
+1
-1
kubectl_port-forward.md
docs/user-guide/kubectl/kubectl_port-forward.md
+6
-6
portforward.go
pkg/kubectl/cmd/portforward.go
+15
-8
portforward_test.go
pkg/kubectl/cmd/portforward_test.go
+62
-0
kubectl.go
test/e2e/kubectl.go
+1
-1
No files found.
contrib/completions/bash/kubectl
View file @
9bda3c59
...
...
@@ -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
=()
}
...
...
docs/man/man1/kubectl-port-forward.1
View file @
9bda3c59
...
...
@@ -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
...
...
docs/user-guide/kubectl/kubectl_patch.md
View file @
9bda3c59
...
...
@@ -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-0
5 23:27:50.885807613
+0000 UTC
###### Auto generated by spf13/cobra at 2015-08-0
7 09:20:43.877286912
+0000 UTC
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[

]()
...
...
docs/user-guide/kubectl/kubectl_port-forward.md
View file @
9bda3c59
...
...
@@ -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-0
5 14:22:30.874544642
+0000 UTC
###### Auto generated by spf13/cobra at 2015-08-0
7 09:20:43.879885036
+0000 UTC
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[

]()
...
...
pkg/kubectl/cmd/portforward.go
View file @
9bda3c59
...
...
@@ -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"
)
}
...
...
pkg/kubectl/cmd/portforward_test.go
View file @
9bda3c59
...
...
@@ -85,6 +85,68 @@ func TestPortForward(t *testing.T) {
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
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
{}
podPtr
:=
cmd
.
Flags
()
.
StringP
(
"pod"
,
"p"
,
""
,
"Pod name"
)
*
podPtr
=
"foo"
err
:=
RunPortForward
(
f
,
cmd
,
[]
string
{
":5000"
,
":1000"
},
ff
)
...
...
test/e2e/kubectl.go
View file @
9bda3c59
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment