6
6
# Current directory must be the root directory of the repo
7
7
8
8
function trap_add() {
9
- # based on https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
10
- # but: prepends new cmd rather than append it, changed var names and eliminated messages
11
-
12
- local cmd_to_add signal
13
-
14
- cmd_to_add=$1 ; shift
15
- for signal in " $@ " ; do
16
- trap -- " $(
17
- # print the new trap command
18
- printf ' %s\n' " ${cmd_to_add} "
19
- # helper fn to get existing trap command from output
20
- # of trap -p
21
- # shellcheck disable=SC2329,SC2317
22
- extract_trap_cmd () { printf ' %s\n' " $3 " ; }
23
- # print existing trap command with newline
24
- eval " extract_trap_cmd $( trap -p " ${signal} " ) "
25
- ) " " ${signal} "
26
- done
9
+ # based on https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
10
+ # but: prepends new cmd rather than append it, changed var names and eliminated messages
11
+
12
+ local cmd_to_add signal
13
+
14
+ cmd_to_add=$1
15
+ shift
16
+ for signal in " $@ " ; do
17
+ trap -- " $(
18
+ # print the new trap command
19
+ printf ' %s\n' " ${cmd_to_add} "
20
+ # helper fn to get existing trap command from output
21
+ # of trap -p
22
+ # shellcheck disable=SC2329,SC2317
23
+ extract_trap_cmd () { printf ' %s\n' " $3 " ; }
24
+ # print existing trap command with newline
25
+ eval " extract_trap_cmd $( trap -p " ${signal} " ) "
26
+ ) " " ${signal} "
27
+ done
27
28
}
28
29
29
30
function errexit_msg {
@@ -72,7 +73,10 @@ if [ "$SAS_COMMON_SOURCED" = "" ]; then
72
73
export USER_DIR=${USER_DIR:- $(pwd)}
73
74
if [ -d " $USER_DIR " ]; then
74
75
# Resolve full path
75
- USER_DIR=$( cd " $( dirname " $USER_DIR " ) " || exit ; pwd) /$( basename " $USER_DIR " )
76
+ USER_DIR=$(
77
+ cd " $( dirname " $USER_DIR " ) " || exit
78
+ pwd
79
+ ) /$( basename " $USER_DIR " )
76
80
export USER_DIR
77
81
fi
78
82
if [ -f " $USER_DIR /user.env" ]; then
@@ -122,7 +126,6 @@ if [ "$SAS_COMMON_SOURCED" = "" ]; then
122
126
# set TLS Cert Generator (cert-manager|openssl)
123
127
export CERT_GENERATOR=" ${CERT_GENERATOR:- openssl} "
124
128
125
-
126
129
# Set default timeout for kubectl namespace delete command
127
130
export KUBE_NAMESPACE_DELETE_TIMEOUT=${KUBE_NAMESPACE_DELETE_TIMEOUT:- 5m}
128
131
154
157
155
158
function checkDefaultStorageClass {
156
159
if [ -z " $defaultStorageClass " ]; then
157
- # Check for kubernetes environment conflicts/requirements
160
+ # Check for kubernetes environment conflicts/requirements
158
161
defaultStorageClass=$( kubectl get storageclass -o jsonpath=" {range .items[*]}{.metadata.name}{'\t'}{.metadata.annotations..storageclass\.kubernetes\.io/is-default-class}{'\n'}{end}" | grep true | awk ' {print $1}' )
159
162
if [ " $defaultStorageClass " ]; then
160
163
log_debug " Found default storageClass: [$defaultStorageClass ]"
@@ -198,21 +201,20 @@ function validateTenantID {
198
201
fi
199
202
}
200
203
201
-
202
204
function validateNamespace {
203
205
local namespace
204
206
namespace=" $1 "
205
- if [[ " $namespace " =~ ^[a-z0-9]([\- a-z0-9]* [a-z0-9])? $ ]]; then
207
+ if [[ $namespace =~ ^[a-z0-9]([\- a-z0-9]* [a-z0-9])? $ ]]; then
206
208
log_debug " Namespace [$namespace ] passes validation"
207
209
else
208
210
log_error " [$namespace ] is not a valid namespace name"
209
211
exit 1
210
212
fi
211
213
}
212
214
213
-
214
215
function randomPassword {
215
- date +%s | sha256sum | base64 | head -c 32 ; echo
216
+ date +%s | sha256sum | base64 | head -c 32
217
+ echo
216
218
}
217
219
218
220
function disable_sa_token_automount {
@@ -222,7 +224,7 @@ function disable_sa_token_automount {
222
224
should_disable=${SEC_DISABLE_SA_TOKEN_AUTOMOUNT:- true}
223
225
224
226
if [ " $should_disable " == " true" ]; then
225
- if [ -n " $( kubectl -n " $ns " get serviceAccount " $sa_name " -o name 2> /dev/null) " ]; then
227
+ if [ -n " $( kubectl -n " $ns " get serviceAccount " $sa_name " -o name 2> /dev/null) " ]; then
226
228
log_debug " Disabling automount of API tokens for serviceAccount [$ns /$sa_name ]"
227
229
kubectl -n " $ns " patch serviceAccount " $sa_name " -p ' {"automountServiceAccountToken":false}'
228
230
else
@@ -244,7 +246,7 @@ function enable_pod_token_automount {
244
246
log_debug " Enabling automount of API tokens for pods deployed via [$resource_type /$resource_name ]"
245
247
246
248
if [ " $resource_type " == " daemonset" ] || [ " $resource_type " == " deployment" ]; then
247
- kubectl -n " $ns " patch " $resource_type " " $resource_name " -p ' {"spec": {"template": {"spec": {"automountServiceAccountToken":true}}}}'
249
+ kubectl -n " $ns " patch " $resource_type " " $resource_name " -p ' {"spec": {"template": {"spec": {"automountServiceAccountToken":true}}}}'
248
250
else
249
251
log_error " Invalid request to function [${FUNCNAME[0]} ]; unsupported resource_type [$resource_type ]"
250
252
return 1
@@ -264,31 +266,30 @@ export -f disable_sa_token_automount
264
266
export -f enable_pod_token_automount
265
267
266
268
function parseFullImage {
267
- # shellcheck disable=SC2034
268
- fullImage=" $1 "
269
- unset REGISTRY REPOS IMAGE VERSION FULL_IMAGE_ESCAPED
269
+ # shellcheck disable=SC2034
270
+ fullImage=" $1 "
271
+ unset REGISTRY REPOS IMAGE VERSION FULL_IMAGE_ESCAPED
270
272
271
- if [[ " $1 " =~ (.* )\/ (.* )\/ (.* )\: (.* ) ]]; then
273
+ if [[ $1 =~ (.* )\/ (.* )\/ (.* )\: (.* ) ]]; then
272
274
REGISTRY=" ${BASH_REMATCH[1]} "
273
275
REPOS=" ${BASH_REMATCH[2]} "
274
276
IMAGE=" ${BASH_REMATCH[3]} "
275
277
VERSION=" ${BASH_REMATCH[4]} "
276
278
# shellcheck disable=SC2034
277
279
FULL_IMAGE_ESCAPED=" $REGISTRY \/$REPOS \/$IMAGE \:$VERSION "
278
280
return 0
279
- else
281
+ else
280
282
log_warn " Invalid value for full container image; does not fit expected pattern [$1 ]."
281
283
return 1
282
- fi
284
+ fi
283
285
}
284
286
285
-
286
287
function v4m_replace {
287
288
288
289
if echo " $OSTYPE " | grep ' darwin' > /dev/null 2>&1 ; then
289
- sed -i ' ' " s;$1 ;$2 ;g" " $3 "
290
+ sed -i ' ' " s;$1 ;$2 ;g" " $3 "
290
291
else
291
- sed -i " s;$1 ;$2 ;g" " $3 "
292
+ sed -i " s;$1 ;$2 ;g" " $3 "
292
293
fi
293
294
}
294
295
@@ -304,7 +305,7 @@ function generateImageKeysFile {
304
305
305
306
local pullsecret_text
306
307
307
- if ! parseFullImage " $1 " ; then
308
+ if ! parseFullImage " $1 " ; then
308
309
log_error " Unable to parse full image [$1 ]"
309
310
return 1
310
311
fi
@@ -316,8 +317,8 @@ function generateImageKeysFile {
316
317
template_file=$2
317
318
318
319
if [ " $template_file " != " $imageKeysFile " ]; then
319
- rm -f " $imageKeysFile "
320
- cp " $template_file " " $imageKeysFile "
320
+ rm -f " $imageKeysFile "
321
+ cp " $template_file " " $imageKeysFile "
321
322
else
322
323
log_debug " Modifying an existing imageKeysFile"
323
324
fi
@@ -349,20 +350,19 @@ function generateImageKeysFile {
349
350
v4m_pullPolicy=${V4M_PULL_POLICY:- " IfNotPresent" }
350
351
351
352
v4m_replace " __${prefix} GLOBAL_REGISTRY_OSBUG__" " $GLOBAL_REGISTRY_OSBUG " " $imageKeysFile "
352
- v4m_replace " __${prefix} GLOBAL_REGISTRY__" " $GLOBAL_REGISTRY " " $imageKeysFile "
353
+ v4m_replace " __${prefix} GLOBAL_REGISTRY__" " $GLOBAL_REGISTRY " " $imageKeysFile "
353
354
v4m_replace " __${prefix} IMAGE_REGISTRY__" " $REGISTRY " " $imageKeysFile "
354
- v4m_replace " __${prefix} IMAGE_REPO_3LEVEL__" " $REGISTRY \/$REPOS \/$IMAGE " " $imageKeysFile "
355
- v4m_replace " __${prefix} IMAGE_REPO_2LEVEL__" " $REPOS \/$IMAGE " " $imageKeysFile "
356
- v4m_replace " __${prefix} IMAGE__" " $IMAGE " " $imageKeysFile "
357
- v4m_replace " __${prefix} IMAGE_TAG__" " $VERSION " " $imageKeysFile "
358
- v4m_replace " __${prefix} IMAGE_PULL_POLICY__" " $v4m_pullPolicy " " $imageKeysFile "
359
- v4m_replace " __${prefix} IMAGE_PULL_SECRET__" " $pullsecret_text " " $imageKeysFile " # Handle Charts Accepting a Single Image Pull Secret
360
- v4m_replace " __${prefix} IMAGE_PULL_SECRETS__" " $pullsecrets_text " " $imageKeysFile " # Handle Charts Accepting Multiple Image Pull Secrets
355
+ v4m_replace " __${prefix} IMAGE_REPO_3LEVEL__" " $REGISTRY \/$REPOS \/$IMAGE " " $imageKeysFile "
356
+ v4m_replace " __${prefix} IMAGE_REPO_2LEVEL__" " $REPOS \/$IMAGE " " $imageKeysFile "
357
+ v4m_replace " __${prefix} IMAGE__" " $IMAGE " " $imageKeysFile "
358
+ v4m_replace " __${prefix} IMAGE_TAG__" " $VERSION " " $imageKeysFile "
359
+ v4m_replace " __${prefix} IMAGE_PULL_POLICY__" " $v4m_pullPolicy " " $imageKeysFile "
360
+ v4m_replace " __${prefix} IMAGE_PULL_SECRET__" " $pullsecret_text " " $imageKeysFile " # Handle Charts Accepting a Single Image Pull Secret
361
+ v4m_replace " __${prefix} IMAGE_PULL_SECRETS__" " $pullsecrets_text " " $imageKeysFile " # Handle Charts Accepting Multiple Image Pull Secrets
361
362
362
363
return 0
363
364
}
364
365
365
-
366
366
export -f parseFullImage
367
367
export -f v4m_replace
368
368
export -f generateImageKeysFile
0 commit comments