$ ls ./plugin_command_tree kubectl-parent kubectl-parent-subcommand kubectl-parent-subcommand-subsubcommand
当出现多个位置有同名插件时,执行 kubectl plugin list 能够检测出哪些插件由于 PATH 查找顺序原因导致永远不会被执行问题
1 2 3 4 5 6 7 8 9 10
$ kubectl plugin list The following kubectl-compatible plugins are available:
test/fixtures/pkg/kubectl/plugins/kubectl-foo /usr/local/bin/kubectl-foo - warning: /usr/local/bin/kubectl-foo is overshadowed by a similarly named plugin: test/fixtures/pkg/kubectl/plugins/kubectl-foo plugins/kubectl-invalid - warning: plugins/kubectl-invalid identified as a kubectl plugin, but it is not executable
error: 2 plugin warnings were found
三、Golang 的插件辅助库
由于插件机制的变更,导致其他语言编写的插件在实时获取某些配置信息、动态修改 kubectl 配置方面可能造成一定的阻碍;为此 kubernetes 提供了一个 command line runtime package,使用 Go 编写插件,配合这个库可以更加方便的解析和调整 kubectl 的配置信息
官方为了演示如何使用这个 cli-runtime 库编写了一个 namespace 切换的插件(自己白写了…),仓库地址在 Github 上,基本编译使用如下(直接 go get 后编译文件默认为目录名 cmd)
➜ ~ go get k8s.io/sample-cli-plugin/cmd ➜ ~ sudo mv gopath/bin/cmd /usr/local/bin/kubectl-ns ➜ ~ kubectl ns default ➜ ~ kubectl ns --help View or set the current namespace
Usage: ns [new-namespace] [flags]
Examples:
# view the current namespace in your KUBECONFIG kubectl ns
# view all of the namespaces in use by contexts in your KUBECONFIG kubectl ns --list
# switch your current-context to one that contains the desired namespace kubectl ns foo
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 "/Users/mritd/.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 -h, --helphelpfor ns --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. --list if true, print the list of all namespaces in the current KUBECONFIG -n, --namespace string If present, the namespace scope for this CLI request --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") -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