kubectl 명령어를 alias로 설정 하고 자동완성기능을 사용하려면 bash-completion 패키지에 의존해야한다.
ubuntu = apt-get install bash-completion
redhat = yum install bash-completion
~/.bashrc 파일에 source <(kubectl completion bash) 추가하면 아래 명령어 결과를 확인할 수 있다.
명령어가 성공한 경우 정상적으로 설치되었으며, 명령어가 실패한 경우 ~/.bashrc 파일에source <(kubectl completion bash)를 추가한다.
root@k8s-master1:~# type _init_completion
_init_completion is a function
_init_completion ()
{
local exclude="" flag outx errx inx OPTIND=1;
while getopts "n:e:o:i:s" flag "$@"; do
case $flag in
n)
exclude+=$OPTARG
;;
e)
errx=$OPTARG
;;
o)
outx=$OPTARG
;;
i)
inx=$OPTARG
;;
s)
split=false;
exclude+==
;;
esac;
done;
COMPREPLY=();
local redir="@(?([0-9])<|?([0-9&])>?(>)|>&)";
_get_comp_words_by_ref -n "$exclude<>&" cur prev words cword;
_variables && return 1;
if [[ $cur == $redir* || $prev == $redir ]]; then
local xspec;
case $cur in
2'>'*)
xspec=$errx
;;
*'>'*)
xspec=$outx
;;
*'<'*)
xspec=$inx
;;
*)
case $prev in
2'>'*)
xspec=$errx
;;
*'>'*)
xspec=$outx
;;
*'<'*)
xspec=$inx
;;
esac
;;
esac;
cur="${cur##$redir}";
_filedir $xspec;
return 1;
fi;
local i skip;
for ((i=1; i < ${#words[@]}; 1))
do
if [[ ${words[i]} == $redir* ]]; then
[[ ${words[i]} == $redir ]] && skip=2 || skip=1;
words=("${words[@]:0:i}" "${words[@]:i+skip}");
[[ $i -le $cword ]] && (( cword -= skip ));
else
(( i++ ));
fi;
done;
[[ $cword -le 0 ]] && return 1;
prev=${words[cword-1]};
[[ -n ${split-} ]] && _split_longopt && split=true;
return 0
}
자동 완성 스크립트가 모든 사용자 혹은 현재 사용자에게 활성화 할 수 있다.
-- 현재 사용자 활성화
echo 'source <(kubectl completion bash)' >>~/.bashrc
-- 전체 사용자 활성화
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc