-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgk8s
executable file
·198 lines (176 loc) · 4.33 KB
/
gk8s
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
#!/usr/bin/env bash
cat <<'EOF'
This tool is deprecated.
Please use the Golang version of this script instead:
https://github.com/icy/gk8s
EOF
exit 0
# Date : 2019-09-06
# Author : Ky-Anh Huynh
# Purpose : wrapper for all k8s commands/clusters
# Source : https://github.com/icy/bashy/blob/master/bin/gk8s
# TODOs :
#
# - [ ] Should migrate to enviroment use instead?
#
# Ideas
#
# 1. Hook up $HOME variable
# 2. Explicitly avoid kind of human mistakes when dealing with
# multiple clusters and namespaces
# 3. To use with/by non-human scripts ;)
#
# Usage
#
# 1. Initialize clusters' root
#
# $ mkdir ~/projects/gk8s/<cluster-name>
# $ cd ~/projects/gk8s/<cluster-name>
# $ touch .gk8s
#
# 2. Provision aws configurations
#
# $ ln -s ~/.aws .aws
#
# If you don't do this, the script tries to create symlink automatically.
#
# 3. Provision k8s configurations
#
# $ ln -s ~/.kube .kube
#
# 5. Invoke the command
#
# $ gk8s get pods # on the default namespace,
# # on namespace specified in `.gk8s`
# $ gk8s get pods @% # on all namespaces
#
# You can move around and execute your alias command instead
#
# $ cd /my/parent/home
# $ gk8s :cluster-name get pods
#
# `kubectl` is injected when you are not specifed them as your first
# argument. E.g, `gk8s get pods` is the same as `gk8s kubectl get pods`.
#
# Other usage
#
# 1. Move around the root: use `GK8S_HOME` instead of ~/projects/gk8s/.
# You can specify this in `~/.config/gk8s`.
# 2. To allow debugging, use `VERBOSE=2 gk8s <...>`
#
_ver="${BASH_VERSINFO[0]}"
if [[ "$_ver" -lt 4 ]]; then
echo >&2 ":: $0: Sorry, Bash >=4 is required."
exit 1
fi
_log2() {
[[ "${VERBOSE:-0}" -ge 2 ]] || return 0
echo >&2 ":: ${*}"
}
_help() {
echo >&2 ":: Please specify a command."
}
GK8S_HOME="${GK8S_HOME:-$HOME/projects/gk8s}";
export GK8S_HOME; readonly GK8S_HOME
# Try to learn if the first argument is about the cluster name
first_arg="${1:-}"
if [[ "${first_arg:0:1}" == ":" ]]; then
shift
_CLUSTER="${first_arg:1}"
if [[ -z "$_CLUSTER" ]]; then
# FIXME: remove duplicate code...
_CLUSTER="$(pwd)"
NHOME="$(pwd -P)"
_CLUSTER="${_CLUSTER##*/}"
else
( cd "$GK8S_HOME/$_CLUSTER/" || exit ; )
NHOME="$GK8S_HOME/$_CLUSTER/"
fi
else
_CLUSTER="$(pwd)"
NHOME="$(pwd -P)"
_CLUSTER="${_CLUSTER##*/}"
fi
export NHOME; readonly NHOME
if [[ ! -f "$NHOME/.gk8s" ]]; then
echo >&2 "... Missing .gk8s file in working directory '$NHOME'."
if [[ -f "$NHOME/.k8s" ]]; then
echo >&2 ".... found .k8s file. Rename this file to .gk8s would help."
fi
exit 1
fi
if [[ ! -f "$NHOME/.kube/config" ]]; then
echo >&2 ":: kubectl config file not found: $NHOME/.kube/config"
exit 1
fi
if [[ "$#" -eq 0 ]]; then
set -- _help
fi
_ns="--namespace ${GK8S_NAMESPACE:-default}"
case "${1:-}" in
"kubectl"*|"helm"*|_help)
;;
"--")
shift;
;;
*)
set -- kubectl "$@"
;;
esac
# Insert the namespace configuration...
_cmd1="$1";
case "$_cmd1" in
kubectl|kubectl.*)
_log2 "Original command: '$@'"
shift;
_cmd2="$1"; shift;
case "$_cmd2" in
"apply"|"diff"|"delete")
set -- "$_cmd1" "$_cmd2" "$@"
;;
*)
set -- "$_cmd1" "$_cmd2" $_ns "$@"
_log2 "Modified command: '$@'"
;;
esac
;;
esac
export OHOME="$HOME"; readonly OHOME
export HOME="$NHOME"
_oargs=( "$@" )
_start=0
while [[ "$_start" -le "${#_oargs}" ]]; do
case "${_oargs[$_start]}" in
"@%")
_oargs["$_start"]="--all-namespaces"
;;
esac
(( _start ++ ))
done
set -- "${_oargs[@]}"
# Print heading...
_log2 ":: Cluster : $_CLUSTER, input namespace: $_ns"
_log2 ":: WDir : $(pwd)"
_log2 ":: Command : $@"
_log2 ":: OHOME : $OHOME"
_log2 ":: NHOME : $HOME"
_log2 ":: GK8S_HOME : $GK8S_HOME"
if [[ -f "$NHOME/.gk8s.lock" ]]; then
echo >&2 ":: Lock file found $NHOME/.gk8s.lock. Action can not be processed."
exit 1
fi
if grep -qsEe "[[:space:]]delete[[:space:]]" <<< $* ; then
if [[ ! -f ".delete" ]]; then
echo >&2 ":: Action delete was requested but confirmation file (.delete) not found"
echo >&2 ":: Working directory: $(pwd)"
exit 127
fi
_log2 ":: Removing confirmation file: $(pwd)/.delete"
1>&2 rm -fv .delete
fi
[[ "${VERBOSE:-0}" -ge 2 ]] && set -x
# Create a symlink to ~/.aws if necessary
if [[ ! -d "$NHOME/.aws" && -d "$OHOME/.aws" ]]; then
1>&2 ln -sv "$OHOME/.aws" "$NHOME/.aws"
fi
"${@}"