2020-12-29 08:14:35 +08:00
|
|
|
NAMESPACE=default
|
|
|
|
RUNNING_POD=""
|
|
|
|
LEFT_ARGS=""
|
2021-02-19 17:09:41 +08:00
|
|
|
VERBOSE=false
|
|
|
|
SRC=""
|
|
|
|
DST=""
|
2021-01-17 16:01:09 +08:00
|
|
|
|
|
|
|
# aliases
|
|
|
|
alias k="kubectl"
|
|
|
|
alias kpg="kubectl get pods | grep"
|
|
|
|
alias ksg="kubectl get service | grep"
|
|
|
|
alias k_get_pods_sort_by_time="k get pods --sort-by=.metadata.creationTimestamp"
|
|
|
|
|
2020-12-29 08:14:35 +08:00
|
|
|
function getpod {
|
|
|
|
RAN=true
|
|
|
|
function usage ()
|
|
|
|
{
|
|
|
|
echo "Usage : $0 [options] [--]
|
|
|
|
Options:
|
|
|
|
-K kubectl context
|
2021-02-19 17:09:41 +08:00
|
|
|
-R Not randomly select pod
|
|
|
|
-d Destination
|
|
|
|
-s Source
|
2020-12-29 08:14:35 +08:00
|
|
|
-n NAMESPACE
|
|
|
|
-p PROJECT
|
2021-02-19 17:09:41 +08:00
|
|
|
-v Verbose
|
2020-12-29 08:14:35 +08:00
|
|
|
-h Display this message"
|
|
|
|
}
|
2021-02-19 17:09:41 +08:00
|
|
|
while getopts ":hvs:d:K:Rp:" opt
|
2020-12-29 08:14:35 +08:00
|
|
|
do
|
|
|
|
case $opt in
|
2021-02-19 17:09:41 +08:00
|
|
|
R) RAN=false ;;
|
|
|
|
s) SRC=$OPTARG ;;
|
|
|
|
d) DST=$OPTARG ;;
|
|
|
|
n) NAMESPACE=$OPTARG ;;
|
|
|
|
p) PROJECT=$OPTARG ;;
|
|
|
|
K) KCONTEXT=$OPTARG ;;
|
|
|
|
v) VERBOSE=true ;;
|
|
|
|
h) usage; return 0 ;;
|
2020-12-29 08:14:35 +08:00
|
|
|
*) echo -e "\n Option does not exist: $OPTARG\n"
|
2021-02-19 17:09:41 +08:00
|
|
|
usage; return 1 ;;
|
2020-12-29 08:14:35 +08:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(($OPTIND-1))
|
|
|
|
|
|
|
|
RUNNING_POD_INDEX=-1
|
|
|
|
while true; do
|
|
|
|
echo "kubectl -n $NAMESPACE get pods | grep $PROJECT"
|
|
|
|
ALL_PODS=$(kubectl -n $NAMESPACE get pods | grep "$PROJECT")
|
|
|
|
echo $fg[green]"All Pods:"$reset_color
|
|
|
|
echo $ALL_PODS
|
|
|
|
if [[ ${#ALL_PODS[@]} == 0 ]]; then
|
|
|
|
echo $fg[red]"Pod not found for $PROJECT"$reset_color
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
RUNNING_PODS=($(echo $ALL_PODS | egrep "$PROJECT.* ?[1-9]/[0-9]? *Running" | awk '{print $1}'))
|
2021-01-14 21:15:47 +08:00
|
|
|
if [[ `echo $ALL_PODS | wc -l | trim` != ${#RUNNING_PODS[@]} ]]; then
|
2020-12-29 08:14:35 +08:00
|
|
|
sleep 2
|
|
|
|
echo $fg[red]'Pods are not ready, wait...'$reset_color
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if [[ ${#RUNNING_PODS[@]} == 0 ]]; then
|
|
|
|
echo "Pod not found for $PROJECT"
|
|
|
|
break
|
|
|
|
fi
|
2021-02-21 16:26:54 +08:00
|
|
|
RUNNING_POD_INDEX=$(($RANDOM % ${#RUNNING_PODS[@]} + 1))
|
|
|
|
[[ $RAN == 'true' ]] && break
|
|
|
|
if [ ${#RUNNING_PODS[@]} -gt 1 ];then
|
2020-12-29 08:14:35 +08:00
|
|
|
echo $fg[green]'Running Pods:'$reset_color
|
|
|
|
INDEX=1
|
|
|
|
for i in $RUNNING_PODS;do
|
|
|
|
echo "[$INDEX] $i"
|
|
|
|
let INDEX=${INDEX}+1
|
|
|
|
done
|
|
|
|
echo $fg[green]'Select option of pod to execute:'$reset_color
|
|
|
|
while true;do
|
|
|
|
read RUNNING_POD_INDEX
|
|
|
|
if [[ $RUNNING_POD_INDEX -gt 0 && $RUNNING_POD_INDEX -le ${#RUNNING_PODS[@]} ]];then
|
|
|
|
break
|
|
|
|
else
|
|
|
|
echo 'invalid option...'
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
RUNNING_POD=$RUNNING_PODS[$RUNNING_POD_INDEX]
|
|
|
|
LEFT_ARGS=$@
|
|
|
|
}
|
|
|
|
|
|
|
|
function kexec {
|
|
|
|
getpod $@
|
|
|
|
if [[ $RUNNING_POD != "" ]]; then
|
|
|
|
echo "kubectl -it -n $NAMESPACE exec $RUNNING_POD -- /bin/sh -c $LEFT_ARGS"
|
|
|
|
kubectl -it -n $NAMESPACE exec $RUNNING_POD -- /bin/sh -c $LEFT_ARGS
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-02-19 17:09:41 +08:00
|
|
|
function cmd {
|
|
|
|
if $VERBOSE; then
|
|
|
|
echo "Running: $fg[green]$1$reset_color"
|
|
|
|
fi
|
|
|
|
eval $1
|
|
|
|
}
|
|
|
|
function kcp {
|
|
|
|
getpod $@
|
|
|
|
if [[ $RUNNING_POD != "" ]]; then
|
|
|
|
cmd "kubectl -n $NAMESPACE cp $RUNNING_POD:$SRC $DST"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-29 08:14:35 +08:00
|
|
|
function klogs {
|
|
|
|
finalopts=()
|
|
|
|
while [[ $@ != "" ]] do
|
|
|
|
case $1 in
|
|
|
|
--context=*)
|
|
|
|
KCONTEXT="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
2022-01-09 14:48:13 +08:00
|
|
|
-d)
|
|
|
|
DEPLOYMENT="$2"
|
2020-12-29 08:14:35 +08:00
|
|
|
shift; shift
|
|
|
|
;;
|
|
|
|
-i)
|
|
|
|
INSTANCE="$2"
|
|
|
|
shift; shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
finalopts+=($1)
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2022-01-09 14:48:13 +08:00
|
|
|
if [[ "$DEPLOYMENT" != "" ]]; then
|
|
|
|
kubectl logs -f deployment/$DEPLOYMENT --all-containers=true --since=24h --pod-running-timeout=2s $finalopts
|
2020-12-29 08:14:35 +08:00
|
|
|
elif [[ "$INSTANCE" != "" ]]; then
|
|
|
|
while true; do
|
|
|
|
kubectl logs -f --max-log-requests=10 -l app.kubernetes.io/instance=$INSTANCE 1>&0
|
|
|
|
echo "Waiting..."
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
function k_delete_evicted {
|
|
|
|
k delete pod `k get pods | grep Evicted | awk '{print $1}'`
|
|
|
|
}
|
|
|
|
function k_get_instance {
|
|
|
|
k get pods -o jsonpath="{.items[*].metadata.labels['app\.kubernetes\.io\/instance']}" | tr " " "\n" | uniq
|
|
|
|
}
|
|
|
|
function kubectl() {
|
|
|
|
DEBUG=false
|
|
|
|
finalopts=()
|
|
|
|
while [[ $@ != "" ]] do
|
|
|
|
case $1 in
|
|
|
|
--context=*)
|
|
|
|
KCONTEXT="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--debug)
|
|
|
|
DEBUG=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
finalopts+=("$@")
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
finalopts+=($1)
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
[[ $DEBUG == "true" ]] && echo "kubectl --kubeconfig=$HOME/.kube/${KCONTEXT}_config $finalopts"
|
|
|
|
command kubectl --kubeconfig=$HOME/.kube/${KCONTEXT}_config $finalopts
|
|
|
|
}
|
2021-01-12 08:44:27 +08:00
|
|
|
function k_force_delete_pod () {
|
|
|
|
k delete pod $1 --grace-period=0 --force
|
|
|
|
}
|
2021-01-14 20:22:13 +08:00
|
|
|
function k_get_containers_of_pod {
|
|
|
|
k get pods $1 -o jsonpath='{.spec.containers[*].name}*'
|
|
|
|
}
|
2021-01-17 16:01:09 +08:00
|
|
|
function set_k8s_context_core {
|
|
|
|
C=$1
|
|
|
|
if [[ "$C" == "" ]]; then
|
|
|
|
echo "Select your context:"
|
|
|
|
ls ~/.kube/*_config | xargs -n 1 basename | sed s/_config//g
|
|
|
|
read C
|
|
|
|
fi
|
|
|
|
export KCONTEXT=$C
|
|
|
|
}
|
2021-10-12 02:34:26 +08:00
|
|
|
|
|
|
|
function helm() {
|
|
|
|
DEBUG=false
|
|
|
|
finalopts=()
|
|
|
|
while [[ $@ != "" ]] do
|
|
|
|
case $1 in
|
|
|
|
--context=*)
|
|
|
|
KCONTEXT="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--debug)
|
|
|
|
DEBUG=true
|
|
|
|
finalopts+=($1)
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
finalopts+=($1)
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
[[ $DEBUG == "true" ]] && echo "helm $finalopts --kubeconfig=$HOME/.kube/${KCONTEXT}_config"
|
|
|
|
command helm $finalopts --kubeconfig=$HOME/.kube/${KCONTEXT}_config
|
|
|
|
}
|