-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes.bashrc
103 lines (88 loc) · 2.36 KB
/
kubernetes.bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# kubernetes stuff because I don't want to make another repo
# lighter weight version of kubectx/kubens that uses environment variables
# env variables allow different shells to be in different contexts,
# unlike kubectx which touches config files
alias kube-drain="kubectl drain --force --delete-emptydir-data --ignore-daemonsets --grace-period=0 "
function kubectl() {
cmd="$(which kubectl)"
if [[ "$1" != "config" ]]; then
# add KUBECTX context if defined
if [[ ! -z "${KUBECTX}" && "$@" != *"--context"* ]]; then
cmd="$cmd --context=${KUBECTX}"
fi
# add KUBENS context if defined
if [[ ! -z "${KUBENS}" && "$@" != *"--namespace"* ]]; then
cmd="$cmd --namespace=${KUBENS}"
fi
fi
# echo $cmd "$@"
$cmd "$@"
}
function kctx() {
# set default context
export KUBECTX="$1"
}
function kns() {
# set default namespace
export KUBENS="$1"
}
alias kube-addresses="kubectl get node -o json | jq '.items[] | [.status.addresses[].address,.spec.providerID]'"
function pod() {
# get the first pod matching a prefix
# useful with, e.g.
# kubectl logs -f `pod hub`
# prioritize prefix
pod=
for match in $(kubectl get pod --no-headers | awk '{print $1}' | grep "${1}"); do
if [[ -z "$pod" ]]; then
pod=$match
fi
prefix_pod=$(echo $match | grep "^${1}")
if [[ ! -z "$prefix_pod" ]]; then
pod="$prefix_pod"
break
fi
done
if [[ -z "$pod" ]]; then
# output something to avoid `pod nomatch` returning an empty string
# which can result in performing the operation on all pods (e.g. describe `pod nomatch`)
echo "nosuchpod-${1}"
else
echo "$pod"
fi
}
function kube-delete-pods() {
pat="$1"
shift
pods=$(kubectl get pod --no-headers "$@" | awk '{print $1}' | grep "$pat")
echo "$pods"
read -p "Delete above pods (y/[n])?" yesno
case "$yesno" in
y|Y )
echo kubectl delete pod "$@" $pods;
kubectl delete pod "$@" $pods;
;;
* )
echo "Cancelled"
;;
esac
}
alias kube-watch-not-running="watch 'kubectl get pod | grep -v Running'"
function kx() {
# run in a pod with kubectl exec -it
pod="$1"
shift
kubectl exec -it "$pod" -- sh -c "COLUMNS=$COLUMNS $@"
}
function kxps() {
# run ps aux in a pod
kx "$1" "$@"
}
function kxnbshow() {
# show a notebook
pod="$1"
shift
nb="$1"
shift
kx "$pod" cat "$nb" | nbshow -s "$@" -
}