Merge pull request #105141 from VilledeMontreal/fix/noloadPluginsForComp

Do not try to load plugins for cobra commands
This commit is contained in:
Kubernetes Prow Robot 2021-10-06 12:18:31 -07:00 committed by GitHub
commit dbd8d3bcd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 3 deletions

View File

@ -101,9 +101,25 @@ func NewDefaultKubectlCommandWithArgs(pluginHandler PluginHandler, args []string
// only look for suitable extension executables if
// the specified command does not already exist
if _, _, err := cmd.Find(cmdPathPieces); err != nil {
if err := HandlePluginCommand(pluginHandler, cmdPathPieces); err != nil {
fmt.Fprintf(errout, "Error: %v\n", err)
os.Exit(1)
// Also check the commands that will be added by Cobra.
// These commands are only added once rootCmd.Execute() is called, so we
// need to check them explicitly here.
var cmdName string // first "non-flag" arguments
for _, arg := range cmdPathPieces {
if !strings.HasPrefix(arg, "-") {
cmdName = arg
break
}
}
switch cmdName {
case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
// Don't search for a plugin
default:
if err := HandlePluginCommand(pluginHandler, cmdPathPieces); err != nil {
fmt.Fprintf(errout, "Error: %v\n", err)
os.Exit(1)
}
}
}
}

View File

@ -78,6 +78,50 @@ func TestKubectlCommandHandlesPlugins(t *testing.T) {
name: "test that a plugin does not execute over an existing command by the same name",
args: []string{"kubectl", "version"},
},
// The following tests make sure that commands added by Cobra cannot be shadowed by a plugin
// See https://github.com/kubernetes/kubectl/issues/1116
{
name: "test that a plugin does not execute over Cobra's help command",
args: []string{"kubectl", "help"},
},
{
name: "test that a plugin does not execute over Cobra's __complete command",
args: []string{"kubectl", cobra.ShellCompRequestCmd},
},
{
name: "test that a plugin does not execute over Cobra's __completeNoDesc command",
args: []string{"kubectl", cobra.ShellCompNoDescRequestCmd},
},
// The following tests make sure that commands added by Cobra cannot be shadowed by a plugin
// even when a flag is specified first. This can happen when using aliases.
// See https://github.com/kubernetes/kubectl/issues/1119
{
name: "test that a flag does not break Cobra's help command",
args: []string{"kubectl", "--kubeconfig=/path/to/kubeconfig", "help"},
},
{
name: "test that a flag does not break Cobra's __complete command",
args: []string{"kubectl", "--kubeconfig=/path/to/kubeconfig", cobra.ShellCompRequestCmd},
},
{
name: "test that a flag does not break Cobra's __completeNoDesc command",
args: []string{"kubectl", "--kubeconfig=/path/to/kubeconfig", cobra.ShellCompNoDescRequestCmd},
},
// As for the previous tests, an alias could add a flag without using the = form.
// We don't support this case as parsing the flags becomes quite complicated (flags
// that take a value, versus flags that don't)
// {
// name: "test that a flag with a space does not break Cobra's help command",
// args: []string{"kubectl", "--kubeconfig", "/path/to/kubeconfig", "help"},
// },
// {
// name: "test that a flag with a space does not break Cobra's __complete command",
// args: []string{"kubectl", "--kubeconfig", "/path/to/kubeconfig", cobra.ShellCompRequestCmd},
// },
// {
// name: "test that a flag with a space does not break Cobra's __completeNoDesc command",
// args: []string{"kubectl", "--kubeconfig", "/path/to/kubeconfig", cobra.ShellCompNoDescRequestCmd},
// },
}
for _, test := range tests {
@ -92,6 +136,7 @@ func TestKubectlCommandHandlesPlugins(t *testing.T) {
})
root := NewDefaultKubectlCommandWithArgs(pluginsHandler, test.args, in, out, errOut)
root.SetOut(out)
if err := root.Execute(); err != nil {
t.Fatalf("unexpected error: %v", err)
}