-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kubectl binary plugins #37499
Merged
k8s-github-robot
merged 2 commits into
kubernetes:master
from
fabianofranz:kubectl_plugins
Apr 28, 2017
Merged
kubectl binary plugins #37499
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
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() { | ||
return nil | ||
} | ||
|
||
return &cobra.Command{ | ||
Use: plugin.Name, | ||
Short: plugin.ShortDesc, | ||
Long: templates.LongDesc(plugin.LongDesc), | ||
Example: templates.Examples(plugin.Example), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := plugins.RunningContext{ | ||
In: in, | ||
Out: out, | ||
ErrOut: errout, | ||
Args: args, | ||
Env: os.Environ(), | ||
WorkingDir: plugin.Dir, | ||
} | ||
if err := runner.Run(plugin, ctx); err != nil { | ||
cmdutil.CheckErr(err) | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||
"k8s.io/kubernetes/pkg/kubectl/plugins" | ||
) | ||
|
||
type mockPluginRunner struct { | ||
success bool | ||
} | ||
|
||
func (r *mockPluginRunner) Run(p *plugins.Plugin, ctx plugins.RunningContext) error { | ||
if !r.success { | ||
return fmt.Errorf("oops %s", p.Name) | ||
} | ||
ctx.Out.Write([]byte(fmt.Sprintf("ok: %s", p.Name))) | ||
return nil | ||
} | ||
|
||
func TestPluginCmd(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
plugin *plugins.Plugin | ||
expectedSuccess bool | ||
expectedNilCmd bool | ||
}{ | ||
{ | ||
name: "success", | ||
plugin: &plugins.Plugin{ | ||
Description: plugins.Description{ | ||
Name: "success", | ||
ShortDesc: "The Test Plugin", | ||
Command: "echo ok", | ||
}, | ||
}, | ||
expectedSuccess: true, | ||
}, | ||
{ | ||
name: "incomplete", | ||
plugin: &plugins.Plugin{ | ||
Description: plugins.Description{ | ||
Name: "incomplete", | ||
ShortDesc: "The Incomplete Plugin", | ||
}, | ||
}, | ||
expectedNilCmd: true, | ||
}, | ||
{ | ||
name: "failure", | ||
plugin: &plugins.Plugin{ | ||
Description: plugins.Description{ | ||
Name: "failure", | ||
ShortDesc: "The Failing Plugin", | ||
Command: "false", | ||
}, | ||
}, | ||
expectedSuccess: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
inBuf := bytes.NewBuffer([]byte{}) | ||
outBuf := bytes.NewBuffer([]byte{}) | ||
errBuf := bytes.NewBuffer([]byte{}) | ||
|
||
cmdutil.BehaviorOnFatal(func(str string, code int) { | ||
errBuf.Write([]byte(str)) | ||
}) | ||
|
||
runner := &mockPluginRunner{ | ||
success: test.expectedSuccess, | ||
} | ||
|
||
cmd := NewCmdForPlugin(test.plugin, runner, inBuf, outBuf, errBuf) | ||
if cmd == nil { | ||
if !test.expectedNilCmd { | ||
t.Fatalf("%s: command was unexpectedly not registered", test.name) | ||
} | ||
continue | ||
} | ||
cmd.Run(cmd, []string{}) | ||
|
||
if test.expectedSuccess && outBuf.String() != fmt.Sprintf("ok: %s", test.plugin.Name) { | ||
t.Errorf("%s: unexpected output: %q", test.name, outBuf.String()) | ||
} | ||
|
||
if !test.expectedSuccess && errBuf.String() != fmt.Sprintf("error: oops %s", test.plugin.Name) { | ||
t.Errorf("%s: unexpected err output: %q", test.name, errBuf.String()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth mentioning from where these plugins are loaded, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it started like that but I didn't like the results, too clumsy for a command help. Ahead we'll probably want something better than relying on just the help as the default command run, then we can think of something better. And we will have proper docs which we can link to.