Switch from arguments to an input structure for kubectl command

This commit is contained in:
Maciej Szulik 2021-11-04 16:29:08 +01:00
parent c5aea015c3
commit baab99d04c
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4
6 changed files with 81 additions and 70 deletions

View File

@ -21,6 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/cmd" "k8s.io/kubectl/pkg/cmd"
cmdsanity "k8s.io/kubectl/pkg/cmd/util/sanity" cmdsanity "k8s.io/kubectl/pkg/cmd/util/sanity"
) )
@ -28,7 +29,7 @@ import (
func main() { func main() {
var errorCount int var errorCount int
kubectl := cmd.NewKubectlCommand(os.Stdin, ioutil.Discard, ioutil.Discard) kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: ioutil.Discard, ErrOut: ioutil.Discard}})
errors := cmdsanity.RunCmdChecks(kubectl, cmdsanity.AllCmdChecks, []string{}) errors := cmdsanity.RunCmdChecks(kubectl, cmdsanity.AllCmdChecks, []string{})
for _, err := range errors { for _, err := range errors {
errorCount++ errorCount++

View File

@ -23,6 +23,7 @@ import (
"os" "os"
"github.com/spf13/cobra/doc" "github.com/spf13/cobra/doc"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/cmd" "k8s.io/kubectl/pkg/cmd"
"k8s.io/kubernetes/cmd/genutils" "k8s.io/kubernetes/cmd/genutils"
) )
@ -46,6 +47,6 @@ func main() {
// Set environment variables used by kubectl so the output is consistent, // Set environment variables used by kubectl so the output is consistent,
// regardless of where we run. // regardless of where we run.
os.Setenv("HOME", "/home/username") os.Setenv("HOME", "/home/username")
kubectl := cmd.NewKubectlCommand(bytes.NewReader(nil), ioutil.Discard, ioutil.Discard) kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: ioutil.Discard, ErrOut: ioutil.Discard}})
doc.GenMarkdownTree(kubectl, outDir) doc.GenMarkdownTree(kubectl, outDir)
} }

View File

