你插件中的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