KEP-3638: Promote plugin resolution to beta (#120663)
* Promote plugin resolution to beta * Not use plugin for kubectl create -f command execution `kubectl create -f` is legitimate command execution and we shouldn't search plugins if user invokes this. * Add integration test for plugin resolution for create command * Reintroduce feature flag to ability to disable it explicitly Kubernetes-commit: 074a8b00840e85cc95fd83b1825b14ab21ad09c4
This commit is contained in:
parent
382968b0b0
commit
c2f0957625
|
@ -142,17 +142,12 @@ func NewDefaultKubectlCommandWithArgs(o KubectlOptions) *cobra.Command {
|
|||
}
|
||||
}
|
||||
} else if err == nil {
|
||||
if cmdutil.CmdPluginAsSubcommand.IsEnabled() {
|
||||
if !cmdutil.CmdPluginAsSubcommand.IsDisabled() {
|
||||
// Command exists(e.g. kubectl create), but it is not certain that
|
||||
// subcommand also exists (e.g. kubectl create networkpolicy)
|
||||
if IsSubcommandPluginAllowed(foundCmd.Name()) {
|
||||
var subcommand string
|
||||
for _, arg := range foundArgs { // first "non-flag" argument as subcommand
|
||||
if !strings.HasPrefix(arg, "-") {
|
||||
subcommand = arg
|
||||
break
|
||||
}
|
||||
}
|
||||
// we also have to eliminate kubectl create -f
|
||||
if IsSubcommandPluginAllowed(foundCmd.Name()) && len(foundArgs) >= 1 && !strings.HasPrefix(foundArgs[0], "-") {
|
||||
subcommand := foundArgs[0]
|
||||
builtinSubcmdExist := false
|
||||
for _, subcmd := range foundCmd.Commands() {
|
||||
if subcmd.Name() == subcommand {
|
||||
|
|
|
@ -29,8 +29,6 @@ import (
|
|||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/cli-runtime/pkg/genericiooptions"
|
||||
"k8s.io/kubectl/pkg/cmd/plugin"
|
||||
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
)
|
||||
|
||||
func TestNormalizationFuncGlobalExistence(t *testing.T) {
|
||||
|
@ -129,7 +127,6 @@ func TestKubectlSubcommandShadowPlugin(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
cmdtesting.WithAlphaEnvs([]cmdutil.FeatureGate{cmdutil.CmdPluginAsSubcommand}, t, func(t *testing.T) {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
pluginsHandler := &testPluginHandler{
|
||||
pluginsDirectory: "plugin/testdata",
|
||||
|
@ -170,7 +167,6 @@ func TestKubectlSubcommandShadowPlugin(t *testing.T) {
|
|||
t.Fatalf("unexpected plugin execution args: expected %q, got %q", test.expectPluginArgs, pluginsHandler.withArgs)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -429,10 +429,20 @@ const (
|
|||
CmdPluginAsSubcommand FeatureGate = "KUBECTL_ENABLE_CMD_SHADOW"
|
||||
)
|
||||
|
||||
// IsEnabled returns true iff environment variable is set to true.
|
||||
// All other cases, it returns false.
|
||||
func (f FeatureGate) IsEnabled() bool {
|
||||
return os.Getenv(string(f)) == "true"
|
||||
}
|
||||
|
||||
// IsDisabled returns true iff environment variable is set to false.
|
||||
// All other cases, it returns true.
|
||||
// This function is used for the cases where feature is enabled by default,
|
||||
// but it may be needed to provide a way to ability to disable this feature.
|
||||
func (f FeatureGate) IsDisabled() bool {
|
||||
return os.Getenv(string(f)) == "false"
|
||||
}
|
||||
|
||||
func AddValidateFlags(cmd *cobra.Command) {
|
||||
cmd.Flags().String(
|
||||
"validate",
|
||||
|
|
Loading…
Reference in New Issue