enable KCONTEXT to be set from env for all created k8s commands

This commit is contained in:
hophacker 2020-02-27 21:36:25 +08:00
parent e2baa7ef69
commit 6b23e135af

View File

@ -234,17 +234,17 @@ function gitcopy() {
fi fi
} }
function kubectl() { function kubectl() {
CONTEXT=${CONTEXT:-gcloud} KCONTEXT=${KCONTEXT:-gcloud}
DEBUG=false DEBUG=false
finalopts=() finalopts=()
while [[ $@ != "" ]] do while [[ $@ != "" ]] do
case $1 in case $1 in
-c) -c)
CONTEXT="$2" KCONTEXT="$2"
shift; shift shift; shift
;; ;;
--context=*) --context=*)
CONTEXT="${i#*=}" KCONTEXT="${i#*=}"
shift shift
;; ;;
--debug) --debug)
@ -257,20 +257,20 @@ function kubectl() {
;; ;;
esac esac
done done
[[ $DEBUG == "true" ]] && echo "kubectl $finalopts --kubeconfig=$HOME/.kube/${CONTEXT}_config" [[ $DEBUG == "true" ]] && echo "kubectl $finalopts --kubeconfig=$HOME/.kube/${KCONTEXT}_config"
command kubectl $finalopts --kubeconfig=$HOME/.kube/${CONTEXT}_config command kubectl $finalopts --kubeconfig=$HOME/.kube/${KCONTEXT}_config
} }
function stern { function stern {
CONTEXT=gcloud KCONTEXT=${KCONTEXT:-gcloud}
finalopts=() finalopts=()
while [[ $@ != "" ]] do while [[ $@ != "" ]] do
case $1 in case $1 in
-c) -c)
CONTEXT="$2" KCONTEXT="$2"
shift; shift shift; shift
;; ;;
--context=*) --context=*)
CONTEXT="${i#*=}" KCONTEXT="${i#*=}"
shift shift
;; ;;
*) *)
@ -279,21 +279,21 @@ function stern {
;; ;;
esac esac
done done
echo "stern $finalopts --kubeconfig=$HOME/.kube/${CONTEXT}_config" echo "stern $finalopts --kubeconfig=$HOME/.kube/${KCONTEXT}_config"
command stern $finalopts -t --since 10m --kubeconfig=$HOME/.kube/${CONTEXT}_config command stern $finalopts -t --since 10m --kubeconfig=$HOME/.kube/${KCONTEXT}_config
} }
function helm() { function helm() {
CONTEXT=${CONTEXT:-gcloud} KCONTEXT=${KCONTEXT:-gcloud}
DEBUG=false DEBUG=false
finalopts=() finalopts=()
while [[ $@ != "" ]] do while [[ $@ != "" ]] do
case $1 in case $1 in
-c) -c)
CONTEXT="$2" KCONTEXT="$2"
shift; shift shift; shift
;; ;;
--context=*) --context=*)
CONTEXT="${i#*=}" KCONTEXT="${i#*=}"
shift shift
;; ;;
--debug) --debug)
@ -308,20 +308,20 @@ function helm() {
esac esac
done done
TLS="" TLS=""
case $CONTEXT in case $KCONTEXT in
gcloud) gcloud)
TLS="--tls" TLS="--tls"
esac esac
[[ $DEBUG == "true" ]] && echo "helm $finalopts $TLS --kubeconfig=$HOME/.kube/${CONTEXT}_config" [[ $DEBUG == "true" ]] && echo "helm $finalopts $TLS --kubeconfig=$HOME/.kube/${KCONTEXT}_config"
command helm $finalopts $TLS --kubeconfig=$HOME/.kube/${CONTEXT}_config command helm $finalopts $TLS --kubeconfig=$HOME/.kube/${KCONTEXT}_config
} }
function kexec { function kexec {
RAN=false RAN=false
CONTEXT=gcloud KCONTEXT=${KCONTEXT:-gcloud}
while getopts ":c:rp:" opt; do while getopts ":c:rp:" opt; do
case "${opt}" in case "${opt}" in
c) c)
CONTEXT=$OPTARG KCONTEXT=$OPTARG
;; ;;
r) r)
RAN=true RAN=true
@ -339,7 +339,7 @@ function kexec {
RUNNING_POD_INDEX=-1 RUNNING_POD_INDEX=-1
while true; do while true; do
ALL_PODS=$(kubectl -c $CONTEXT get pods | grep "$PROJECT") ALL_PODS=$(kubectl -c $KCONTEXT get pods | grep "$PROJECT")
echo $fg[green]"All Pods:"$reset_color echo $fg[green]"All Pods:"$reset_color
echo $ALL_PODS echo $ALL_PODS
if [[ ${#ALL_PODS[@]} == 0 ]]; then if [[ ${#ALL_PODS[@]} == 0 ]]; then
@ -384,7 +384,7 @@ function kexec {
done done
if [[ $RUNNING_POD_INDEX != -1 ]]; then if [[ $RUNNING_POD_INDEX != -1 ]]; then
echo "executing pod $fg[green]$RUNNING_PODS[$RUNNING_POD_INDEX]$reset_color" echo "executing pod $fg[green]$RUNNING_PODS[$RUNNING_POD_INDEX]$reset_color"
kubectl -c $CONTEXT exec -it $RUNNING_PODS[$RUNNING_POD_INDEX] $@ kubectl -c $KCONTEXT exec -it $RUNNING_PODS[$RUNNING_POD_INDEX] $@
fi fi
} }
@ -411,8 +411,8 @@ function klogs {
;; ;;
esac esac
done done
shift $(($OPTIND - 1))
k -c $CONTEXT logs -f deployment/$PROJECT-chart --all-containers=true --since=5s --pod-running-timeout=2s "$@" kubectl -c $KCONTEXT logs -f deployment/$PROJECT --all-containers=true --since=5s --pod-running-timeout=2s $finalopts
} }
function rgm { function rgm {