-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkubectl-it
executable file
·176 lines (130 loc) · 5.16 KB
/
kubectl-it
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
#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
DEBUG=true
CMD=""
NS=""
POD=""
SELECTED=""
INPUT=""
usage() {
if [ -n "$1" ] ; then
echo $1;
fi
cat <<"EOF"
USAGE:
kubectl it exec|edit|delete|log|port-forward|scale <namespace>
EOF
}
it_select(){
echo "parameter $1 "
SELECTED="$( ${P1} | fzf -1 --ansi )"
}
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 )"
echo $RESOURCE1
eval "kubectl edit $RESOURCE_TYPE $RESOURCE1 -n $NS"
}
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;
eval "kubectl logs $POD -n $NS -c $CONTAINER "
}
logFollow(){
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;
eval "kubectl logs $POD -n $NS -c $CONTAINER -f "
}
delete(){
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 )"
echo $RESOURCE1
eval "kubectl delete $RESOURCE_TYPE $RESOURCE1 -n $NS"
}
exec(){
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;
eval "kubectl exec -ti $POD -n $NS -c $CONTAINER -- /bin/sh"
}
portForward(){
POD="$(kubectl get pods -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf -1 --ansi)"
echo $POD;
get_interactive_parameter " Pod Port ?"
POD_PORT=$INPUT
get_interactive_parameter " Your Port ?"
YOUR_PORT=$INPUT
eval "kubectl port-forward $POD -n $NS $YOUR_PORT:$POD_PORT "
}
scale(){
RESOURCE_TYPE="$( printf 'deployments\nstatefulsets\nreplicationcontrollers\ndaemonsets\nreplicasets' | fzf -1 --ansi)";
RESOURCE1="$( kubectl get $RESOURCE_TYPE -n $NS --output=jsonpath={.items..metadata.name} | tr -s '[[:space:]]' '\n' | fzf --ansi -1 )"
echo "Current Status:"
eval "kubectl get $RESOURCE_TYPE $RESOURCE1 -n $NS"
echo "Enter replica count:"
read SCALE
eval "kubectl scale $RESOURCE_TYPE $RESOURCE1 -n $NS --replicas=$SCALE"
echo "Status:"
eval "kubectl get $RESOURCE_TYPE $RESOURCE1 -n $NS"
}
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 "exec"|"edit"|"log"|"log-follow"|"delete"|"logs"|"port-forward"|"scale")
CMD=${@:$COUNTER:1} ;;
*)
PARAMETER="${@:$COUNTER:1}"
esac
let COUNTER=COUNTER+1
done
fi
if [[ $CMD == "" ]];then
CMD="$( printf 'exec\nedit\nlog\nlog-follow\ndelete\nport-forward\nscale' | fzf -1 --ansi)";
fi
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";
if [[ $CMD == "exec" ]];then
exec
elif [[ $CMD == "edit" ]];then
edit
elif [[ $CMD == "log" || $CMD == "logs" ]];then
log
elif [[ $CMD == "log-follow" ]];then
logFollow
elif [[ $CMD == "delete" ]];then
delete
elif [[ $CMD == "scale" ]];then
scale
elif [[ $CMD == "port-forward" ]];then
portForward
elif [[ $CMD == "help" || $CMD == "-help" || $CMD == "-h" ]];then
usage
fi
}
main() {
read_parameters "$@";
}
main "$@"