-
Notifications
You must be signed in to change notification settings - Fork 3
/
kubectl-mi
executable file
·242 lines (191 loc) · 7.14 KB
/
kubectl-mi
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
INFO='Select multiple context (up to 3 for diff) with TAB then ENTER '
DEBUG=true
CMD=""
SCRIPT="echo 'NO_CMD'"
CHECK_SCRIPT=""
NS=""
SELECTED=""
INPUT=""
CTX_LIST=""
# handle ctrl+c and remove temp files
trap ctrl_c INT
function ctrl_c() {
# echo "Temp files removing"
eval "rm output.log"
}
usage() {
if [ -n "$1" ] ; then
echo $1;
fi
cat <<"EOF"
USAGE:
kubectl mi scale|diff|log <namespace>
EOF
}
get_state(){
RESOURCE_TYPE="$( printf 'pods\ndeployments\nconfigmaps\nservices\nstatefulsets\nendpoints\nevents\nlimitranges\npersistentvolumeclaims\npodtemplates\nreplicationcontrollers\nresourcequotas\nsecrets\nserviceaccounts\nhorizontalpodautoscalers\ncronjobs\njobs\ndaemonsets\ningresses\nnetworkpolicies\nreplicasets\nalertmanagers\nprometheuses\nprometheusrules\nservicemonitors\nnetworkpolicies\nrolebindings\nroles\ndaemonsets\nreplicasets' | fzf -1 --ansi)";
SCRIPT=""
CHECK_SCRIPT="kubectl get $RESOURCE_TYPE -n $NS -o wide -w >> output.log || true &"
run_cmd_on_multiple_context
tail_output
}
exec(){
RESOURCE_TYPE="$( printf 'deployments\nstatefulsets\ndaemonsets' | fzf -1 --ansi)";
RESOURCE1="$( kubectl get $RESOURCE_TYPE -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf --ansi -1 )"
#-o json | jq '.spec.template.spec.containers[].name'
POD="$(kubectl get pods -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf -1 --ansi)"
echo $POD;
CONTAINER="$(kubectl get pod $POD -n $NS -o go-template --template '{{range .spec.containers}}{{.name}}{{"\n"}}{{end}}' | fzf -1 --ansi )"
echo $CONTAINER;
SCRIPT="kubectl exec -ti $POD -n $NS -c $CONTAINER -- /bin/sh"
run_cmd_on_multiple_context
}
edit(){
RESOURCE_TYPE="$( printf 'pods\ndeployments\nconfigmaps\nservices\nstatefulsets\nendpoints\nevents\nlimitranges\npersistentvolumeclaims\npodtemplates\nreplicationcontrollers\nresourcequotas\nsecrets\nserviceaccounts\nhorizontalpodautoscalers\ncronjobs\njobs\ndaemonsets\ningresses\nnetworkpolicies\nreplicasets\nalertmanagers\nprometheuses\nprometheusrules\nservicemonitors\nnetworkpolicies\nrolebindings\nroles\ndaemonsets\nreplicasets' | fzf -1 --ansi)";
RESOURCE1="$( kubectl get $RESOURCE_TYPE -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf --ansi -1 )"
SCRIPT="kubectl edit $RESOURCE_TYPE $RESOURCE1 -n $NS"
CHECK_SCRIPT="kubectl get $RESOURCE_TYPE $RESOURCE1 -n $NS -o wide -w >> output.log &"
run_cmd_on_multiple_context
tail_output
}
it_select(){
echo "parameter $1 "
SELECTED="$( ${P1} | fzf -1 --ansi )"
}
log(){
POD="$(kubectl get pods -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf -1 --ansi)"
echo $POD;
CONTAINER="$(kubectl get pod $POD -n $NS -o go-template --template '{{range .spec.containers}}{{.name}}{{"\n"}}{{end}}' | fzf -1 --ansi )"
echo $CONTAINER;
SCRIPT="kubectl logs $POD -n $NS -c $CONTAINER "
}
difference(){
RESOURCE_TYPE="$( printf 'pods\ndeployments\nconfigmaps\nservices\nstatefulsets\nendpoints\nevents\nlimitranges\npersistentvolumeclaims\npodtemplates\nreplicationcontrollers\nresourcequotas\nsecrets\nserviceaccounts\nhorizontalpodautoscalers\ncronjobs\njobs\ndaemonsets\ningresses\nnetworkpolicies\nreplicasets\nalertmanagers\nprometheuses\nprometheusrules\nservicemonitors\nnetworkpolicies\nrolebindings\nroles' | fzf -1 --ansi)";
RESOURCE1="$( kubectl get $RESOURCE_TYPE -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf --ansi -1 )"
INDEX=0
LENGTH=${#CTX_LIST[@]}
if [[ $LENGTH -gt 3 && $CMD == "scale" ]]; then
echo "!!! You could only check differences of resource up to 3 cluster. This will run for first 3 cluster.!!!!"
fi
while [ "$INDEX" -lt "$LENGTH" ]; do
switch_context ${CTX_LIST[$INDEX]}
eval "kubectl get $RESOURCE_TYPE $RESOURCE1 -n $NS -o yaml > temp$INDEX.yaml"
let INDEX=INDEX+1;
done;
if [[ $INDEX == 3 ]];then
eval "diff3 temp0.yaml temp1.yaml temp2.yaml"
eval "rm temp0.yaml temp1.yaml temp2.yaml"
else
eval "diff -ys --suppress-common-lines temp0.yaml temp1.yaml"
eval "rm temp0.yaml temp1.yaml"
fi
}
scale(){
RESOURCE_TYPE="$( printf 'deployments\nstatefulsets\nreplicationcontrollers\ndaemonsets\nreplicasets' | fzf -1 --ansi)";
RESOURCES=($(eval "kubectl get $RESOURCE_TYPE -n $NS --output=jsonpath={.items..metadata.name}" | tr -s '[[:space:]]' '\n' | sort -n | fzf --ansi -m --header=${INFO} | sed "s/ /\n/g" ));
# RESOURCE1="$( kubectl get $RESOURCE_TYPE -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf --ansi -1 )"
INDX=0
LENGTH=${#RESOURCES[@]}
echo $LENGTH
while [ "$INDX" -lt "$LENGTH" ]; do
echo "Current Status:"
eval "kubectl get $RESOURCE_TYPE ${RESOURCES[$INDX]} -n $NS"
echo "Enter replica count:"
read SCALE
SCRIPT="kubectl scale $RESOURCE_TYPE ${RESOURCES[$INDX]} -n $NS --replicas=$SCALE"
CHECK_SCRIPT="kubectl get po -n $NS -o wide -w >> output.log &"
run_cmd_on_multiple_context
let INDX=INDX+1;
done;
tail_output
}
select_contexts(){
CTX_LIST=($(eval "kubectl config get-contexts -o=name" | sort -n | fzf --ansi -m --header=${INFO} | sed "s/ /\n/g" ));
if [[ -z "${CTX_LIST}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
fi
}
select_ns(){
if [[ "$PARAMETER" == " " ]];then
NS="$(kubectl get ns --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf -1 )"
else
NS="$PARAMETER"
fi
echo "namespace: $NS";
}
switch_context(){
echo "Context switched to $1"
kubectl config use-context $1
}
run_cmd_on_multiple_context(){
INDEX=0
LENGTH=${#CTX_LIST[@]}
while [ "$INDEX" -lt "$LENGTH" ]; do
if [[ $INDEX != 0 ]]; then
switch_context ${CTX_LIST[$INDEX]}
fi
eval $SCRIPT
if [[ $CHECK_SCRIPT != "" ]]; then
eval "nohup echo ${CTX_LIST[$INDEX]} >> output.log | $CHECK_SCRIPT"
fi
let INDEX=INDEX+1;
done;
}
tail_output(){
eval "tail -f output.log"
rm output.log
}
get_interactive_parameter(){
response=
echo -n "$1 > "
read response
if [ -n "$response" ]; then
INPUT=$response
fi
}
read_parameters(){
COUNTER=1
PARAMETER=" ";
if [[ "$#" == 1 ]]; then
CMD=$1;
elif [[ "$#" -gt 1 ]]; then
while [ $COUNTER -le "$#" ]; do
case "${@:$COUNTER:1}" in "scale"|"diff"|"log"|"exec"|"check-state"|"edit")
CMD=${@:$COUNTER:1} ;;
*)
PARAMETER="${@:$COUNTER:1}"
esac
let COUNTER=COUNTER+1
done
fi
if [[ $CMD == "" ]];then
CMD="$( printf 'scale\ndiff\nlog\nedit\ncheck-state' | fzf -1 --ansi)";
fi
select_contexts
switch_context ${CTX_LIST[0]}
select_ns
if [[ $CMD == "scale" ]];then
scale
elif [[ $CMD == "diff" ]];then
difference
elif [[ $CMD == "edit" ]];then
edit
elif [[ $CMD == "check-state" ]];then
get_state
elif [[ $CMD == "exec" ]];then
exec
elif [[ $CMD == "log" || $CMD == "logs" ]];then
log
elif [[ $CMD == "help" || $CMD == "-help" || $CMD == "-h" ]];then
usage
fi
}
main() {
read_parameters "$@";
}
main "$@"