@@ -471,48 +471,85 @@ function cat_flat() {
471471}
472472
473473function k8s_namespace() {
474+ local ns=" $1 "
475+ local ec
476+
474477 if [[ $# -eq 0 ]] ; then
475- kubectl get ns -o ' jsonpath={.items[*].metadata.name}' | tr ' ' ' \n' | grep -vE ' (kube-.*|yandex-.*)' | sed " s/$cur_ns /$cur_ns ←/"
476- return $?
478+ if [[ -f " $HOME /.bin/fzf" || -d " $HOME /.fzf" ]] ; then
479+ ns=$( kubectl get ns | grep -vE ' (kube|yandex)' | fzf --header-lines=1 --height 20% --reverse | awk ' {print $1}' )
480+ [[ -z " $ns " ]] && return 1
481+ else
482+ echo " Usage: kn {namespace|-}"
483+ return 0
484+ fi
477485 fi
478486
479- kubectl config set-context --current --namespace= " $1 "
487+ local cur_ns
480488
481- return $?
489+ if [[ " $ns " == " -" ]] ; then
490+ cur_ns=$( kubectl config view --minify --output ' jsonpath={..namespace}' )
491+ kubectl get ns -o ' jsonpath={.items[*].metadata.name}' | tr ' ' ' \n' | grep -vE ' (kube-|yandex-)' | sed " s#$cur_ns #$cur_ns ←—#"
492+ return 0
493+ fi
494+
495+ if kubectl config set-context --current --namespace=" $ns " ; then
496+ echo " Current namespace is set to \" $ns \" ."
497+ fi
498+
499+ return 1
482500}
483501
484502function k8s_log() {
503+ local resource=" $1 "
504+ local follow
505+
506+ if [[ " $1 " == " -f" || " $2 " == " -f" ]] ; then
507+ follow=true
508+ fi
509+
485510 if [[ $# -eq 0 ]] ; then
486- echo " Usage: kl {resource} {-f|--follow}"
487- return 0
511+ if [[ -f " $HOME /.bin/fzf" || -d " $HOME /.fzf" ]] ; then
512+ resource=$( kubectl get pods | fzf --header-lines=1 --height 20% --reverse | awk ' {print $1}' )
513+ [[ -z " $resource " ]] && return 1
514+ else
515+ echo " Usage: kl {resource} {-f}"
516+ return 0
517+ fi
488518 fi
489519
490520 if [[ -f " $HOME /.bin/lj" ]] ; then
491- if [[ " $2 " == " -f " || " $2 " == " -- follow" ]] ; then
492- kubectl logs " $1 " -f | lj -F
521+ if [[ -n " $ follow" ]] ; then
522+ kubectl logs " $resource " -f | lj -F
493523 return $?
494524 else
495- kubectl logs " $1 " | lj -P
525+ kubectl logs " $resource " | lj -P
496526 return $?
497527 fi
498528 else
499- if [[ " $2 " == " -f " || " $2 " == " -- follow" ]] ; then
500- kubectl logs " $1 " -f
529+ if [[ -n " $ follow" ]] ; then
530+ kubectl logs " $resource " -f
501531 return $?
502532 else
503- kubectl logs " $1 "
533+ kubectl logs " $resource "
504534 return $?
505535 fi
506536 fi
507537}
508538
509539function k8s_shell() {
540+ local pod=" $1 "
541+
510542 if [[ $# -eq 0 ]] ; then
511- echo " Usage: ks {pod}"
512- return 0
543+ if [[ -f " $HOME /.bin/fzf" || -d " $HOME /.fzf" ]] ; then
544+ pod=$( kubectl get pods --field-selector=status.phase=Running | fzf --header-lines=1 --height 20% --reverse | awk ' {print $1}' )
545+ [[ -z " $pod " ]] && return 1
546+ else
547+ echo " Usage: ks {pod}"
548+ return 0
549+ fi
513550 fi
514551
515- kubectl exec -it " $1 " -- /bin/sh
552+ kubectl exec -it " $pod " -- /bin/sh
516553
517554 return $?
518555}
0 commit comments