Skip to content

Commit 2eda472

Browse files
authored
Merge pull request kubernetes#83604 from k-toyoda-pi/fix_shellcheck_log-dump
Fix shellcheck failure in cluster/log-dump/log-dump.sh
2 parents 3dcc1a1 + e99a325 commit 2eda472

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

cluster/log-dump/log-dump.sh

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ readonly max_dump_processes=25
6565
function setup() {
6666
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
6767
if [[ -z "${use_custom_instance_list}" ]]; then
68-
: ${KUBE_CONFIG_FILE:="config-test.sh"}
68+
: "${KUBE_CONFIG_FILE:="config-test.sh"}"
6969
echo "Sourcing kube-util.sh"
7070
source "${KUBE_ROOT}/cluster/kube-util.sh"
7171
echo "Detecting project"
@@ -87,7 +87,7 @@ function setup() {
8787
}
8888

8989
function log-dump-ssh() {
90-
if [[ "${gcloud_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
90+
if [[ "${gcloud_supported_providers}" =~ ${KUBERNETES_PROVIDER} ]]; then
9191
ssh-to-node "$@"
9292
return
9393
fi
@@ -104,7 +104,7 @@ function log-dump-ssh() {
104104
function copy-logs-from-node() {
105105
local -r node="${1}"
106106
local -r dir="${2}"
107-
local files=( ${3} )
107+
read -r -a files <<< "${3}"
108108
# Append "*"
109109
# The * at the end is needed to also copy rotated logs (which happens
110110
# in large clusters and long runs).
@@ -114,12 +114,13 @@ function copy-logs-from-node() {
114114
# Comma delimit (even the singleton, or scp does the wrong thing), surround by braces.
115115
local -r scp_files="{$(printf "%s," "${files[@]}")}"
116116

117-
if [[ "${gcloud_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
117+
if [[ "${gcloud_supported_providers}" =~ ${KUBERNETES_PROVIDER} ]]; then
118118
# get-serial-port-output lets you ask for ports 1-4, but currently (11/21/2016) only port 1 contains useful information
119119
gcloud compute instances get-serial-port-output --project "${PROJECT}" --zone "${ZONE}" --port 1 "${node}" > "${dir}/serial-1.log" || true
120120
gcloud compute scp --recurse --project "${PROJECT}" --zone "${ZONE}" "${node}:${scp_files}" "${dir}" > /dev/null || true
121121
elif [[ "${KUBERNETES_PROVIDER}" == "aws" ]]; then
122-
local ip=$(get_ssh_hostname "${node}")
122+
local ip
123+
ip=$(get_ssh_hostname "${node}")
123124
scp -oLogLevel=quiet -oConnectTimeout=30 -oStrictHostKeyChecking=no -i "${AWS_SSH_KEY}" "${SSH_USER}@${ip}:${scp_files}" "${dir}" > /dev/null || true
124125
elif [[ -n "${use_custom_instance_list}" ]]; then
125126
scp -oLogLevel=quiet -oConnectTimeout=30 -oStrictHostKeyChecking=no -i "${LOG_DUMP_SSH_KEY}" "${LOG_DUMP_SSH_USER}@${node}:${scp_files}" "${dir}" > /dev/null || true
@@ -136,25 +137,25 @@ function copy-logs-from-node() {
136137
function save-logs() {
137138
local -r node_name="${1}"
138139
local -r dir="${2}"
139-
local files="${3}"
140+
local save_files="${3}"
140141
local opt_systemd_services="${4:-""}"
141142
local on_master="${5:-"false"}"
142143

143144
if [[ -n "${use_custom_instance_list}" ]]; then
144145
if [[ -n "${LOG_DUMP_SAVE_LOGS:-}" ]]; then
145-
files="${files} ${LOG_DUMP_SAVE_LOGS:-}"
146+
save_files="${save_files} ${LOG_DUMP_SAVE_LOGS:-}"
146147
fi
147148
else
148149
case "${KUBERNETES_PROVIDER}" in
149150
gce|gke|kubernetes-anywhere)
150-
files="${files} ${gce_logfiles}"
151+
save_files="${save_files} ${gce_logfiles}"
151152
;;
152153
aws)
153-
files="${files} ${aws_logfiles}"
154+
save_files="${save_files} ${aws_logfiles}"
154155
;;
155156
esac
156157
fi
157-
local -r services=( ${systemd_services} ${opt_systemd_services} ${LOG_DUMP_SAVE_SERVICES:-} )
158+
read -r -a services <<< "${systemd_services} ${opt_systemd_services} ${LOG_DUMP_SAVE_SERVICES:-}"
158159

159160
if log-dump-ssh "${node_name}" "command -v journalctl" &> /dev/null; then
160161
if [[ "${on_master}" == "true" ]]; then
@@ -174,7 +175,7 @@ function save-logs() {
174175
log-dump-ssh "${node_name}" "sudo journalctl --output=short-precise" > "${dir}/systemd.log" || true
175176
fi
176177
else
177-
files="${kern_logfile} ${files} ${initd_logfiles} ${supervisord_logfiles}"
178+
save_files="${kern_logfile} ${save_files} ${initd_logfiles} ${supervisord_logfiles}"
178179
fi
179180

180181
# Try dumping coverage profiles, if it looks like coverage is enabled in the first place.
@@ -195,8 +196,8 @@ function save-logs() {
195196
echo "Changing logfiles to be world-readable for download"
196197
log-dump-ssh "${node_name}" "sudo chmod -R a+r /var/log" || true
197198

198-
echo "Copying '${files}' from ${node_name}"
199-
copy-logs-from-node "${node_name}" "${dir}" "${files}"
199+
echo "Copying '${save_files}' from ${node_name}"
200+
copy-logs-from-node "${node_name}" "${dir}" "${save_files}"
200201
}
201202

202203
# Saves a copy of the Windows Docker event log to ${WINDOWS_LOGS_DIR}\docker.log
@@ -223,8 +224,9 @@ function save-windows-logs-via-diagnostics-tool() {
223224
local node="${1}"
224225
local dest_dir="${2}"
225226

226-
gcloud compute instances add-metadata ${node} --metadata enable-diagnostics=true --project=${PROJECT} --zone=${ZONE}
227-
local logs_archive_in_gcs=$(gcloud alpha compute diagnose export-logs ${node} --zone=${ZONE} --project=${PROJECT} | tail -n 1)
227+
gcloud compute instances add-metadata "${node}" --metadata enable-diagnostics=true --project="${PROJECT}" --zone="${ZONE}"
228+
local logs_archive_in_gcs
229+
logs_archive_in_gcs=$(gcloud alpha compute diagnose export-logs "${node}" --zone="${ZONE}" --project="${PROJECT}" | tail -n 1)
228230
local temp_local_path="${node}.zip"
229231
for retry in {1..20}; do
230232
if gsutil mv "${logs_archive_in_gcs}" "${temp_local_path}" > /dev/null 2>&1; then
@@ -236,8 +238,8 @@ function save-windows-logs-via-diagnostics-tool() {
236238
done
237239

238240
if [[ -f "${temp_local_path}" ]]; then
239-
unzip ${temp_local_path} -d "${dest_dir}" > /dev/null
240-
rm -f ${temp_local_path}
241+
unzip "${temp_local_path}" -d "${dest_dir}" > /dev/null
242+
rm -f "${temp_local_path}"
241243
fi
242244
}
243245

@@ -249,14 +251,14 @@ function save-windows-logs-via-ssh() {
249251
export-windows-docker-event-log "${node}"
250252

251253
local remote_files=()
252-
for file in ${windows_node_logfiles[@]}; do
254+
for file in ${windows_node_logfiles}; do
253255
remote_files+=( "${WINDOWS_LOGS_DIR}\\${file}" )
254256
done
255257
remote_files+=( "${windows_node_otherfiles[@]}" )
256258

257259
# TODO(pjh, yujuhong): handle rotated logs and copying multiple files at the
258260
# same time.
259-
for remote_file in ${remote_files[@]}; do
261+
for remote_file in "${remote_files[@]}"; do
260262
# Retry up to 3 times to allow ssh keys to be properly propagated and
261263
# stored.
262264
for retry in {1..3}; do
@@ -278,7 +280,7 @@ function save-logs-windows() {
278280
local -r node="${1}"
279281
local -r dest_dir="${2}"
280282

281-
if [[ ! "${gcloud_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
283+
if [[ ! "${gcloud_supported_providers}" =~ ${KUBERNETES_PROVIDER} ]]; then
282284
echo "Not saving logs for ${node}, Windows log dumping requires gcloud support"
283285
return
284286
fi
@@ -300,14 +302,14 @@ function run-in-docker-container() {
300302
local node_name="$1"
301303
local container="$2"
302304
shift 2
303-
log-dump-ssh "${node_name}" "docker exec \"\$(docker ps -f label=io.kubernetes.container.name=${container} --format \"{{.ID}}\")\" $@"
305+
log-dump-ssh "${node_name}" "docker exec \"\$(docker ps -f label=io.kubernetes.container.name=${container} --format \"{{.ID}}\")\" $*"
304306
}
305307

306308
function dump_masters() {
307309
local master_names
308310
if [[ -n "${use_custom_instance_list}" ]]; then
309-
master_names=( $(log_dump_custom_get_instances master) )
310-
elif [[ ! "${master_ssh_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
311+
while IFS='' read -r line; do master_names+=("$line"); done < <(log_dump_custom_get_instances master)
312+
elif [[ ! "${master_ssh_supported_providers}" =~ ${KUBERNETES_PROVIDER} ]]; then
311313
echo "Master SSH not supported for ${KUBERNETES_PROVIDER}"
312314
return
313315
elif [[ -n "${KUBEMARK_MASTER_NAME:-}" ]]; then
@@ -358,8 +360,8 @@ function dump_nodes() {
358360
node_names=( "$@" )
359361
elif [[ -n "${use_custom_instance_list}" ]]; then
360362
echo "Dumping logs for nodes provided by log_dump_custom_get_instances() function"
361-
node_names=( $(log_dump_custom_get_instances node) )
362-
elif [[ ! "${node_ssh_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
363+
while IFS='' read -r line; do node_names+=("$line"); done < <(log_dump_custom_get_instances node)
364+
elif [[ ! "${node_ssh_supported_providers}" =~ ${KUBERNETES_PROVIDER} ]]; then
363365
echo "Node SSH not supported for ${KUBERNETES_PROVIDER}"
364366
return
365367
else
@@ -386,7 +388,7 @@ function dump_nodes() {
386388
linux_nodes_selected_for_logs=()
387389
if [[ -n "${LOGDUMP_ONLY_N_RANDOM_NODES:-}" ]]; then
388390
# We randomly choose 'LOGDUMP_ONLY_N_RANDOM_NODES' many nodes for fetching logs.
389-
for index in `shuf -i 0-$(( ${#node_names[*]} - 1 )) -n ${LOGDUMP_ONLY_N_RANDOM_NODES}`
391+
for index in $(shuf -i 0-$(( ${#node_names[*]} - 1 )) -n "${LOGDUMP_ONLY_N_RANDOM_NODES}")
390392
do
391393
linux_nodes_selected_for_logs+=("${node_names[$index]}")
392394
done
@@ -435,11 +437,11 @@ function dump_nodes() {
435437
# Sets:
436438
# NON_LOGEXPORTED_NODES
437439
function find_non_logexported_nodes() {
438-
succeeded_nodes=$(gsutil ls ${gcs_artifacts_dir}/logexported-nodes-registry) || return 1
440+
succeeded_nodes=$(gsutil ls "${gcs_artifacts_dir}/logexported-nodes-registry") || return 1
439441
echo "Successfully listed marker files for successful nodes"
440442
NON_LOGEXPORTED_NODES=()
441443
for node in "${NODE_NAMES[@]}"; do
442-
if [[ ! "${succeeded_nodes}" =~ "${node}" ]]; then
444+
if [[ ! "${succeeded_nodes}" =~ ${node} ]]; then
443445
NON_LOGEXPORTED_NODES+=("${node}")
444446
fi
445447
done
@@ -450,7 +452,7 @@ function find_non_logexported_nodes() {
450452
function dump_nodes_with_logexporter() {
451453
if [[ -n "${use_custom_instance_list}" ]]; then
452454
echo "Dumping logs for nodes provided by log_dump_custom_get_instances() function"
453-
NODE_NAMES=( $(log_dump_custom_get_instances node) )
455+
while IFS='' read -r line; do NODE_NAMES+=("$line"); done < <(log_dump_custom_get_instances node)
454456
else
455457
echo "Detecting nodes in the cluster"
456458
detect-node-names &> /dev/null
@@ -462,7 +464,7 @@ function dump_nodes_with_logexporter() {
462464
fi
463465

464466
# Obtain parameters required by logexporter.
465-
local -r service_account_credentials="$(cat ${GOOGLE_APPLICATION_CREDENTIALS} | base64 | tr -d '\n')"
467+
local -r service_account_credentials="$(base64 "${GOOGLE_APPLICATION_CREDENTIALS}" | tr -d '\n')"
466468
local -r cloud_provider="${KUBERNETES_PROVIDER}"
467469
local -r enable_hollow_node_logs="${ENABLE_HOLLOW_NODE_LOGS:-false}"
468470
local -r logexport_sleep_seconds="$(( 90 + NUM_NODES / 3 ))"
@@ -504,10 +506,10 @@ function dump_nodes_with_logexporter() {
504506
# Store logs from logexporter pods to allow debugging log exporting process
505507
# itself.
506508
proc=${max_dump_processes}
507-
"${KUBECTL}" get pods -n "${logexporter_namespace}" -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.nodeName}{"\n"}{end}' | while read pod node; do
509+
while read -r pod node; do
508510
echo "Fetching logs from ${pod} running on ${node}"
509-
mkdir -p ${report_dir}/${node}
510-
"${KUBECTL}" logs -n "${logexporter_namespace}" ${pod} > ${report_dir}/${node}/${pod}.log &
511+
mkdir -p "${report_dir}/${node}"
512+
"${KUBECTL}" logs -n "${logexporter_namespace}" "${pod}" > "${report_dir}/${node}/${pod}.log" &
511513

512514
# We don't want to run more than ${max_dump_processes} at a time, so
513515
# wait once we hit that many nodes. This isn't ideal, since one might
@@ -517,14 +519,13 @@ function dump_nodes_with_logexporter() {
517519
proc=${max_dump_processes}
518520
wait
519521
fi
520-
done
522+
done < <("${KUBECTL}" get pods -n "${logexporter_namespace}" -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.nodeName}{"\n"}{end}')
521523
# Wait for any remaining processes.
522524
if [[ proc -gt 0 && proc -lt ${max_dump_processes} ]]; then
523525
wait
524526
fi
525527

526528
# List registry of marker files (of nodes whose logexporter succeeded) from GCS.
527-
local nodes_succeeded
528529
for retry in {1..10}; do
529530
if find_non_logexported_nodes; then
530531
break
@@ -554,32 +555,33 @@ function dump_nodes_with_logexporter() {
554555
"${KUBECTL}" get pods --namespace "${logexporter_namespace}" || true
555556
"${KUBECTL}" delete namespace "${logexporter_namespace}" || true
556557
if [[ "${#failed_nodes[@]}" != 0 ]]; then
557-
echo -e "Dumping logs through SSH for the following nodes:\n${failed_nodes[@]}"
558+
echo -e "Dumping logs through SSH for the following nodes:\n${failed_nodes[*]}"
558559
dump_nodes "${failed_nodes[@]}"
559560
fi
560561
}
561562

562563
function detect_node_failures() {
563-
if ! [[ "${gcloud_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
564+
if ! [[ "${gcloud_supported_providers}" =~ ${KUBERNETES_PROVIDER} ]]; then
564565
return
565566
fi
566567

567568
detect-node-names
568569
if [[ "${KUBERNETES_PROVIDER}" == "gce" ]]; then
569-
local all_instance_groups=(${INSTANCE_GROUPS[@]} ${WINDOWS_INSTANCE_GROUPS[@]})
570+
local all_instance_groups=("${INSTANCE_GROUPS[@]}" "${WINDOWS_INSTANCE_GROUPS[@]}")
570571
else
571-
local all_instance_groups=(${INSTANCE_GROUPS[@]})
572+
local all_instance_groups=("${INSTANCE_GROUPS[@]}")
572573
fi
573574

574575
if [ -z "${all_instance_groups:-}" ]; then
575576
return
576577
fi
577578
for group in "${all_instance_groups[@]}"; do
578-
local creation_timestamp=$(gcloud compute instance-groups managed describe \
579-
"${group}" \
580-
--project "${PROJECT}" \
581-
--zone "${ZONE}" \
582-
--format='value(creationTimestamp)')
579+
local creation_timestamp
580+
creation_timestamp=$(gcloud compute instance-groups managed describe \
581+
"${group}" \
582+
--project "${PROJECT}" \
583+
--zone "${ZONE}" \
584+
--format='value(creationTimestamp)')
583585
echo "Failures for ${group} (if any):"
584586
gcloud logging read --order=asc \
585587
--format='table(timestamp,jsonPayload.resource.name,jsonPayload.event_subtype)' \

hack/.shellcheck_failures

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
./cluster/gce/gci/master-helper.sh
99
./cluster/gce/upgrade.sh
1010
./cluster/gce/util.sh
11-
./cluster/log-dump/log-dump.sh
1211
./cluster/pre-existing/util.sh

0 commit comments

Comments
 (0)