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
139c62c3
Commit
139c62c3
authored
Feb 13, 2018
by
Shawn Hsiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kubectl port-forward allows using resource name to select a matching pod
parent
245ca8ef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
21 deletions
+53
-21
portforward.go
pkg/kubectl/cmd/portforward.go
+52
-17
portforward_test.go
pkg/kubectl/cmd/portforward_test.go
+1
-4
No files found.
pkg/kubectl/cmd/portforward.go
View file @
139c62c3
...
@@ -23,6 +23,7 @@ import (
...
@@ -23,6 +23,7 @@ import (
"net/url"
"net/url"
"os"
"os"
"os/signal"
"os/signal"
"time"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
...
@@ -51,15 +52,32 @@ type PortForwardOptions struct {
...
@@ -51,15 +52,32 @@ type PortForwardOptions struct {
}
}
var
(
var
(
portforwardLong
=
templates
.
LongDesc
(
i18n
.
T
(
`
Forward one or more local ports to a pod.
Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.
If there are multiple pods matching the criteria, a pod will be selected automatically. The
forwarding session ends when the selected pod terminates, and rerun of the command is needed
to resume forwarding.`
))
portforwardExample
=
templates
.
Examples
(
i18n
.
T
(
`
portforwardExample
=
templates
.
Examples
(
i18n
.
T
(
`
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
kubectl port-forward mypod 5000 6000
kubectl port-forward pod/mypod 5000 6000
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
kubectl port-forward deployment/mydeployment 5000 6000
# Listen on port 8888 locally, forwarding to 5000 in the pod
# Listen on port 8888 locally, forwarding to 5000 in the pod
kubectl port-forward mypod 8888:5000
kubectl port-forward
pod/
mypod 8888:5000
# Listen on a random port locally, forwarding to 5000 in the pod
# Listen on a random port locally, forwarding to 5000 in the pod
kubectl port-forward mypod :5000`
))
kubectl port-forward pod/mypod :5000`
))
)
const
(
// Amount of time to wait until at least one pod is running
defaultPodPortForwardWaitTimeout
=
60
*
time
.
Second
)
)
func
NewCmdPortForward
(
f
cmdutil
.
Factory
,
cmdOut
,
cmdErr
io
.
Writer
)
*
cobra
.
Command
{
func
NewCmdPortForward
(
f
cmdutil
.
Factory
,
cmdOut
,
cmdErr
io
.
Writer
)
*
cobra
.
Command
{
...
@@ -70,10 +88,10 @@ func NewCmdPortForward(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Comma
...
@@ -70,10 +88,10 @@ func NewCmdPortForward(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Comma
},
},
}
}
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"port-forward
POD
[LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]"
,
Use
:
"port-forward
TYPE/NAME
[LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]"
,
DisableFlagsInUseLine
:
true
,
DisableFlagsInUseLine
:
true
,
Short
:
i18n
.
T
(
"Forward one or more local ports to a pod"
),
Short
:
i18n
.
T
(
"Forward one or more local ports to a pod"
),
Long
:
"Forward one or more local ports to a pod."
,
Long
:
portforwardLong
,
Example
:
portforwardExample
,
Example
:
portforwardExample
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
err
:=
opts
.
Complete
(
f
,
cmd
,
args
);
err
!=
nil
{
if
err
:=
opts
.
Complete
(
f
,
cmd
,
args
);
err
!=
nil
{
...
@@ -87,7 +105,7 @@ func NewCmdPortForward(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Comma
...
@@ -87,7 +105,7 @@ func NewCmdPortForward(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Comma
}
}
},
},
}
}
cmd
.
Flags
()
.
StringP
(
"pod"
,
"p"
,
""
,
"Pod name"
)
cmd
util
.
AddPodRunningTimeoutFlag
(
cmd
,
defaultPodPortForwardWaitTimeout
)
// TODO support UID
// TODO support UID
return
cmd
return
cmd
}
}
...
@@ -116,24 +134,41 @@ func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts Po
...
@@ -116,24 +134,41 @@ func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts Po
// Complete completes all the required options for port-forward cmd.
// Complete completes all the required options for port-forward cmd.
func
(
o
*
PortForwardOptions
)
Complete
(
f
cmdutil
.
Factory
,
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
func
(
o
*
PortForwardOptions
)
Complete
(
f
cmdutil
.
Factory
,
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
var
err
error
var
err
error
o
.
PodName
=
cmdutil
.
GetFlagString
(
cmd
,
"pod"
)
if
len
(
args
)
<
2
{
if
len
(
o
.
PodName
)
==
0
&&
len
(
args
)
==
0
{
return
cmdutil
.
UsageErrorf
(
cmd
,
"TYPE/NAME and list of ports are required for port-forward"
)
return
cmdutil
.
UsageErrorf
(
cmd
,
"POD is required for port-forward"
)
}
}
if
len
(
o
.
PodName
)
!=
0
{
o
.
Namespace
,
_
,
err
=
f
.
DefaultNamespace
()
printDeprecationWarning
(
"port-forward POD"
,
"-p POD"
)
if
err
!=
nil
{
o
.
Ports
=
args
return
err
}
else
{
o
.
PodName
=
args
[
0
]
o
.
Ports
=
args
[
1
:
]
}
}
o
.
Namespace
,
_
,
err
=
f
.
DefaultNamespace
()
builder
:=
f
.
NewBuilder
()
.
Internal
()
.
ContinueOnError
()
.
NamespaceParam
(
o
.
Namespace
)
.
DefaultNamespace
()
getPodTimeout
,
err
:=
cmdutil
.
GetPodRunningTimeoutFlag
(
cmd
)
if
err
!=
nil
{
return
cmdutil
.
UsageErrorf
(
cmd
,
err
.
Error
())
}
resourceName
:=
args
[
0
]
builder
.
ResourceNames
(
"pods"
,
resourceName
)
obj
,
err
:=
builder
.
Do
()
.
Object
()
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
forwardablePod
,
err
:=
f
.
AttachablePodForObject
(
obj
,
getPodTimeout
)
if
err
!=
nil
{
return
err
}
o
.
PodName
=
forwardablePod
.
Name
o
.
Ports
=
args
[
1
:
]
clientset
,
err
:=
f
.
ClientSet
()
clientset
,
err
:=
f
.
ClientSet
()
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -157,7 +192,7 @@ func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
...
@@ -157,7 +192,7 @@ func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, arg
// Validate validates all the required options for port-forward cmd.
// Validate validates all the required options for port-forward cmd.
func
(
o
PortForwardOptions
)
Validate
()
error
{
func
(
o
PortForwardOptions
)
Validate
()
error
{
if
len
(
o
.
PodName
)
==
0
{
if
len
(
o
.
PodName
)
==
0
{
return
fmt
.
Errorf
(
"pod name must be specified"
)
return
fmt
.
Errorf
(
"pod name
or resource type/name
must be specified"
)
}
}
if
len
(
o
.
Ports
)
<
1
{
if
len
(
o
.
Ports
)
<
1
{
...
...
pkg/kubectl/cmd/portforward_test.go
View file @
139c62c3
...
@@ -70,6 +70,7 @@ func testPortForward(t *testing.T, flags map[string]string, args []string) {
...
@@ -70,6 +70,7 @@ func testPortForward(t *testing.T, flags map[string]string, args []string) {
var
err
error
var
err
error
f
,
tf
,
codec
,
ns
:=
cmdtesting
.
NewAPIFactory
()
f
,
tf
,
codec
,
ns
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Client
=
&
fake
.
RESTClient
{
tf
.
Client
=
&
fake
.
RESTClient
{
VersionedAPIPath
:
"/api/v1"
,
GroupVersion
:
schema
.
GroupVersion
{
Group
:
""
},
GroupVersion
:
schema
.
GroupVersion
{
Group
:
""
},
NegotiatedSerializer
:
ns
,
NegotiatedSerializer
:
ns
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
...
@@ -131,7 +132,3 @@ func testPortForward(t *testing.T, flags map[string]string, args []string) {
...
@@ -131,7 +132,3 @@ func testPortForward(t *testing.T, flags map[string]string, args []string) {
func
TestPortForward
(
t
*
testing
.
T
)
{
func
TestPortForward
(
t
*
testing
.
T
)
{
testPortForward
(
t
,
nil
,
[]
string
{
"foo"
,
":5000"
,
":1000"
})
testPortForward
(
t
,
nil
,
[]
string
{
"foo"
,
":5000"
,
":1000"
})
}
}
func
TestPortForwardWithPFlag
(
t
*
testing
.
T
)
{
testPortForward
(
t
,
map
[
string
]
string
{
"pod"
:
"foo"
},
[]
string
{
":5000"
,
":1000"
})
}
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