@ -26,6 +26,7 @@ import (
mangen "github.com/cpuguy83/go-md2man/v2/md2man" mangen "github.com/cpuguy83/go-md2man/v2/md2man"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
kubectlcmd "k8s.io/kubectl/pkg/cmd" kubectlcmd "k8s.io/kubectl/pkg/cmd"
"k8s.io/kubernetes/cmd/genutils" "k8s.io/kubernetes/cmd/genutils"
apiservapp "k8s.io/kubernetes/cmd/kube-apiserver/app" apiservapp "k8s.io/kubernetes/cmd/kube-apiserver/app"
@ -96,7 +97,7 @@ func main() {
} }
case "kubectl": case "kubectl":
// generate manpage for kubectl // generate manpage for kubectl
kubectl := kubectlcmd.NewKubectlCommand(bytes.NewReader(nil), ioutil.Discard, ioutil.Discard) kubectl := kubectlcmd.NewKubectlCommand(kubectlcmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: ioutil.Discard, ErrOut: ioutil.Discard}})
genMarkdown(kubectl, "", outDir) genMarkdown(kubectl, "", outDir)
for _, c := range kubectl.Commands() { for _, c := range kubectl.Commands() {
genMarkdown(c, "kubectl", outDir) genMarkdown(c, "kubectl", outDir)

View File

@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/cmd" "k8s.io/kubectl/pkg/cmd"
"k8s.io/kubernetes/cmd/genutils" "k8s.io/kubernetes/cmd/genutils"
) )
@ -65,7 +66,7 @@ func main() {
// Set environment variables used by kubectl so the output is consistent, // Set environment variables used by kubectl so the output is consistent,
// regardless of where we run. // regardless of where we run.
os.Setenv("HOME", "/home/username") os.Setenv("HOME", "/home/username")
kubectl := cmd.NewKubectlCommand(bytes.NewReader(nil), ioutil.Discard, ioutil.Discard) kubectl := cmd.NewKubectlCommand(cmd.KubectlOptions{IOStreams: genericclioptions.IOStreams{In: bytes.NewReader(nil), Out: ioutil.Discard, ErrOut: ioutil.Discard}})
genYaml(kubectl, "", outDir) genYaml(kubectl, "", outDir)
for _, c := range kubectl.Commands() { for _, c := range kubectl.Commands() {
genYaml(c, "kubectl", outDir) genYaml(c, "kubectl", outDir)

View File

@ -18,7 +18,6 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -82,21 +81,34 @@ import (
const kubectlCmdHeaders = "KUBECTL_COMMAND_HEADERS" const kubectlCmdHeaders = "KUBECTL_COMMAND_HEADERS"
type KubectlOptions struct {
PluginHandler PluginHandler
Arguments []string
ConfigFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
}
// NewDefaultKubectlCommand creates the `kubectl` command with default arguments // NewDefaultKubectlCommand creates the `kubectl` command with default arguments
func NewDefaultKubectlCommand() *cobra.Command { func NewDefaultKubectlCommand() *cobra.Command {
return NewDefaultKubectlCommandWithArgs(NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes), os.Args, os.Stdin, os.Stdout, os.Stderr) return NewDefaultKubectlCommandWithArgs(KubectlOptions{
PluginHandler: NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes),
Arguments: os.Args,
ConfigFlags: genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag(),
IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
})
} }
// NewDefaultKubectlCommandWithArgs creates the `kubectl` command with arguments // NewDefaultKubectlCommandWithArgs creates the `kubectl` command with arguments
func NewDefaultKubectlCommandWithArgs(pluginHandler PluginHandler, args []string, in io.Reader, out, errout io.Writer) *cobra.Command { func NewDefaultKubectlCommandWithArgs(o KubectlOptions) *cobra.Command {
cmd := NewKubectlCommand(in, out, errout) cmd := NewKubectlCommand(o)
if pluginHandler == nil { if o.PluginHandler == nil {
return cmd return cmd
} }
if len(args) > 1 { if len(o.Arguments) > 1 {
cmdPathPieces := args[1:] cmdPathPieces := o.Arguments[1:]
// only look for suitable extension executables if // only look for suitable extension executables if
// the specified command does not already exist // the specified command does not already exist
@ -116,8 +128,8 @@ func NewDefaultKubectlCommandWithArgs(pluginHandler PluginHandler, args []string
case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd: case "help", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
// Don't search for a plugin // Don't search for a plugin
default: default:
if err := HandlePluginCommand(pluginHandler, cmdPathPieces); err != nil { if err := HandlePluginCommand(o.PluginHandler, cmdPathPieces); err != nil {
fmt.Fprintf(errout, "Error: %v\n", err) fmt.Fprintf(o.IOStreams.ErrOut, "Error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
} }
@ -233,8 +245,8 @@ func HandlePluginCommand(pluginHandler PluginHandler, cmdArgs []string) error {
} }
// NewKubectlCommand creates the `kubectl` command and its nested children. // NewKubectlCommand creates the `kubectl` command and its nested children.
func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command { func NewKubectlCommand(o KubectlOptions) *cobra.Command {
warningHandler := rest.NewWarningWriter(err, rest.WarningWriterOptions{Deduplicate: true, Color: term.AllowsColorOutput(err)}) warningHandler := rest.NewWarningWriter(o.IOStreams.ErrOut, rest.WarningWriterOptions{Deduplicate: true, Color: term.AllowsColorOutput(o.IOStreams.ErrOut)})
warningsAsErrors := false warningsAsErrors := false
// Parent command to which all subcommands are added. // Parent command to which all subcommands are added.
cmds := &cobra.Command{ cmds := &cobra.Command{
@ -280,7 +292,10 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code") flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code")
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() kubeConfigFlags := o.ConfigFlags
if kubeConfigFlags == nil {
kubeConfigFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
}
kubeConfigFlags.AddFlags(flags) kubeConfigFlags.AddFlags(flags)
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
matchVersionKubeConfigFlags.AddFlags(flags) matchVersionKubeConfigFlags.AddFlags(flags)
@ -296,11 +311,9 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
// the language, instead of just loading from the LANG env. variable. // the language, instead of just loading from the LANG env. variable.
i18n.LoadTranslations("kubectl", nil) i18n.LoadTranslations("kubectl", nil)
ioStreams := genericclioptions.IOStreams{In: in, Out: out, ErrOut: err}
// Proxy command is incompatible with CommandHeaderRoundTripper, so // Proxy command is incompatible with CommandHeaderRoundTripper, so
// clear the WrapConfigFn before running proxy command. // clear the WrapConfigFn before running proxy command.
proxyCmd := proxy.NewCmdProxy(f, ioStreams) proxyCmd := proxy.NewCmdProxy(f, o.IOStreams)
proxyCmd.PreRun = func(cmd *cobra.Command, args []string) { proxyCmd.PreRun = func(cmd *cobra.Command, args []string) {
kubeConfigFlags.WrapConfigFn = nil kubeConfigFlags.WrapConfigFn = nil
} }
@ -308,72 +321,72 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
{ {
Message: "Basic Commands (Beginner):", Message: "Basic Commands (Beginner):",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
create.NewCmdCreate(f, ioStreams), create.NewCmdCreate(f, o.IOStreams),
expose.NewCmdExposeService(f, ioStreams), expose.NewCmdExposeService(f, o.IOStreams),
run.NewCmdRun(f, ioStreams), run.NewCmdRun(f, o.IOStreams),
set.NewCmdSet(f, ioStreams), set.NewCmdSet(f, o.IOStreams),
}, },
}, },
{ {
Message: "Basic Commands (Intermediate):", Message: "Basic Commands (Intermediate):",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
explain.NewCmdExplain("kubectl", f, ioStreams), explain.NewCmdExplain("kubectl", f, o.IOStreams),
get.NewCmdGet("kubectl", f, ioStreams), get.NewCmdGet("kubectl", f, o.IOStreams),
edit.NewCmdEdit(f, ioStreams), edit.NewCmdEdit(f, o.IOStreams),
delete.NewCmdDelete(f, ioStreams), delete.NewCmdDelete(f, o.IOStreams),
}, },
}, },
{ {
Message: "Deploy Commands:", Message: "Deploy Commands:",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
rollout.NewCmdRollout(f, ioStreams), rollout.NewCmdRollout(f, o.IOStreams),
scale.NewCmdScale(f, ioStreams), scale.NewCmdScale(f, o.IOStreams),
autoscale.NewCmdAutoscale(f, ioStreams), autoscale.NewCmdAutoscale(f, o.IOStreams),
}, },
}, },
{ {
Message: "Cluster Management Commands:", Message: "Cluster Management Commands:",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
certificates.NewCmdCertificate(f, ioStreams), certificates.NewCmdCertificate(f, o.IOStreams),
clusterinfo.NewCmdClusterInfo(f, ioStreams), clusterinfo.NewCmdClusterInfo(f, o.IOStreams),
top.NewCmdTop(f, ioStreams), top.NewCmdTop(f, o.IOStreams),
drain.NewCmdCordon(f, ioStreams), drain.NewCmdCordon(f, o.IOStreams),
drain.NewCmdUncordon(f, ioStreams), drain.NewCmdUncordon(f, o.IOStreams),
drain.NewCmdDrain(f, ioStreams), drain.NewCmdDrain(f, o.IOStreams),
taint.NewCmdTaint(f, ioStreams), taint.NewCmdTaint(f, o.IOStreams),
}, },
}, },
{ {
Message: "Troubleshooting and Debugging Commands:", Message: "Troubleshooting and Debugging Commands:",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
describe.NewCmdDescribe("kubectl", f, ioStreams), describe.NewCmdDescribe("kubectl", f, o.IOStreams),
logs.NewCmdLogs(f, ioStreams), logs.NewCmdLogs(f, o.IOStreams),
attach.NewCmdAttach(f, ioStreams), attach.NewCmdAttach(f, o.IOStreams),
cmdexec.NewCmdExec(f, ioStreams), cmdexec.NewCmdExec(f, o.IOStreams),
portforward.NewCmdPortForward(f, ioStreams), portforward.NewCmdPortForward(f, o.IOStreams),
proxyCmd, proxyCmd,
cp.NewCmdCp(f, ioStreams), cp.NewCmdCp(f, o.IOStreams),
auth.NewCmdAuth(f, ioStreams), auth.NewCmdAuth(f, o.IOStreams),
debug.NewCmdDebug(f, ioStreams), debug.NewCmdDebug(f, o.IOStreams),
}, },
}, },
{ {
Message: "Advanced Commands:", Message: "Advanced Commands:",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
diff.NewCmdDiff(f, ioStreams), diff.NewCmdDiff(f, o.IOStreams),
apply.NewCmdApply("kubectl", f, ioStreams), apply.NewCmdApply("kubectl", f, o.IOStreams),
patch.NewCmdPatch(f, ioStreams), patch.NewCmdPatch(f, o.IOStreams),
replace.NewCmdReplace(f, ioStreams), replace.NewCmdReplace(f, o.IOStreams),
wait.NewCmdWait(f, ioStreams), wait.NewCmdWait(f, o.IOStreams),
kustomize.NewCmdKustomize(ioStreams), kustomize.NewCmdKustomize(o.IOStreams),
}, },
}, },
{ {
Message: "Settings Commands:", Message: "Settings Commands:",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
label.NewCmdLabel(f, ioStreams), label.NewCmdLabel(f, o.IOStreams),
annotate.NewCmdAnnotate("kubectl", f, ioStreams), annotate.NewCmdAnnotate("kubectl", f, o.IOStreams),
completion.NewCmdCompletion(ioStreams.Out, ""), completion.NewCmdCompletion(o.IOStreams.Out, ""),
}, },
}, },
} }
@ -382,7 +395,7 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
filters := []string{"options"} filters := []string{"options"}
// Hide the "alpha" subcommand if there are no alpha commands in this build. // Hide the "alpha" subcommand if there are no alpha commands in this build.
alpha := NewCmdAlpha(f, ioStreams) alpha := NewCmdAlpha(f, o.IOStreams)
if !alpha.HasSubCommands() { if !alpha.HasSubCommands() {
filters = append(filters, alpha.Name()) filters = append(filters, alpha.Name())
} }
@ -393,12 +406,12 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
registerCompletionFuncForGlobalFlags(cmds, f) registerCompletionFuncForGlobalFlags(cmds, f)
cmds.AddCommand(alpha) cmds.AddCommand(alpha)
cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), ioStreams)) cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), o.IOStreams))
cmds.AddCommand(plugin.NewCmdPlugin(ioStreams)) cmds.AddCommand(plugin.NewCmdPlugin(o.IOStreams))
cmds.AddCommand(version.NewCmdVersion(f, ioStreams)) cmds.AddCommand(version.NewCmdVersion(f, o.IOStreams))
cmds.AddCommand(apiresources.NewCmdAPIVersions(f, ioStreams)) cmds.AddCommand(apiresources.NewCmdAPIVersions(f, o.IOStreams))
cmds.AddCommand(apiresources.NewCmdAPIResources(f, ioStreams)) cmds.AddCommand(apiresources.NewCmdAPIResources(f, o.IOStreams))
cmds.AddCommand(options.NewCmdOptions(ioStreams.Out)) cmds.AddCommand(options.NewCmdOptions(o.IOStreams.Out))
// Stop warning about normalization of flags. That makes it possible to // Stop warning about normalization of flags. That makes it possible to
// add the klog flags later. // add the klog flags later.

