@@ -20,7 +20,7 @@ set -o errexit
20
20
set -o nounset
21
21
set -o pipefail
22
22
23
- KUBE_ROOT=$( cd $( dirname " ${BASH_SOURCE[0]} " ) /.. && pwd)
23
+ KUBE_ROOT=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " /.. && pwd)
24
24
25
25
DEFAULT_KUBECONFIG=" ${HOME:- .} /.kube/config"
26
26
@@ -29,15 +29,21 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
29
29
#
30
30
# NOTE This must match the version_regex in build/common.sh
31
31
# kube::release::parse_and_validate_release_version()
32
- KUBE_RELEASE_VERSION_REGEX=" ^v(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)(-([a-zA-Z0-9]+)\\ .(0|[1-9][0-9]*))?$"
33
- KUBE_RELEASE_VERSION_DASHED_REGEX=" v(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)(-([a-zA-Z0-9]+)-(0|[1-9][0-9]*))?"
32
+ #
33
+ # KUBE_RELEASE_VERSION_REGEX is used in hack/get-build.sh and cluster/gce/util.sh and KUBE_RELEASE_VERSION_DASHED_REGEX is used in cluster/gce/util.sh,
34
+ # make sure to remove these vars when not used anymore
35
+ export KUBE_RELEASE_VERSION_REGEX=" ^v(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)(-([a-zA-Z0-9]+)\\ .(0|[1-9][0-9]*))?$"
36
+ export KUBE_RELEASE_VERSION_DASHED_REGEX=" v(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)(-([a-zA-Z0-9]+)-(0|[1-9][0-9]*))?"
34
37
35
38
# KUBE_CI_VERSION_REGEX matches things like "v1.2.3-alpha.4.56+abcdefg" This
36
39
#
37
40
# NOTE This must match the version_regex in build/common.sh
38
41
# kube::release::parse_and_validate_ci_version()
39
- KUBE_CI_VERSION_REGEX=" ^v(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)-([a-zA-Z0-9]+)\\ .(0|[1-9][0-9]*)(\\ .(0|[1-9][0-9]*)\\ +[-0-9a-z]*)?$"
40
- KUBE_CI_VERSION_DASHED_REGEX=" ^v(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-([a-zA-Z0-9]+)-(0|[1-9][0-9]*)(-(0|[1-9][0-9]*)\\ +[-0-9a-z]*)?"
42
+ #
43
+ # TODO: KUBE_CI_VERSION_REGEX is used in hack/get-build.sh and KUBE_CI_VERSION_DASHED_REGEX is used in cluster/gce/util.sh,
44
+ # make sure to remove these vars when not used anymore
45
+ export KUBE_CI_VERSION_REGEX=" ^v(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)\\ .(0|[1-9][0-9]*)-([a-zA-Z0-9]+)\\ .(0|[1-9][0-9]*)(\\ .(0|[1-9][0-9]*)\\ +[-0-9a-z]*)?$"
46
+ export KUBE_CI_VERSION_DASHED_REGEX=" ^v(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-([a-zA-Z0-9]+)-(0|[1-9][0-9]*)(-(0|[1-9][0-9]*)\\ +[-0-9a-z]*)?"
41
47
42
48
# Generate kubeconfig data for the created cluster.
43
49
# Assumed vars:
@@ -93,17 +99,17 @@ function create-kubeconfig() {
93
99
fi
94
100
95
101
local user_args=()
96
- if [[ ! -z " ${KUBE_BEARER_TOKEN:- } " ]]; then
102
+ if [[ -n " ${KUBE_BEARER_TOKEN:- } " ]]; then
97
103
user_args+=(
98
104
" --token=${KUBE_BEARER_TOKEN} "
99
105
)
100
- elif [[ ! -z " ${KUBE_USER:- } " && ! -z " ${KUBE_PASSWORD:- } " ]]; then
106
+ elif [[ -n " ${KUBE_USER:- } " && -n " ${KUBE_PASSWORD:- } " ]]; then
101
107
user_args+=(
102
108
" --username=${KUBE_USER} "
103
109
" --password=${KUBE_PASSWORD} "
104
110
)
105
111
fi
106
- if [[ ! -z " ${KUBE_CERT:- } " && ! -z " ${KUBE_KEY:- } " ]]; then
112
+ if [[ -n " ${KUBE_CERT:- } " && -n " ${KUBE_KEY:- } " ]]; then
107
113
user_args+=(
108
114
" --client-certificate=${KUBE_CERT} "
109
115
" --client-key=${KUBE_KEY} "
@@ -112,7 +118,7 @@ function create-kubeconfig() {
112
118
fi
113
119
114
120
KUBECONFIG=" ${KUBECONFIG} " " ${kubectl} " config set-cluster " ${CONTEXT} " " ${cluster_args[@]} "
115
- if [[ -n " ${user_args[@ ]:- } " ]]; then
121
+ if [[ -n " ${user_args[* ]:- } " ]]; then
116
122
KUBECONFIG=" ${KUBECONFIG} " " ${kubectl} " config set-credentials " ${CONTEXT} " " ${user_args[@]} "
117
123
fi
118
124
KUBECONFIG=" ${KUBECONFIG} " " ${kubectl} " config set-context " ${CONTEXT} " --cluster=" ${CONTEXT} " --user=" ${CONTEXT} "
@@ -124,7 +130,7 @@ function create-kubeconfig() {
124
130
# If we have a bearer token, also create a credential entry with basic auth
125
131
# so that it is easy to discover the basic auth password for your cluster
126
132
# to use in a web browser.
127
- if [[ ! -z " ${KUBE_BEARER_TOKEN:- } " && ! -z " ${KUBE_USER:- } " && ! -z " ${KUBE_PASSWORD:- } " ]]; then
133
+ if [[ -n " ${KUBE_BEARER_TOKEN:- } " && -n " ${KUBE_USER:- } " && -n " ${KUBE_PASSWORD:- } " ]]; then
128
134
KUBECONFIG=" ${KUBECONFIG} " " ${kubectl} " config set-credentials " ${CONTEXT} -basic-auth" " --username=${KUBE_USER} " " --password=${KUBE_PASSWORD} "
129
135
fi
130
136
@@ -147,7 +153,8 @@ function clear-kubeconfig() {
147
153
148
154
local kubectl=" ${KUBE_ROOT} /cluster/kubectl.sh"
149
155
# Unset the current-context before we delete it, as otherwise kubectl errors.
150
- local cc=$( " ${kubectl} " config view -o jsonpath=' {.current-context}' )
156
+ local cc
157
+ cc=$( " ${kubectl} " config view -o jsonpath=' {.current-context}' )
151
158
if [[ " ${cc} " == " ${CONTEXT} " ]]; then
152
159
" ${kubectl} " config unset current-context
153
160
fi
@@ -173,11 +180,13 @@ function clear-kubeconfig() {
173
180
function get-kubeconfig-basicauth() {
174
181
export KUBECONFIG=${KUBECONFIG:- $DEFAULT_KUBECONFIG }
175
182
176
- local cc=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.current-context}" )
177
- if [[ ! -z " ${KUBE_CONTEXT:- } " ]]; then
183
+ local cc
184
+ cc=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.current-context}" )
185
+ if [[ -n " ${KUBE_CONTEXT:- } " ]]; then
178
186
cc=" ${KUBE_CONTEXT} "
179
187
fi
180
- local user=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.contexts[?(@.name == \" ${cc} \" )].context.user}" )
188
+ local user
189
+ user=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.contexts[?(@.name == \" ${cc} \" )].context.user}" )
181
190
get-kubeconfig-user-basicauth " ${user} "
182
191
183
192
if [[ -z " ${KUBE_USER:- } " || -z " ${KUBE_PASSWORD:- } " ]]; then
@@ -211,7 +220,7 @@ function get-kubeconfig-user-basicauth() {
211
220
# KUBE_USER
212
221
# KUBE_PASSWORD
213
222
function gen-kube-basicauth() {
214
- KUBE_USER=admin
223
+ KUBE_USER=' admin'
215
224
KUBE_PASSWORD=$( python -c ' import string,random; print("".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16)))' )
216
225
}
217
226
@@ -228,11 +237,13 @@ function gen-kube-basicauth() {
228
237
function get-kubeconfig-bearertoken() {
229
238
export KUBECONFIG=${KUBECONFIG:- $DEFAULT_KUBECONFIG }
230
239
231
- local cc=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.current-context}" )
232
- if [[ ! -z " ${KUBE_CONTEXT:- } " ]]; then
240
+ local cc
241
+ cc=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.current-context}" )
242
+ if [[ -n " ${KUBE_CONTEXT:- } " ]]; then
233
243
cc=" ${KUBE_CONTEXT} "
234
244
fi
235
- local user=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.contexts[?(@.name == \" ${cc} \" )].context.user}" )
245
+ local user
246
+ user=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.contexts[?(@.name == \" ${cc} \" )].context.user}" )
236
247
KUBE_BEARER_TOKEN=$( " ${KUBE_ROOT} /cluster/kubectl.sh" config view -o jsonpath=" {.users[?(@.name == \" ${user} \" )].user.token}" )
237
248
}
238
249
@@ -245,7 +256,7 @@ function gen-kube-bearertoken() {
245
256
}
246
257
247
258
function load-or-gen-kube-basicauth() {
248
- if [[ ! -z " ${KUBE_CONTEXT:- } " ]]; then
259
+ if [[ -n " ${KUBE_CONTEXT:- } " ]]; then
249
260
get-kubeconfig-basicauth
250
261
fi
251
262
@@ -273,11 +284,11 @@ function load-or-gen-kube-basicauth() {
273
284
#
274
285
# Args:
275
286
# $1 version string from command line
276
- # Vars set:
287
+ # Vars set and exported for external reference :
277
288
# KUBE_VERSION
278
289
function set_binary_version() {
279
290
if [[ " ${1} " =~ " /" ]]; then
280
- IFS=' /' read -a path <<< " ${1}"
291
+ IFS=' /' read -r - a path <<< " ${1}"
281
292
if [[ " ${path[0]} " == " release" ]]; then
282
293
KUBE_VERSION=$( gsutil cat " gs://kubernetes-release/${1} .txt" )
283
294
else
@@ -286,6 +297,7 @@ function set_binary_version() {
286
297
else
287
298
KUBE_VERSION=${1}
288
299
fi
300
+ export KUBE_VERSION
289
301
}
290
302
291
303
# Search for the specified tarball in the various known output locations,
@@ -317,7 +329,7 @@ function find-tar() {
317
329
#
318
330
# Assumed vars:
319
331
# KUBE_ROOT
320
- # Vars set:
332
+ # Vars set and exported :
321
333
# NODE_BINARY_TAR
322
334
# SERVER_BINARY_TAR
323
335
# KUBE_MANIFESTS_TAR
@@ -326,15 +338,35 @@ function find-release-tars() {
326
338
if [[ -z " ${SERVER_BINARY_TAR} " ]]; then
327
339
exit 1
328
340
fi
341
+ export SERVER_BINARY_TAR
342
+
343
+ local find_result
329
344
if [[ " ${NUM_WINDOWS_NODES} " -gt " 0" ]]; then
330
- NODE_BINARY_TAR=$( find-tar kubernetes-node-windows-amd64.tar.gz)
345
+ if NODE_BINARY_TAR=$( find-tar kubernetes-node-windows-amd64.tar.gz) ; then
346
+ find_result=0
347
+ else
348
+ find_result=1
349
+ fi
350
+ export NODE_BINARY_TAR
331
351
fi
332
352
333
353
# This tarball is used by GCI, Ubuntu Trusty, and Container Linux.
334
354
KUBE_MANIFESTS_TAR=
335
355
if [[ " ${MASTER_OS_DISTRIBUTION:- } " == " trusty" || " ${MASTER_OS_DISTRIBUTION:- } " == " gci" || " ${MASTER_OS_DISTRIBUTION:- } " == " ubuntu" ]] || \
336
356
[[ " ${NODE_OS_DISTRIBUTION:- } " == " trusty" || " ${NODE_OS_DISTRIBUTION:- } " == " gci" || " ${NODE_OS_DISTRIBUTION:- } " == " ubuntu" || " ${NODE_OS_DISTRIBUTION:- } " == " custom" ]] ; then
337
- KUBE_MANIFESTS_TAR=$( find-tar kubernetes-manifests.tar.gz)
357
+ if KUBE_MANIFESTS_TAR=$( find-tar kubernetes-manifests.tar.gz) ; then
358
+ find_result=0
359
+ else
360
+ find_result=1
361
+ fi
362
+ export KUBE_MANIFESTS_TAR
363
+ fi
364
+
365
+ # the function result is used in function `verify-release-tars`
366
+ if [[ $find_result == 0 ]]; then
367
+ return 0
368
+ else
369
+ return 1
338
370
fi
339
371
}
340
372
@@ -344,7 +376,8 @@ function find-release-tars() {
344
376
# Optional vars:
345
377
# GEN_ETCD_CA_CERT (CA cert encode with base64 and ZIP compression)
346
378
# GEN_ETCD_CA_KEY (CA key encode with base64)
347
- #
379
+ # ca_cert (require when GEN_ETCD_CA_CERT and GEN_ETCD_CA_KEY is set)
380
+ # ca_key (require when GEN_ETCD_CA_CERT and GEN_ETCD_CA_KEY is set)
348
381
# If GEN_ETCD_CA_CERT or GEN_ETCD_CA_KEY is not specified, it will generates certs for CA.
349
382
#
350
383
# Args:
426
459
fi
427
460
428
461
if [[ -n " ${GEN_ETCD_CA_CERT} " && -n " ${GEN_ETCD_CA_KEY} " ]]; then
462
+ # ca_cert and ca_key are optional external vars supplied in cluster/gce/util.sh,
463
+ # so it's ok to disable shellcheck here
464
+ # shellcheck disable=SC2154
429
465
echo " ${ca_cert} " | base64 --decode | gunzip > ca.pem
466
+ # shellcheck disable=SC2154
430
467
echo " ${ca_key} " | base64 --decode > ca-key.pem
431
468
fi
432
469
@@ -443,13 +480,13 @@ EOF
443
480
;;
444
481
server)
445
482
echo " Generate server certificates..."
446
- echo ' {"CN":"' ${member_ip} ' ","hosts":[""],"key":{"algo":"ecdsa","size":256}}' \
483
+ echo ' {"CN":"' " ${member_ip} " ' ","hosts":[""],"key":{"algo":"ecdsa","size":256}}' \
447
484
| ${CFSSL_BIN} gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server -hostname=" ${member_ip} ,127.0.0.1" - \
448
485
| ${CFSSLJSON_BIN} -bare " ${prefix} "
449
486
;;
450
487
peer)
451
488
echo " Generate peer certificates..."
452
- echo ' {"CN":"' ${member_ip} ' ","hosts":[""],"key":{"algo":"ecdsa","size":256}}' \
489
+ echo ' {"CN":"' " ${member_ip} " ' ","hosts":[""],"key":{"algo":"ecdsa","size":256}}' \
453
490
| ${CFSSL_BIN} gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=peer -hostname=" ${member_ip} ,127.0.0.1" - \
454
491
| ${CFSSLJSON_BIN} -bare " ${prefix} "
455
492
;;
459
496
exit 2
460
497
esac
461
498
499
+ # the popd will access `directory stack`, no `real` parameters is actually needed
500
+ # shellcheck disable=SC2119
462
501
popd
463
502
}
464
503
@@ -478,7 +517,7 @@ function verify-kube-binaries() {
478
517
# If KUBERNETES_SKIP_CONFIRM is set to y, we'll automatically download binaries
479
518
# without prompting.
480
519
function verify-release-tars() {
481
- if ! $( find-release-tars) ; then
520
+ if ! find-release-tars; then
482
521
download-release-binaries
483
522
fi
484
523
}
@@ -489,7 +528,7 @@ function download-release-binaries() {
489
528
local resp=" y"
490
529
if [[ ! " ${KUBERNETES_SKIP_CONFIRM:- n} " =~ ^[yY]$ ]]; then
491
530
echo " Required release artifacts appear to be missing. Do you wish to download them? [Y/n]"
492
- read resp
531
+ read -r resp
493
532
fi
494
533
if [[ " ${resp} " =~ ^[nN]$ ]]; then
495
534
echo " You must download release artifacts to continue. You can use "
@@ -502,10 +541,12 @@ function download-release-binaries() {
502
541
503
542
# Run pushd without stack output
504
543
function pushd() {
505
- command pushd $@ > /dev/null
544
+ command pushd " $@ " > /dev/null
506
545
}
507
546
508
547
# Run popd without stack output
548
+ # the popd will access `directory stack`, no `real` parameters is actually needed
549
+ # shellcheck disable=SC2120
509
550
function popd() {
510
- command popd $@ > /dev/null
551
+ command popd " $@ " > /dev/null
511
552
}
0 commit comments