Skip to content

Commit 7a01cdc

Browse files
authored
Merge pull request kubernetes#82454 from beautytiger/fix_shellcheck_common.sh
fix shell checks errors in cluster/common.sh
2 parents 9de5763 + faf2733 commit 7a01cdc

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

cluster/common.sh

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ set -o errexit
2020
set -o nounset
2121
set -o pipefail
2222

23-
KUBE_ROOT=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
23+
KUBE_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)
2424

2525
DEFAULT_KUBECONFIG="${HOME:-.}/.kube/config"
2626

@@ -29,15 +29,21 @@ source "${KUBE_ROOT}/hack/lib/util.sh"
2929
#
3030
# NOTE This must match the version_regex in build/common.sh
3131
# 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]*))?"
3437

3538
# KUBE_CI_VERSION_REGEX matches things like "v1.2.3-alpha.4.56+abcdefg" This
3639
#
3740
# NOTE This must match the version_regex in build/common.sh
3841
# 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]*)?"
4147

4248
# Generate kubeconfig data for the created cluster.
4349
# Assumed vars:
@@ -93,17 +99,17 @@ function create-kubeconfig() {
9399
fi
94100

95101
local user_args=()
96-
if [[ ! -z "${KUBE_BEARER_TOKEN:-}" ]]; then
102+
if [[ -n "${KUBE_BEARER_TOKEN:-}" ]]; then
97103
user_args+=(
98104
"--token=${KUBE_BEARER_TOKEN}"
99105
)
100-
elif [[ ! -z "${KUBE_USER:-}" && ! -z "${KUBE_PASSWORD:-}" ]]; then
106+
elif [[ -n "${KUBE_USER:-}" && -n "${KUBE_PASSWORD:-}" ]]; then
101107
user_args+=(
102108
"--username=${KUBE_USER}"
103109
"--password=${KUBE_PASSWORD}"
104110
)
105111
fi
106-
if [[ ! -z "${KUBE_CERT:-}" && ! -z "${KUBE_KEY:-}" ]]; then
112+
if [[ -n "${KUBE_CERT:-}" && -n "${KUBE_KEY:-}" ]]; then
107113
user_args+=(
108114
"--client-certificate=${KUBE_CERT}"
109115
"--client-key=${KUBE_KEY}"
@@ -112,7 +118,7 @@ function create-kubeconfig() {
112118
fi
113119

114120
KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-cluster "${CONTEXT}" "${cluster_args[@]}"
115-
if [[ -n "${user_args[@]:-}" ]]; then
121+
if [[ -n "${user_args[*]:-}" ]]; then
116122
KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-credentials "${CONTEXT}" "${user_args[@]}"
117123
fi
118124
KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-context "${CONTEXT}" --cluster="${CONTEXT}" --user="${CONTEXT}"
@@ -124,7 +130,7 @@ function create-kubeconfig() {
124130
# If we have a bearer token, also create a credential entry with basic auth
125131
# so that it is easy to discover the basic auth password for your cluster
126132
# 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
128134
KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-credentials "${CONTEXT}-basic-auth" "--username=${KUBE_USER}" "--password=${KUBE_PASSWORD}"
129135
fi
130136

@@ -147,7 +153,8 @@ function clear-kubeconfig() {
147153

148154
local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
149155
# 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}')
151158
if [[ "${cc}" == "${CONTEXT}" ]]; then
152159
"${kubectl}" config unset current-context
153160
fi
@@ -173,11 +180,13 @@ function clear-kubeconfig() {
173180
function get-kubeconfig-basicauth() {
174181
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
175182

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
178186
cc="${KUBE_CONTEXT}"
179187
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}")
181190
get-kubeconfig-user-basicauth "${user}"
182191

183192
if [[ -z "${KUBE_USER:-}" || -z "${KUBE_PASSWORD:-}" ]]; then
@@ -211,7 +220,7 @@ function get-kubeconfig-user-basicauth() {
211220
# KUBE_USER
212221
# KUBE_PASSWORD
213222
function gen-kube-basicauth() {
214-
KUBE_USER=admin
223+
KUBE_USER='admin'
215224
KUBE_PASSWORD=$(python -c 'import string,random; print("".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16)))')
216225
}
217226

@@ -228,11 +237,13 @@ function gen-kube-basicauth() {
228237
function get-kubeconfig-bearertoken() {
229238
export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
230239

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
233243
cc="${KUBE_CONTEXT}"
234244
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}")
236247
KUBE_BEARER_TOKEN=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.token}")
237248
}
238249

@@ -245,7 +256,7 @@ function gen-kube-bearertoken() {
245256
}
246257

247258
function load-or-gen-kube-basicauth() {
248-
if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
259+
if [[ -n "${KUBE_CONTEXT:-}" ]]; then
249260
get-kubeconfig-basicauth
250261
fi
251262

@@ -273,11 +284,11 @@ function load-or-gen-kube-basicauth() {
273284
#
274285
# Args:
275286
# $1 version string from command line
276-
# Vars set:
287+
# Vars set and exported for external reference:
277288
# KUBE_VERSION
278289
function set_binary_version() {
279290
if [[ "${1}" =~ "/" ]]; then
280-
IFS='/' read -a path <<< "${1}"
291+
IFS='/' read -r -a path <<< "${1}"
281292
if [[ "${path[0]}" == "release" ]]; then
282293
KUBE_VERSION=$(gsutil cat "gs://kubernetes-release/${1}.txt")
283294
else
@@ -286,6 +297,7 @@ function set_binary_version() {
286297
else
287298
KUBE_VERSION=${1}
288299
fi
300+
export KUBE_VERSION
289301
}
290302

291303
# Search for the specified tarball in the various known output locations,
@@ -317,7 +329,7 @@ function find-tar() {
317329
#
318330
# Assumed vars:
319331
# KUBE_ROOT
320-
# Vars set:
332+
# Vars set and exported:
321333
# NODE_BINARY_TAR
322334
# SERVER_BINARY_TAR
323335
# KUBE_MANIFESTS_TAR
@@ -326,15 +338,35 @@ function find-release-tars() {
326338
if [[ -z "${SERVER_BINARY_TAR}" ]]; then
327339
exit 1
328340
fi
341+
export SERVER_BINARY_TAR
342+
343+
local find_result
329344
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
331351
fi
332352

333353
# This tarball is used by GCI, Ubuntu Trusty, and Container Linux.
334354
KUBE_MANIFESTS_TAR=
335355
if [[ "${MASTER_OS_DISTRIBUTION:-}" == "trusty" || "${MASTER_OS_DISTRIBUTION:-}" == "gci" || "${MASTER_OS_DISTRIBUTION:-}" == "ubuntu" ]] || \
336356
[[ "${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
338370
fi
339371
}
340372

@@ -344,7 +376,8 @@ function find-release-tars() {
344376
# Optional vars:
345377
# GEN_ETCD_CA_CERT (CA cert encode with base64 and ZIP compression)
346378
# 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)
348381
# If GEN_ETCD_CA_CERT or GEN_ETCD_CA_KEY is not specified, it will generates certs for CA.
349382
#
350383
# Args:
@@ -426,7 +459,11 @@ EOF
426459
fi
427460

428461
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
429465
echo "${ca_cert}" | base64 --decode | gunzip > ca.pem
466+
# shellcheck disable=SC2154
430467
echo "${ca_key}" | base64 --decode > ca-key.pem
431468
fi
432469

@@ -443,13 +480,13 @@ EOF
443480
;;
444481
server)
445482
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}}' \
447484
| ${CFSSL_BIN} gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server -hostname="${member_ip},127.0.0.1" - \
448485
| ${CFSSLJSON_BIN} -bare "${prefix}"
449486
;;
450487
peer)
451488
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}}' \
453490
| ${CFSSL_BIN} gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=peer -hostname="${member_ip},127.0.0.1" - \
454491
| ${CFSSLJSON_BIN} -bare "${prefix}"
455492
;;
@@ -459,6 +496,8 @@ EOF
459496
exit 2
460497
esac
461498

499+
# the popd will access `directory stack`, no `real` parameters is actually needed
500+
# shellcheck disable=SC2119
462501
popd
463502
}
464503

@@ -478,7 +517,7 @@ function verify-kube-binaries() {
478517
# If KUBERNETES_SKIP_CONFIRM is set to y, we'll automatically download binaries
479518
# without prompting.
480519
function verify-release-tars() {
481-
if ! $(find-release-tars); then
520+
if ! find-release-tars; then
482521
download-release-binaries
483522
fi
484523
}
@@ -489,7 +528,7 @@ function download-release-binaries() {
489528
local resp="y"
490529
if [[ ! "${KUBERNETES_SKIP_CONFIRM:-n}" =~ ^[yY]$ ]]; then
491530
echo "Required release artifacts appear to be missing. Do you wish to download them? [Y/n]"
492-
read resp
531+
read -r resp
493532
fi
494533
if [[ "${resp}" =~ ^[nN]$ ]]; then
495534
echo "You must download release artifacts to continue. You can use "
@@ -502,10 +541,12 @@ function download-release-binaries() {
502541

503542
# Run pushd without stack output
504543
function pushd() {
505-
command pushd $@ > /dev/null
544+
command pushd "$@" > /dev/null
506545
}
507546

508547
# Run popd without stack output
548+
# the popd will access `directory stack`, no `real` parameters is actually needed
549+
# shellcheck disable=SC2120
509550
function popd() {
510-
command popd $@ > /dev/null
551+
command popd "$@" > /dev/null
511552
}

hack/.shellcheck_failures

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
./build/lib/release.sh
2-
./cluster/common.sh
32
./cluster/gce/config-default.sh
43
./cluster/gce/config-test.sh
54
./cluster/gce/gci/configure-helper.sh

0 commit comments

Comments
 (0)