View File

@ -25,12 +25,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
) )
func TestNormalizationFuncGlobalExistence(t *testing.T) { func TestNormalizationFuncGlobalExistence(t *testing.T) {
// This test can be safely deleted when we will not support multiple flag formats // This test can be safely deleted when we will not support multiple flag formats
root := NewKubectlCommand(os.Stdin, os.Stdout, os.Stderr) root := NewKubectlCommand(KubectlOptions{IOStreams: genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}})
if root.Parent() != nil { if root.Parent() != nil {
t.Fatal("We expect the root command to be returned") t.Fatal("We expect the root command to be returned")
@ -129,14 +128,9 @@ func TestKubectlCommandHandlesPlugins(t *testing.T) {
pluginsHandler := &testPluginHandler{ pluginsHandler := &testPluginHandler{
pluginsDirectory: "plugin/testdata", pluginsDirectory: "plugin/testdata",
} }
_, in, out, errOut := genericclioptions.NewTestIOStreams() ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
cmdutil.BehaviorOnFatal(func(str string, code int) { root := NewDefaultKubectlCommandWithArgs(KubectlOptions{PluginHandler: pluginsHandler, Arguments: test.args, IOStreams: ioStreams})
errOut.Write([]byte(str))
})
root := NewDefaultKubectlCommandWithArgs(pluginsHandler, test.args, in, out, errOut)
root.SetOut(out)
if err := root.Execute(); err != nil { if err := root.Execute(); err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }