Skip to content

Commit 3f8b9f1

Browse files
committed
hack: add verify-shellcheck target to validate bash scripts in the repository
* add a bash script to run shellcheck on all the scripts in the repository. * add `verify-shellcheck` make target to run the shellcheck script. * fix shellcheck warnings in existing bash scripts. Signed-off-by: Deepesh Pathak <[email protected]>
1 parent 9cb9556 commit 3f8b9f1

23 files changed

+262
-91
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ clean-release: ## Remove the release folder
568568
rm -rf $(RELEASE_DIR)
569569

570570
.PHONY: verify
571-
verify: verify-boilerplate verify-modules verify-gen
571+
verify: verify-boilerplate verify-modules verify-gen verify-shellcheck
572572

573573
.PHONY: verify-boilerplate
574574
verify-boilerplate:
@@ -585,3 +585,7 @@ verify-gen: generate
585585
@if !(git diff --quiet HEAD); then \
586586
git diff; echo "generated files are out of date, run make generate"; exit 1; \
587587
fi
588+
589+
.PHONY: verify-shellcheck
590+
verify-shellcheck:
591+
./hack/verify-shellcheck.sh

hack/create-dev-cluster.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ export AZURE_VNET_NAME=${CLUSTER_NAME}-vnet
3434
# Azure settings.
3535
export AZURE_LOCATION="${AZURE_LOCATION:-southcentralus}"
3636
export AZURE_RESOURCE_GROUP=${CLUSTER_NAME}
37-
export AZURE_SUBSCRIPTION_ID_B64="$(echo -n "$AZURE_SUBSCRIPTION_ID" | base64 | tr -d '\n')"
38-
export AZURE_TENANT_ID_B64="$(echo -n "$AZURE_TENANT_ID" | base64 | tr -d '\n')"
39-
export AZURE_CLIENT_ID_B64="$(echo -n "$AZURE_CLIENT_ID" | base64 | tr -d '\n')"
40-
export AZURE_CLIENT_SECRET_B64="$(echo -n "$AZURE_CLIENT_SECRET" | base64 | tr -d '\n')"
37+
38+
AZURE_SUBSCRIPTION_ID_B64="$(echo -n "$AZURE_SUBSCRIPTION_ID" | base64 | tr -d '\n')"
39+
AZURE_TENANT_ID_B64="$(echo -n "$AZURE_TENANT_ID" | base64 | tr -d '\n')"
40+
AZURE_CLIENT_ID_B64="$(echo -n "$AZURE_CLIENT_ID" | base64 | tr -d '\n')"
41+
AZURE_CLIENT_SECRET_B64="$(echo -n "$AZURE_CLIENT_SECRET" | base64 | tr -d '\n')"
42+
43+
export AZURE_SUBSCRIPTION_ID_B64 AZURE_TENANT_ID_B64 AZURE_CLIENT_ID_B64 AZURE_CLIENT_SECRET_B64
4144

4245
# Machine settings.
4346
export CONTROL_PLANE_MACHINE_COUNT=${CONTROL_PLANE_MACHINE_COUNT:-3}
@@ -49,13 +52,15 @@ export CLUSTER_TEMPLATE="${CLUSTER_TEMPLATE:-cluster-template.yaml}"
4952

5053
# Generate SSH key.
5154
SSH_KEY_FILE=${SSH_KEY_FILE:-""}
52-
if ! [ -n "$SSH_KEY_FILE" ]; then
55+
if [ -z "$SSH_KEY_FILE" ]; then
5356
SSH_KEY_FILE=.sshkey
5457
rm -f "${SSH_KEY_FILE}" 2>/dev/null
5558
ssh-keygen -t rsa -b 2048 -f "${SSH_KEY_FILE}" -N '' 1>/dev/null
5659
echo "Machine SSH key generated in ${SSH_KEY_FILE}"
5760
fi
58-
export AZURE_SSH_PUBLIC_KEY_B64=$(cat "${SSH_KEY_FILE}.pub" | base64 | tr -d '\r\n')
61+
62+
AZURE_SSH_PUBLIC_KEY_B64=$(base64 "${SSH_KEY_FILE}.pub" | tr -d '\r\n')
63+
export AZURE_SSH_PUBLIC_KEY_B64
5964

6065
echo "================ DOCKER BUILD ==============="
6166
PULL_POLICY=IfNotPresent make modules docker-build

hack/ensure-acr-login.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cd "${REPO_ROOT}" || exit 1
2323

2424
if [[ "${REGISTRY:-}" =~ capzci\.azurecr\.io ]]; then
2525
# if we are using the prow Azure Container Registry, login.
26-
${REPO_ROOT}/hack/ensure-azcli.sh
26+
"${REPO_ROOT}/hack/ensure-azcli.sh"
2727
: "${AZURE_SUBSCRIPTION_ID:?Environment variable empty or not defined.}"
2828
az account set -s "${AZURE_SUBSCRIPTION_ID}"
2929
az acr login --name capzci

