From 4634073d0b979d2827106a0a1010456ac420baa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Wed, 10 May 2023 10:41:43 +0300 Subject: [PATCH] Use getter function for plugin subcommand resolution Plugin subcommand resolution is relatively less used than the builtin subcommands. That's why, instead always initializing a hash map on memory, it would be better to use a getter function only serves as needed. In addition to that this function will be exported that external libraries can use it. --- staging/src/k8s.io/kubectl/pkg/cmd/cmd.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go b/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go index 4f843ea1b25..8c1b316d1c9 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go @@ -84,10 +84,6 @@ import ( const kubectlCmdHeaders = "KUBECTL_COMMAND_HEADERS" -var ( - allowedCmdsSubcommandPlugin = map[string]struct{}{"create": {}} -) - type KubectlOptions struct { PluginHandler PluginHandler Arguments []string @@ -146,7 +142,7 @@ func NewDefaultKubectlCommandWithArgs(o KubectlOptions) *cobra.Command { if cmdutil.CmdPluginAsSubcommand.IsEnabled() { // Command exists(e.g. kubectl create), but it is not certain that // subcommand also exists (e.g. kubectl create networkpolicy) - if _, ok := allowedCmdsSubcommandPlugin[foundCmd.Name()]; ok { + if _, ok := GetAllowedCmdsAsSubcommandPlugins()[foundCmd.Name()]; ok { var subcommand string for _, arg := range foundArgs { // first "non-flag" argument as subcommand if !strings.HasPrefix(arg, "-") { @@ -176,6 +172,13 @@ func NewDefaultKubectlCommandWithArgs(o KubectlOptions) *cobra.Command { return cmd } +// GetAllowedCmdsAsSubcommandPlugins returns the list of builtin commands +// that plugins are allowed to be used as subcommands only if the passed subcommand +// does not exist as builtin. +func GetAllowedCmdsAsSubcommandPlugins() map[string]struct{} { + return map[string]struct{}{"create": {}} +} + // PluginHandler is capable of parsing command line arguments // and performing executable filename lookups to search // for valid plugin files, and execute found plugins.