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
21584734
Commit
21584734
authored
Apr 25, 2017
by
Fabiano Franz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Plugins are loaded under the 'kubectl plugin' command
parent
2b178ad6
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
71 additions
and
28 deletions
+71
-28
.generated_docs
docs/.generated_docs
+3
-0
kubectl-plugin.1
docs/man/man1/kubectl-plugin.1
+3
-0
kubectl_plugin.md
docs/user-guide/kubectl/kubectl_plugin.md
+3
-0
kubectl_plugin.yaml
docs/yaml/kubectl/kubectl_plugin.yaml
+3
-0
test-cmd-util.sh
hack/make-rules/test-cmd-util.sh
+16
-10
cmd.go
pkg/kubectl/cmd/cmd.go
+1
-18
plugin.go
pkg/kubectl/cmd/plugin.go
+42
-0
No files found.
docs/.generated_docs
View file @
21584734
...
...
@@ -73,6 +73,7 @@ docs/man/man1/kubectl-label.1
docs/man/man1/kubectl-logs.1
docs/man/man1/kubectl-options.1
docs/man/man1/kubectl-patch.1
docs/man/man1/kubectl-plugin.1
docs/man/man1/kubectl-port-forward.1
docs/man/man1/kubectl-proxy.1
docs/man/man1/kubectl-replace.1
...
...
@@ -162,6 +163,7 @@ docs/user-guide/kubectl/kubectl_label.md
docs/user-guide/kubectl/kubectl_logs.md
docs/user-guide/kubectl/kubectl_options.md
docs/user-guide/kubectl/kubectl_patch.md
docs/user-guide/kubectl/kubectl_plugin.md
docs/user-guide/kubectl/kubectl_port-forward.md
docs/user-guide/kubectl/kubectl_proxy.md
docs/user-guide/kubectl/kubectl_replace.md
...
...
@@ -211,6 +213,7 @@ docs/yaml/kubectl/kubectl_label.yaml
docs/yaml/kubectl/kubectl_logs.yaml
docs/yaml/kubectl/kubectl_options.yaml
docs/yaml/kubectl/kubectl_patch.yaml
docs/yaml/kubectl/kubectl_plugin.yaml
docs/yaml/kubectl/kubectl_port-forward.yaml
docs/yaml/kubectl/kubectl_proxy.yaml
docs/yaml/kubectl/kubectl_replace.yaml
...
...
docs/man/man1/kubectl-plugin.1
0 → 100644
View file @
21584734
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.
docs/user-guide/kubectl/kubectl_plugin.md
0 → 100644
View file @
21584734
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.
docs/yaml/kubectl/kubectl_plugin.yaml
0 → 100644
View file @
21584734
This file is autogenerated, but we've stopped checking such files into the
repository to reduce the need for rebases. Please run hack/generate-docs.sh to
populate this file.
hack/make-rules/test-cmd-util.sh
View file @
21584734
...
...
@@ -3684,19 +3684,25 @@ __EOF__
###########
kube::log::status
"Testing kubectl plugins"
#
single plugins path
#
top-level plugin command
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins kubectl
-h
2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'plugin\s\+Runs a command-line plugin'
# no plugins
output_message
=
$(
!
kubectl plugin 2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'no plugins installed'
# single plugins path
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins kubectl plugin 2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'echo\s\+Echoes for test-cmd'
kube::test::if_has_string
"
${
output_message
}
"
'get\s\+The wonderful new plugin-based get!'
kube::test::if_has_string
"
${
output_message
}
"
'error\s\+The tremendous plugin that always fails!'
kube::test::if_has_not_string
"
${
output_message
}
"
'The hello plugin'
kube::test::if_has_not_string
"
${
output_message
}
"
'Incomplete plugin'
# when overriding existing command, both appear in help. TODO handle this to not register plugins that override existing cmd.
kube::test::if_has_string
"
${
output_message
}
"
'get\s\+Display one or many resources'
kube::test::if_has_not_string
"
${
output_message
}
"
'no plugins installed'
# multiple plugins path
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl
-h
2>&1
)
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl
plugin
-h
2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'echo\s\+Echoes for test-cmd'
kube::test::if_has_string
"
${
output_message
}
"
'get\s\+The wonderful new plugin-based get!'
kube::test::if_has_string
"
${
output_message
}
"
'error\s\+The tremendous plugin that always fails!'
...
...
@@ -3709,18 +3715,18 @@ __EOF__
kube::test::if_has_not_string
"
$output_message
{output_message}"
'The wonderful new plugin-based get'
# plugin help
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl hello
-h
2>&1
)
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl
plugin
hello
-h
2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'The hello plugin is a new plugin used by test-cmd to test multiple plugin locations.'
kube::test::if_has_string
"
${
output_message
}
"
'Usage:'
# run plugin
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl hello 2>&1
)
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl
plugin
hello 2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'#hello#'
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl
echo
2>&1
)
output_message
=
$(
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/:test/fixtures/pkg/kubectl/plugins2/ kubectl
plugin
echo
2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'This plugin works!'
output_message
=
$(
!
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/ kubectl hello 2>&1
)
output_message
=
$(
!
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/ kubectl
plugin
hello 2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'unknown command'
output_message
=
$(
!
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/ kubectl error 2>&1
)
output_message
=
$(
!
KUBECTL_PLUGINS_PATH
=
test
/fixtures/pkg/kubectl/plugins/ kubectl
plugin
error 2>&1
)
kube::test::if_has_string
"
${
output_message
}
"
'error: exit status 1'
kube::test::clear_all
...
...
pkg/kubectl/cmd/cmd.go
View file @
21584734
...
...
@@ -347,24 +347,6 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
},
},
}
// Loads plugins and create commands for each plugin identified
loadedPlugins
,
loadErr
:=
f
.
PluginLoader
()
.
Load
()
if
loadErr
!=
nil
{
glog
.
V
(
1
)
.
Infof
(
"Unable to load plugins: %v"
,
loadErr
)
}
pluginRunner
:=
f
.
PluginRunner
()
if
len
(
loadedPlugins
)
>
0
{
pluginCmds
:=
[]
*
cobra
.
Command
{}
for
_
,
p
:=
range
loadedPlugins
{
pluginCmds
=
append
(
pluginCmds
,
NewCmdForPlugin
(
p
,
pluginRunner
,
in
,
out
,
err
))
}
groups
=
append
(
groups
,
templates
.
CommandGroup
{
Message
:
"Plugins:"
,
Commands
:
pluginCmds
,
})
}
groups
.
Add
(
cmds
)
filters
:=
[]
string
{
...
...
@@ -386,6 +368,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
}
cmds
.
AddCommand
(
cmdconfig
.
NewCmdConfig
(
clientcmd
.
NewDefaultPathOptions
(),
out
,
err
))
cmds
.
AddCommand
(
NewCmdPlugin
(
f
,
in
,
out
,
err
))
cmds
.
AddCommand
(
NewCmdVersion
(
f
,
out
))
cmds
.
AddCommand
(
NewCmdApiVersions
(
f
,
out
))
cmds
.
AddCommand
(
NewCmdOptions
())
...
...
pkg/kubectl/cmd/plugin.go
View file @
21584734
...
...
@@ -17,15 +17,57 @@ limitations under the License.
package
cmd
import
(
"fmt"
"io"
"os"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/plugins"
"k8s.io/kubernetes/pkg/util/i18n"
)
var
(
plugin_long
=
templates
.
LongDesc
(
`
Runs a command-line plugin.
Plugins are subcommands that are not part of the major command-line distribution
and can even be provided by third-parties. Please refer to the documentation and
examples for more information about how to install and write your own plugins.`
)
)
// NewCmdPlugin creates the command that is the top-level for plugin commands.
func
NewCmdPlugin
(
f
cmdutil
.
Factory
,
in
io
.
Reader
,
out
,
err
io
.
Writer
)
*
cobra
.
Command
{
// Loads plugins and create commands for each plugin identified
loadedPlugins
,
loadErr
:=
f
.
PluginLoader
()
.
Load
()
if
loadErr
!=
nil
{
glog
.
V
(
1
)
.
Infof
(
"Unable to load plugins: %v"
,
loadErr
)
}
cmd
:=
&
cobra
.
Command
{
Use
:
"plugin NAME"
,
Short
:
i18n
.
T
(
"Runs a command-line plugin"
),
Long
:
plugin_long
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
len
(
loadedPlugins
)
==
0
{
cmdutil
.
CheckErr
(
fmt
.
Errorf
(
"no plugins installed."
))
}
cmdutil
.
DefaultSubCommandRun
(
err
)(
cmd
,
args
)
},
}
if
len
(
loadedPlugins
)
>
0
{
pluginRunner
:=
f
.
PluginRunner
()
for
_
,
p
:=
range
loadedPlugins
{
cmd
.
AddCommand
(
NewCmdForPlugin
(
p
,
pluginRunner
,
in
,
out
,
err
))
}
}
return
cmd
}
// NewCmdForPlugin creates a command capable of running the provided plugin.
func
NewCmdForPlugin
(
plugin
*
plugins
.
Plugin
,
runner
plugins
.
PluginRunner
,
in
io
.
Reader
,
out
,
errout
io
.
Writer
)
*
cobra
.
Command
{
if
!
plugin
.
IsValid
()
{
...
...
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