hack/ensure-kind.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ verify_kind_version() {
4343
IFS=" " read -ra kind_version <<< "$(kind version)"
4444
if [[ "${MINIMUM_KIND_VERSION}" != $(echo -e "${MINIMUM_KIND_VERSION}\n${kind_version[1]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) ]]; then
4545
cat <<EOF
46-
Detected kind version: ${kind_version}.
46+
Detected kind version: ${kind_version[0]}.
4747
Requires ${MINIMUM_KIND_VERSION} or greater.
4848
Please install ${MINIMUM_KIND_VERSION} or later.
4949
EOF

hack/install-cert-manager.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ kubectl wait --for=condition=Available --timeout=5m -n cert-manager deployment/c
5252
kubectl wait --for=condition=Available --timeout=5m -n cert-manager deployment/cert-manager-cainjector
5353
kubectl wait --for=condition=Available --timeout=5m -n cert-manager deployment/cert-manager-webhook
5454

55-
for i in {1..6}; do (echo "$TEST_RESOURCE" | kubectl apply -f - ) && break || sleep 15; done
55+
for _ in {1..6}; do
56+
(echo "$TEST_RESOURCE" | kubectl apply -f -) && break
57+
sleep 15
58+
done
59+
5660
kubectl wait --for=condition=Ready --timeout=300s -n cert-manager-test certificate/selfsigned-cert
5761
echo "$TEST_RESOURCE" | kubectl delete -f -

hack/kustomize-sub.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ set -o nounset
1818
set -o pipefail
1919

2020
root=$(dirname "${BASH_SOURCE[0]}")
21-
$root/tools/bin/kustomize build $1 | $root/tools/bin/envsubst
21+
"$root/tools/bin/kustomize" build "$1" | "$root/tools/bin/envsubst"

hack/log/log-dump.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ set -o pipefail
2121
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
2222
cd "${REPO_ROOT}" || exit 1
2323

24-
# shellcheck source=../hack/ensure-kind.sh
24+
# shellcheck source=hack/ensure-kind.sh
2525
source "${REPO_ROOT}/hack/ensure-kind.sh"
26-
# shellcheck source=../hack/ensure-kubectl.sh
26+
# shellcheck source=hack/ensure-kubectl.sh
2727
source "${REPO_ROOT}/hack/ensure-kubectl.sh"
2828

2929
export ARTIFACTS="${ARTIFACTS:-${PWD}/_artifacts}"
@@ -33,7 +33,8 @@ export KUBECONFIG="${KUBECONFIG:-${PWD}/kubeconfig}"
3333

3434
get_node_name() {
3535
local -r pod_name="${1}"
36-
echo "$(kubectl get pod "${pod_name}" -ojsonpath={.spec.nodeName})"
36+
# shellcheck disable=SC1083
37+
kubectl get pod "${pod_name}" -ojsonpath={.spec.nodeName}
3738
}
3839

3940
dump_mgmt_cluster_logs() {
@@ -90,7 +91,8 @@ dump_workload_cluster_logs() {
9091
kubectl apply -f "${REPO_ROOT}/hack/log/log-dump-daemonset.yaml"
9192
kubectl wait pod -l app=log-dump-node --for=condition=Ready --timeout=5m
9293

93-
local -r log_dump_pods=( $(kubectl get pod -l app=log-dump-node -ojsonpath={.items[*].metadata.name}) )
94+
local -r log_dump_pods=()
95+
IFS=" " read -r -a log_dump_pods <<< "$(kubectl get pod -l app=log-dump-node -ojsonpath='{.items[*].metadata.name}')"
9496
local log_dump_commands=(
9597
"journalctl --output=short-precise -u kubelet > kubelet.log"
9698
"journalctl --output=short-precise -u containerd > containerd.log"
@@ -108,7 +110,8 @@ dump_workload_cluster_logs() {
108110
fi
109111

110112
for log_dump_pod in "${log_dump_pods[@]}"; do
111-
local node_name="$(get_node_name "${log_dump_pod}")"
113+
local node_name
114+
node_name="$(get_node_name "${log_dump_pod}")"
112115

113116
local log_dump_dir="${ARTIFACTS}/workload-cluster/${node_name}"
114117
mkdir -p "${log_dump_dir}"
@@ -127,6 +130,7 @@ dump_workload_cluster_logs() {
127130

128131
cleanup() {
129132
kubectl delete -f "${REPO_ROOT}/hack/log/log-dump-daemonset.yaml" || true
133+
# shellcheck source=hack/log/redact.sh
130134
source "${REPO_ROOT}/hack/log/redact.sh"
131135
}
132136

hack/log/redact.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ set -o pipefail
2020

2121
echo "================ REDACTING LOGS ================"
2222

23-
log_files=( $(find "${ARTIFACTS:-${PWD}/_artifacts}" -type f) )
23+
log_files=()
24+
while IFS='' read -r line; do log_files+=("$line"); done < <(find "${ARTIFACTS:-${PWD}/_artifacts}" -type f)
2425
redact_vars=(
2526
"${AZURE_CLIENT_ID:-}"
2627
"${AZURE_CLIENT_SECRET:-}"

hack/parse-prow-creds.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ parse_cred() {
2525
# for Prow we use the provided AZURE_CREDENTIALS file.
2626
# the file is expected to be in toml format.
2727
if [[ -n "${AZURE_CREDENTIALS:-}" ]]; then
28-
export AZURE_SUBSCRIPTION_ID="$(cat ${AZURE_CREDENTIALS} | parse_cred SubscriptionID)"
29-
export AZURE_TENANT_ID="$(cat ${AZURE_CREDENTIALS} | parse_cred TenantID)"
30-
export AZURE_CLIENT_ID="$(cat ${AZURE_CREDENTIALS} | parse_cred ClientID)"
31-
export AZURE_CLIENT_SECRET="$(cat ${AZURE_CREDENTIALS} | parse_cred ClientSecret)"
32-
export AZURE_MULTI_TENANCY_ID="$(cat ${AZURE_CREDENTIALS} | parse_cred MultiTenancyClientID)"
33-
export AZURE_MULTI_TENANCY_SECRET="$(cat ${AZURE_CREDENTIALS} | parse_cred MultiTenancyClientSecret)"
34-
export AZURE_STORAGE_ACCOUNT="$(cat ${AZURE_CREDENTIALS} | parse_cred StorageAccountName)"
35-
export AZURE_STORAGE_KEY="$(cat ${AZURE_CREDENTIALS} | parse_cred StorageAccountKey)"
28+
AZURE_SUBSCRIPTION_ID="$(parse_cred SubscriptionID < "${AZURE_CREDENTIALS}")"
29+
AZURE_TENANT_ID="$(parse_cred TenantID < "${AZURE_CREDENTIALS}")"
30+
AZURE_CLIENT_ID="$(parse_cred ClientID < "${AZURE_CREDENTIALS}")"
31+
AZURE_CLIENT_SECRET="$(parse_cred ClientSecret < "${AZURE_CREDENTIALS}")"
32+
AZURE_MULTI_TENANCY_ID="$(parse_cred MultiTenancyClientID < "${AZURE_CREDENTIALS}")"
33+
AZURE_MULTI_TENANCY_SECRET="$(parse_cred MultiTenancyClientSecret < "${AZURE_CREDENTIALS}")"
34+
AZURE_STORAGE_ACCOUNT="$(parse_cred StorageAccountName < "${AZURE_CREDENTIALS}")"
35+
AZURE_STORAGE_KEY="$(parse_cred StorageAccountKey < "${AZURE_CREDENTIALS}")"
36+
37+
export AZURE_SUBSCRIPTION_ID AZURE_TENANT_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET AZURE_MULTI_TENANCY_ID AZURE_MULTI_TENANCY_SECRET AZURE_STORAGE_ACCOUNT AZURE_STORAGE_KEY
3638
fi

hack/print-workspace-status.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ if GIT_VERSION=$(git describe --tags --abbrev=14 2>/dev/null); then
3434
#
3535
# TODO: We continue calling this "git version" because so many
3636
# downstream consumers are expecting it there.
37+
# shellcheck disable=SC2001
3738
DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g")
3839
if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
3940
# We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
41+
# shellcheck disable=SC2001
4042
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\-\2/")
4143
elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
4244
# We have distance to base tag (v1.1.0-1-gCommitHash)
45+
# shellcheck disable=SC2001
4346
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/-\1/")
4447
fi
4548
if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then
@@ -66,9 +69,9 @@ if GIT_VERSION=$(git describe --tags --abbrev=14 2>/dev/null); then
6669
fi
6770
fi
6871

69-
GIT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
72+
GIT_BRANCH=$(git branch | grep '\*' | cut -d ' ' -f2)
7073
GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags)
71-
GIT_RELEASE_COMMIT=$(git rev-list -n 1 ${GIT_RELEASE_TAG} | head -c 14)
74+
GIT_RELEASE_COMMIT=$(git rev-list -n 1 "${GIT_RELEASE_TAG}" | head -c 14)
7275

7376
cat <<EOF
7477
GIT_COMMIT ${GIT_COMMIT-}

0 commit comments

Comments
 (0)