制作kubectl
的插件非常簡單,只需要?jiǎng)?chuàng)建以kubectl-
作為前綴的可執(zhí)行文件笋轨,并放在系統(tǒng)的可執(zhí)行文件存放目錄(比如/usr/local/bin
)下面穿扳,kubectl
就能夠自動(dòng)找到這些插件怀泊。下面舉個(gè)例子作為說明躯畴。
我們?cè)?code>/usr/local/bin下面創(chuàng)建一個(gè)kubectl-whoami
文件民鼓,內(nèi)容如下:
#!/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 }}'
然后我們把這個(gè)文件設(shè)為可執(zhí)行文件:
chmod a+x kubectl-whoami
這樣就已經(jīng)完成了一個(gè)插件的制作,這個(gè)whoami
的插件可以用于查看當(dāng)前的k8s
用戶蓬抄,我們可以嘗試運(yùn)行
kubectl whoami
將會(huì)看到類似下面的輸出:
Current user: minikube
想查看系統(tǒng)裝了哪些kubectl
插件丰嘉,可以運(yùn)行:
kubectl plugin list
像我們剛剛只制作了一個(gè)插件,那么看到的輸出應(yīng)該是:
The following compatible plugins are available:
/usr/local/bin/kubectl-whoami