你插件中的kubectl标志
- 2019 年 12 月 4 日
- 笔记
之前由Gianluca Arbezzano在Gianarb.it上发布
这不是一个新的话题,不涉及hacking,但它是每个在设计kubectl插件的人都需要知道。
我最近就在开发一个,必须让用户体验与kubectl相比尽可能友好,因为这是一个好的所要做的事!欺骗其他开发人员,使他们的生活舒适,如果你习惯这样做:
$ kubectl get pod -n your-namespace -L app=http
要从一个特定的命名空间your-namespace获取pod,被标签app=http过滤,要是你的插件也做类似的事情,它将受益于这歌经典get的交互方式,你应该重用这些标志。
示例:设计一个kubectl-plugin,能够对一组容器运行pprof。
我的期望是:
$ kubectl pprof -n your-namespace -n pod-name-go-app
Kubernetes社区用Go编写了很多代码,这意味着有很多库可以重用。
kubernetes/cli-runtime是一个库,它提供了创建kubectl插件的实用工具。他们的一个包叫做genericclioptions,你可以从它的名字中知道,它的目标是显而易见的。
https://github.com/kubernetes/cli-runtime/tree/master/pkg/genericclioptions
// import "github.com/spf13/cobra" // import "github.com/spf13/pflag" // import "k8s.io/cli-runtime/pkg/genericclioptions" // Create the set of flags for your kubect-plugin flags := pflag.NewFlagSet("kubectl-plugin", pflag.ExitOnError) pflag.CommandLine = flags // Configure the genericclioptions streams := genericclioptions.IOStreams{ In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr, } // This set of flags is the one used for the kubectl configuration such as: // cache-dir, cluster-name, namespace, kube-config, insecure, timeout, impersonate, // ca-file and so on kubeConfigFlags := genericclioptions.NewConfigFlags(false) // It is a set of flags related to a specific resource such as: label selector (-L), --all-namespaces, --schema and so on. kubeResouceBuilderFlags := genericclioptions.NewResourceBuilderFlags() var rootCmd = &cobra.Command{ Use: "kubectl-plugin", Short: "My root command", Run: func(cmd *cobra.Command, args []string) { cmd.SetOutput(streams.ErrOut) } } // You can join all this flags to your root command flags.AddFlagSet(rootCmd.PersistentFlags()) kubeConfigFlags.AddFlags(flags) kubeResouceBuilderFlags.AddFlags(flags)
这是输出:
$ kubectl-plugin --help My root command Usage: kubectl-plugin [flags] Flags: --as string Username to impersonate for the operation --as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups. --cache-dir string Default HTTP cache directory (default "/home/gianarb/.kube/http-cache") --certificate-authority string Path to a cert file for the certificate authority --client-certificate string Path to a client certificate file for TLS --client-key string Path to a client key file for TLS --cluster string The name of the kubeconfig cluster to use --context string The name of the kubeconfig context to use -f, --filename strings identifying the resource. -h, --help help for kubectl-pprof --insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure --kubeconfig string Path to the kubeconfig file to use for CLI requests. -n, --namespace string If present, the namespace scope for this CLI request -R, --recursive Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. (default true) --request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0") -l, --selector string Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2) -s, --server string The address and port of the Kubernetes API server --token string Bearer token for authentication to the API server --user string The name of the kubeconfig user to use