kubectl插件的创建和使用
2020-03-28 本文已影响0人
hyperjiang
制作kubectl
的插件非常简单,只需要创建以kubectl-
作为前缀的可执行文件,并放在系统的可执行文件存放目录(比如/usr/local/bin
)下面,kubectl
就能够自动找到这些插件。下面举个例子作为说明。
我们在/usr/local/bin
下面创建一个kubectl-whoami
文件,内容如下:
#!/bin/bash
# this plugin makes use of the `kubectl config` command in order to output
# information about the current user, based on the currently selected context
kubectl config view --template='{{ range .contexts }}{{ if eq .name "'$(kubectl config current-context)'" }}Current user: {{ .context.user }}{{ end }}{{ end }}'
然后我们把这个文件设为可执行文件:
chmod a+x kubectl-whoami
这样就已经完成了一个插件的制作,这个whoami
的插件可以用于查看当前的k8s
用户,我们可以尝试运行
kubectl whoami
将会看到类似下面的输出:
Current user: minikube
想查看系统装了哪些kubectl
插件,可以运行:
kubectl plugin list
像我们刚刚只制作了一个插件,那么看到的输出应该是:
The following compatible plugins are available:
/usr/local/bin/kubectl-whoami