-
Notifications
You must be signed in to change notification settings - Fork 1
/
oc
executable file
·431 lines (369 loc) · 12 KB
/
oc
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/bash
#set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
# Handle source locations that might be a symlink (ref: http://bit.ly/2kcvSCS)
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
DOC="https://github.com/openshift-evangelists/oc-wrapper/blob/master/README.md"
SCRIPT_NAME=$(basename "$0")
OC_BINARY=${OC_BINARY:-"$DIR/oc-origin"}
OC_CONTEXT=${OC_CONTEXT:-""}
TIMEOUT=${OC_LOGIN_TIMEOUT:-"1s"}
OC_DEBUG_ENABLED=${OC_DEBUG_ENABLED:-"0"} # Use log levels: 0 NONE, 1 ERROR, 2 WARN, 3 INFO, 4 DEBUG, 5 TRACE
[ ! -x ${OC_BINARY} ] && echo "The real oc binary \"${OC_BINARY}\" does not exist. Make sure it's renamed to something different than oc" && exit 1
[[ "" != "${OC_CONTEXT}" ]] && OC_BINARY="${OC_BINARY} --config=${OC_CONTEXT}"
################## AUXILIARY FUNCTIONS
#
#
function currentContext {
local _ctx=$(${OC_BINARY} config current-context 2> /dev/null)
# TODO: See if oc whoami -c is better
echo "$_ctx"
}
function currentUser {
local _ctx=${1:-}
[ "${_ctx}" == "" ] && _ctx=$(currentContext)
if [ "${_ctx}" == "" ]
then
echo ""
else
local _path="{.contexts[?(@.name == \"$_ctx\")].context.user}"
local _user="$(${OC_BINARY} config view -o jsonpath="$_path" 2> /dev/null)"
echo "$_user"
fi
}
function currentCluster {
local _ctx=${1:-}
[ "${_ctx}" == "" ] && _ctx=$(currentContext)
if [ "${_ctx}" == "" ]
then
echo ""
else
local _path="{.contexts[?(@.name == \"$_ctx\")].context.cluster}"
local _cluster="$(${OC_BINARY} config view -o jsonpath="$_path" 2> /dev/null)"
echo "$_cluster"
fi
}
function _exec_oc {
logInfo "Sending command to real oc"
logDebug "Before"
logDebug "exec ${OC_BINARY} $@"
exec ${OC_BINARY} "$@"
logDebug "After"
}
function _oc_newKubecfg {
logInfo "Sending command to real oc"
logDebug "Before"
logDebug "KUBECONFIG=~/.kube/config.trash ${OC_BINARY} $@"
cp -f ~/.kube/config ~/.kube/config.trash
KUBECONFIG=~/.kube/config.trash ${OC_BINARY} "$@"
local retCode=$?
[ debugEnabled ] && rm -f ~/.kube/config.trash
logDebug "After"
return $retCode
}
function debugEnabled() {
[ "$OC_DEBUG_ENABLED" == "" ] && return 0
}
function logDebug(){
[ "$OC_DEBUG_ENABLED" -ge 4 ] && echo "[DEBUG] $@"
}
function logInfo(){
[ "$OC_DEBUG_ENABLED" -ge 3 ] && echo "[INFO] $@"
}
################## MAIN SCRIPT
#
#
function help {
echo "OpenShift Wrapper"
echo ""
echo "This wrapper helps work with contexts in a meaningful way"
echo ""
echo "Basic Commands:"
echo ""
echo " login"
echo " logout"
echo " context-status"
echo " list"
echo " use <CONTEXT>"
echo " rename-context <NEW-CONTEXT-NAME>"
echo " delete-context <CONTEXT>"
echo
${OC_BINARY} -h
echo ""
echo ""
echo "See the documentation at $DOC"
}
function list {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
for context in `${OC_BINARY} config view -o jsonpath='{range .contexts[*]}{.name}{" "}{end}'`
do
if [ "${context}" == "${_ctx}" ]
then
echo " - ${context} ***********************"
else
echo " - ${context}"
fi
done
}
function list.help {
echo "Lists existing contexts. It shows current context with a line of *"
echo ""
echo "See the documentation at $DOC"
}
function login {
local _arg1=${1:-}
local _retCode
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
# If there's no current context, delegate to real login
local _ctx=$(currentContext)
if [ "" == "${_ctx}" ]
then
${OC_BINARY} login "$@"
else
# TODO: If we're connecting to a net new cluster there will be parameters
if [ "${_arg1}" != "" ]
then
${OC_BINARY} login "$@"
else
${OC_BINARY} whoami --request-timeout=$TIMEOUT > /dev/null 2>&1
_retCode=$?
if [ $_retCode -ne 0 ]; then
# Check to see if the server is up
${OC_BINARY} whoami --request-timeout=$TIMEOUT /dev/null 2> /tmp/oc-whoami-err.temp
grep "Client.Timeout" /tmp/oc-whoami-err.temp > /dev/null 2>&1
_retCode=$?
rm -f /tmp/oc-whoami-err.temp
if [ $_retCode -ne 0 ]; then
local _ctx=$(${OC_BINARY} whoami -c)
local _cluster=$(${OC_BINARY} whoami --show-server)
local _user=$(${OC_BINARY} config view -o jsonpath='{.contexts[?(@.name == "'${_ctx}'")].context.user}' | cut -f 1 -d '/')
echo "oc login ${_cluster} -u ${_user} --request-timeout=2s"
${OC_BINARY} login ${_cluster} -u ${_user} --request-timeout=$TIMEOUT
_retCode=$?
if [ $_retCode -eq 0 ]; then
# TODO: Login might change the username/cluster if it has been customized.
${OC_BINARY} config delete-context ${_ctx} > /dev/null 2>&1
${OC_BINARY} config rename-context $(${OC_BINARY} whoami -c) ${_ctx} > /dev/null 2>&1
echo "User has succesfully logged back to ${_ctx}"
else
echo "There's been an error trying to log the user back in"
fi
else
echo "There's been a Timeout error trying to log the user back in. Is the server up and reachable?"
fi
else
echo "User is already logged in and valid. Context is $(${OC_BINARY} whoami -c)"
fi
fi
fi
}
function login.help {
echo "Validates that the user is logged in with the current context, and if not, it logs it in"
echo ""
echo "See the documentation at $DOC"
}
function use {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
[ "${_ctx}" == "${_arg1}" ] && return
${OC_BINARY} config use-context ${_arg1}
}
function use.help {
echo "Changes to the specified context"
echo ""
echo "Usage:"
echo " $SCRIPT_NAME use <CONTEXT>"
echo ""
echo "See the documentation at $DOC"
}
function project {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
[ "" == "${_arg1}" ] && _exec_oc project -h && return
[ "-q" == "${_arg1}" ] && _exec_oc project -q && return
[[ ${_arg1} == --short* ]] && _exec_oc project $_arg1 && return
${OC_BINARY} config set-context ${_ctx} --namespace=${_arg1} > /dev/null 2>&1
_cluster_name=$(${OC_BINARY} config view -o jsonpath='{.contexts[?(@.name == "'${_ctx}'")].context.cluster}')
_cluster=$(${OC_BINARY} config view -o jsonpath='{.clusters[?(@.name == "'${_cluster_name}'")].cluster.server}')
echo "Now using project \"${_arg1}\" on server \"$_cluster\"."
}
function project.help {
${OC_BINARY} project -h
}
function new-project {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
[ "" == "${_arg1}" ] && _exec_oc new-project -h && return
_oc_newKubecfg new-project "$@"
# Only change current context if previous action succeeds.
if [ "$?" -eq "0" ]
then
${OC_BINARY} config set-context ${_ctx} --namespace=${_arg1} > /dev/null 2>&1
fi
}
function new-project.help {
${OC_BINARY} new-project -h
}
function delete-project {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
[ "" == "${_arg1}" ] && _exec_oc delete project -h && return
${OC_BINARY} delete project "$@"
# Only change current context if previous action succeeds.
if [ "$?" -eq "0" ]
then
${OC_BINARY} config set-context ${_ctx} --namespace= > /dev/null 2>&1
fi
}
function delete-project.help {
${OC_BINARY} delete project -h
}
function rename-context {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
[ "" == "${_arg1}" ] && echo "You need to specify a new context name" && return
[ "${_ctx}" == "${_arg1}" ] && return
${OC_BINARY} config rename-context ${_ctx} ${_arg1}
}
function rename-context.help {
echo "Renames the current context"
echo ""
echo "Usage:"
echo " $SCRIPT_NAME rename-context <NEW-CONTEXT-NAME>"
echo ""
echo "See the documentation at $DOC"
}
function delete-context {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
[ "" == "${_arg1}" ] && echo "You need to specify a context name" && return
# echo "${OC_BINARY} config view -o jsonpath='{.contexts[?(@.name == \"$_arg1\")].context.user}'"
local __user=$(currentUser $_arg1)
local __cluster=$(currentCluster $_arg1)
${OC_BINARY} config unset users.${__user} &> /dev/null
${OC_BINARY} config unset clusters.${__cluster} &> /dev/null
${OC_BINARY} config delete-context ${_arg1} &> /dev/null
if [ "$(currentContext)" == "${_arg1}" ]
then
${OC_BINARY} config unset current-context &> /dev/null
echo "Current OC context has been unset. Please use \"oc list\" to list available context, and \"oc use <context-name>\" to set one"
fi
}
function delete-context.help {
echo "Deletes the specified context"
echo ""
echo "Usage:"
echo " $SCRIPT_NAME delete-context <CONTEXT-NAME>"
echo ""
echo "See the documentation at $DOC"
}
function context-status {
local _arg1=${1:-}
[ "$_arg1" == "-h" ] || [ "$_arg1" == "--help" ] && ${FUNCNAME[0]}.help && return 0
local _ctx=$(currentContext)
[ "$_ctx" == "" ] && echo "There's no context selected. Use 'oc use <context-name>' or 'oc list'" && return 0
# Two cases, if named context
local _project=""
local _user=""
local _cluster=""
_project=$(${OC_BINARY} config view -o jsonpath='{.contexts[?(@.name == "'${_ctx}'")].context.namespace}' 2> /dev/null)
_cluster_name=$(${OC_BINARY} config view -o jsonpath='{.contexts[?(@.name == "'${_ctx}'")].context.cluster}' 2> /dev/null)
_cluster=$(${OC_BINARY} config view -o jsonpath='{.clusters[?(@.name == "'${_cluster_name}'")].cluster.server}' 2> /dev/null)
_user=$(${OC_BINARY} config view -o jsonpath='{.contexts[?(@.name == "'${_ctx}'")].context.user}' | cut -f 1 -d '/' 2> /dev/null)
${OC_BINARY} get user/~ -o name --token "$(${OC_BINARY} whoami -t)" > /dev/null 2>&1
_connected=$?
echo "You're using profile: ${_ctx}"
[ $_connected -eq 0 ] && echo "[INFO] You're currently connected. Token is still valid" || echo "[ERROR] You're currently disconnected. Token is no longer valid"
echo " "
echo "Cluster: ${_cluster}"
echo "User: ${_user}"
echo "Project: ${_project}"
}
function context-status.help {
echo "Status "
echo ""
echo "Available Arguments:"
echo ""
echo ""
echo "See the documentation at $DOC"
}
function logout {
sed -i -e 's/current-context:.*/current-context:/g' ~/.kube/config
}
function logout.help {
echo "Logs the user out"
echo ""
echo "See the documentation at $DOC"
}
if [[ $# -gt 0 ]]
then
key="$1"
case $key in
login)
shift # past argument
login "$@"
;;
logout)
logout
;;
list)
shift # past argument
list "$@"
;;
use)
shift # past argument
use "$@"
;;
project)
shift # past argument
project "$@"
;;
new-project)
shift # past argument
new-project "$@"
;;
rename-context)
shift # past argument
rename-context "$@"
;;
delete-context)
shift # past argument
delete-context "$@"
;;
delete)
if [ "$2" == "project" ]
then
shift 2
delete-project "$@"
else
_exec_oc "$@"
fi
;;
context-status)
context-status
;;
-h|--help)
help
;;
*)
_exec_oc "$@"
;;
esac
else
help
fi