66# Current directory must be the root directory of the repo
77
88function 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
2728}
2829
2930function errexit_msg {
@@ -72,7 +73,10 @@ if [ "$SAS_COMMON_SOURCED" = "" ]; then
7273 export USER_DIR=${USER_DIR:- $(pwd)}
7374 if [ -d " $USER_DIR " ]; then
7475 # 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 " )
7680 export USER_DIR
7781 fi
7882 if [ -f " $USER_DIR /user.env" ]; then
@@ -122,7 +126,6 @@ if [ "$SAS_COMMON_SOURCED" = "" ]; then
122126 # set TLS Cert Generator (cert-manager|openssl)
123127 export CERT_GENERATOR=" ${CERT_GENERATOR:- openssl} "
124128
125-
126129 # Set default timeout for kubectl namespace delete command
127130 export KUBE_NAMESPACE_DELETE_TIMEOUT=${KUBE_NAMESPACE_DELETE_TIMEOUT:- 5m}
128131
154157
155158function checkDefaultStorageClass {
156159 if [ -z " $defaultStorageClass " ]; then
157- # Check for kubernetes environment conflicts/requirements
160+ # Check for kubernetes environment conflicts/requirements
158161 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}' )
159162 if [ " $defaultStorageClass " ]; then
160163 log_debug " Found default storageClass: [$defaultStorageClass ]"
@@ -198,21 +201,20 @@ function validateTenantID {
198201 fi
199202}
200203
201-
202204function validateNamespace {
203205 local namespace
204206 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
206208 log_debug " Namespace [$namespace ] passes validation"
207209 else
208210 log_error " [$namespace ] is not a valid namespace name"
209211 exit 1
210212 fi
211213}
212214
213-
214215function randomPassword {
215- date +%s | sha256sum | base64 | head -c 32 ; echo
216+ date +%s | sha256sum | base64 | head -c 32
217+ echo
216218}
217219
218220function disable_sa_token_automount {
@@ -222,7 +224,7 @@ function disable_sa_token_automount {
222224 should_disable=${SEC_DISABLE_SA_TOKEN_AUTOMOUNT:- true}
223225
224226 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
226228 log_debug " Disabling automount of API tokens for serviceAccount [$ns /$sa_name ]"
227229 kubectl -n " $ns " patch serviceAccount " $sa_name " -p ' {"automountServiceAccountToken":false}'
228230 else
@@ -244,7 +246,7 @@ function enable_pod_token_automount {
244246 log_debug " Enabling automount of API tokens for pods deployed via [$resource_type /$resource_name ]"
245247
246248 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}}}}'
248250 else
249251 log_error " Invalid request to function [${FUNCNAME[0]} ]; unsupported resource_type [$resource_type ]"
250252 return 1
@@ -264,31 +266,30 @@ export -f disable_sa_token_automount
264266export -f enable_pod_token_automount
265267
266268function 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
270272
271- if [[ " $1 " =~ (.* )\/ (.* )\/ (.* )\: (.* ) ]]; then
273+ if [[ $1 =~ (.* )\/ (.* )\/ (.* )\: (.* ) ]]; then
272274 REGISTRY=" ${BASH_REMATCH[1]} "
273275 REPOS=" ${BASH_REMATCH[2]} "
274276 IMAGE=" ${BASH_REMATCH[3]} "
275277 VERSION=" ${BASH_REMATCH[4]} "
276278 # shellcheck disable=SC2034
277279 FULL_IMAGE_ESCAPED=" $REGISTRY \/$REPOS \/$IMAGE \:$VERSION "
278280 return 0
279- else
281+ else
280282 log_warn " Invalid value for full container image; does not fit expected pattern [$1 ]."
281283 return 1
282- fi
284+ fi
283285}
284286
285-
286287function v4m_replace {
287288
288289 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 "
290291 else
291- sed -i " s;$1 ;$2 ;g" " $3 "
292+ sed -i " s;$1 ;$2 ;g" " $3 "
292293 fi
293294}
294295
@@ -304,7 +305,7 @@ function generateImageKeysFile {
304305
305306 local pullsecret_text
306307
307- if ! parseFullImage " $1 " ; then
308+ if ! parseFullImage " $1 " ; then
308309 log_error " Unable to parse full image [$1 ]"
309310 return 1
310311 fi
@@ -316,8 +317,8 @@ function generateImageKeysFile {
316317 template_file=$2
317318
318319 if [ " $template_file " != " $imageKeysFile " ]; then
319- rm -f " $imageKeysFile "
320- cp " $template_file " " $imageKeysFile "
320+ rm -f " $imageKeysFile "
321+ cp " $template_file " " $imageKeysFile "
321322 else
322323 log_debug " Modifying an existing imageKeysFile"
323324 fi
@@ -349,20 +350,19 @@ function generateImageKeysFile {
349350 v4m_pullPolicy=${V4M_PULL_POLICY:- " IfNotPresent" }
350351
351352 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 "
353354 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
361362
362363 return 0
363364}
364365
365-
366366export -f parseFullImage
367367export -f v4m_replace
368368export -f generateImageKeysFile
0 commit comments