Skip to content

Commit 834ed72

Browse files
committed
make script stylistically more consistent
Signed-off-by: Simon Dickhoven <sdickhoven@everquote.com>
1 parent e5cba87 commit 834ed72

File tree

1 file changed

+70
-73
lines changed

1 file changed

+70
-73
lines changed

cni-plugin/deployment/scripts/install-cni.sh

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
# - Expects the desired CNI config in the CNI_NETWORK_CONFIG env variable.
2626

2727
# Ensure all variables are defined, and that the script fails when an error is hit.
28-
set -u -e -o pipefail
28+
set -u -e -o pipefail +o noclobber
2929

3030
# Helper function for raising errors
3131
# Usage:
3232
# some_command || exit_with_error "some_command_failed: maybe try..."
3333
exit_with_error() {
34-
log "${1}"
34+
log "$1"
3535
exit 1
3636
}
3737

@@ -54,7 +54,7 @@ CONTAINER_CNI_BIN_DIR=${CONTAINER_CNI_BIN_DIR:-/opt/cni/bin}
5454
# Directory path where CNI configuration should live on the host
5555
HOST_CNI_NET="${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}"
5656
# Location of legacy "interface mode" file, to be automatically deleted
57-
DEFAULT_CNI_CONF_PATH="${HOST_CNI_NET}/01-linkerd-cni.conf"
57+
DEFAULT_CNI_CONF_PATH="$HOST_CNI_NET/01-linkerd-cni.conf"
5858
KUBECONFIG_FILE_NAME=${KUBECONFIG_FILE_NAME:-ZZZ-linkerd-cni-kubeconfig}
5959
SERVICEACCOUNT_PATH=/var/run/secrets/kubernetes.io/serviceaccount
6060

@@ -80,8 +80,8 @@ cleanup() {
8080
# Find all conflist files and print them out using a NULL separator instead of
8181
# writing each file in a new line. We will subsequently read each string and
8282
# attempt to rm linkerd config from it using jq helper.
83-
local cni_data=''
84-
find "${HOST_CNI_NET}" -maxdepth 1 -type f \( -iname '*conflist' \) -print0 |
83+
local cni_data
84+
find "$HOST_CNI_NET" -maxdepth 1 -type f \( -iname '*conflist' \) -print0 |
8585
while read -r -d $'\0' file; do
8686
log "Removing linkerd-cni config from $file"
8787
cni_data=$(jq 'del( .plugins[]? | select( .type == "linkerd-cni" ))' "$file")
@@ -91,11 +91,11 @@ cleanup() {
9191
done
9292

9393
# Remove binary and kubeconfig file
94-
if [ -e "${HOST_CNI_NET}/${KUBECONFIG_FILE_NAME}" ]; then
95-
log "Removing linkerd-cni kubeconfig: ${HOST_CNI_NET}/${KUBECONFIG_FILE_NAME}"
96-
rm -f "${HOST_CNI_NET}/${KUBECONFIG_FILE_NAME}"
94+
if [ -e "$HOST_CNI_NET/$KUBECONFIG_FILE_NAME" ]; then
95+
log "Removing linkerd-cni kubeconfig: $HOST_CNI_NET/$KUBECONFIG_FILE_NAME"
96+
rm -f "$HOST_CNI_NET/$KUBECONFIG_FILE_NAME"
9797
fi
98-
if [ -e "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_BIN_DIR}"/linkerd-cni ]; then
98+
if [ -e "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_BIN_DIR}/linkerd-cni" ]; then
9999
log "Removing linkerd-cni binary: ${CONTAINER_MOUNT_PREFIX}${DEST_CNI_BIN_DIR}/linkerd-cni"
100100
rm -f "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_BIN_DIR}/linkerd-cni"
101101
fi
@@ -113,54 +113,54 @@ trap 'log "ERROR caught, exiting..."; cleanup ' ERR
113113
install_cni_bin() {
114114
# Place the new binaries if the mounted directory is writeable.
115115
dir="${CONTAINER_MOUNT_PREFIX}${DEST_CNI_BIN_DIR}"
116-
if [ ! -w "${dir}" ]; then
117-
exit_with_error "${dir} is non-writeable, failure"
116+
if [ ! -w "$dir" ]; then
117+
exit_with_error "$dir is non-writeable, failure"
118118
fi
119-
for path in "${CONTAINER_CNI_BIN_DIR}"/*; do
120-
cp "${path}" "${dir}"/ || exit_with_error "Failed to copy ${path} to ${dir}."
119+
for path in "$CONTAINER_CNI_BIN_DIR"/*; do
120+
cp "$path" "$dir/" || exit_with_error "Failed to copy $path to $dir."
121121
done
122122

123-
log "Wrote linkerd CNI binaries to ${dir}"
123+
log "Wrote linkerd CNI binaries to $dir"
124124
}
125125

126126
create_kubeconfig() {
127-
KUBE_CA_FILE=${KUBE_CA_FILE:-${SERVICEACCOUNT_PATH}/ca.crt}
127+
KUBE_CA_FILE=${KUBE_CA_FILE:-$SERVICEACCOUNT_PATH/ca.crt}
128128
SKIP_TLS_VERIFY=${SKIP_TLS_VERIFY:-false}
129-
SERVICEACCOUNT_TOKEN=$(cat ${SERVICEACCOUNT_PATH}/token)
129+
SERVICEACCOUNT_TOKEN=$(cat "$SERVICEACCOUNT_PATH/token")
130130

131131
# Check if we're not running as a k8s pod.
132-
if [[ ! -f "${SERVICEACCOUNT_PATH}/token" ]]; then
132+
if [[ ! -f "$SERVICEACCOUNT_PATH/token" ]]; then
133133
return
134134
fi
135135

136-
if [ -z "${KUBERNETES_SERVICE_HOST}" ]; then
136+
if [ -z "$KUBERNETES_SERVICE_HOST" ]; then
137137
log 'KUBERNETES_SERVICE_HOST not set'; exit 1;
138138
fi
139-
if [ -z "${KUBERNETES_SERVICE_PORT}" ]; then
139+
if [ -z "$KUBERNETES_SERVICE_PORT" ]; then
140140
log 'KUBERNETES_SERVICE_PORT not set'; exit 1;
141141
fi
142142

143-
if [ "${SKIP_TLS_VERIFY}" = 'true' ]; then
143+
if [ "$SKIP_TLS_VERIFY" = 'true' ]; then
144144
TLS_CFG='insecure-skip-tls-verify: true'
145-
elif [ -f "${KUBE_CA_FILE}" ]; then
146-
TLS_CFG="certificate-authority-data: $(base64 "${KUBE_CA_FILE}" | tr -d '\n')"
145+
elif [ -f "$KUBE_CA_FILE" ]; then
146+
TLS_CFG="certificate-authority-data: $(base64 "$KUBE_CA_FILE" | tr -d '\n')"
147147
fi
148148

149-
touch "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}/${KUBECONFIG_FILE_NAME}"
150-
chmod "${KUBECONFIG_MODE:-600}" "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}/${KUBECONFIG_FILE_NAME}"
151-
cat > "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}/${KUBECONFIG_FILE_NAME}" <<EOF
149+
touch "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}/$KUBECONFIG_FILE_NAME"
150+
chmod "${KUBECONFIG_MODE:-600}" "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}/$KUBECONFIG_FILE_NAME"
151+
cat > "${CONTAINER_MOUNT_PREFIX}${DEST_CNI_NET_DIR}/$KUBECONFIG_FILE_NAME" <<EOF
152152
# Kubeconfig file for linkerd CNI plugin.
153153
apiVersion: v1
154154
kind: Config
155155
clusters:
156156
- name: local
157157
cluster:
158-
server: ${KUBERNETES_SERVICE_PROTOCOL:-https}://[${KUBERNETES_SERVICE_HOST}]:${KUBERNETES_SERVICE_PORT}
159-
${TLS_CFG}
158+
server: ${KUBERNETES_SERVICE_PROTOCOL:-https}://[$KUBERNETES_SERVICE_HOST]:$KUBERNETES_SERVICE_PORT
159+
$TLS_CFG
160160
users:
161161
- name: linkerd-cni
162162
user:
163-
token: ${SERVICEACCOUNT_TOKEN}
163+
token: $SERVICEACCOUNT_TOKEN
164164
contexts:
165165
- name: linkerd-cni-context
166166
context:
@@ -179,20 +179,20 @@ create_cni_conf() {
179179
CNI_NETWORK_CONFIG="${CNI_NETWORK_CONFIG:-}"
180180

181181
# If the CNI Network Config has been overwritten, then use template from file
182-
if [ -e "${CNI_NETWORK_CONFIG_FILE}" ]; then
183-
log "Using CNI config template from ${CNI_NETWORK_CONFIG_FILE}."
184-
cp "${CNI_NETWORK_CONFIG_FILE}" "${TMP_CONF}"
185-
elif [ "${CNI_NETWORK_CONFIG}" ]; then
182+
if [ -e "$CNI_NETWORK_CONFIG_FILE" ]; then
183+
log "Using CNI config template from $CNI_NETWORK_CONFIG_FILE."
184+
cp "$CNI_NETWORK_CONFIG_FILE" "$TMP_CONF"
185+
elif [ "$CNI_NETWORK_CONFIG" ]; then
186186
log 'Using CNI config template from CNI_NETWORK_CONFIG environment variable.'
187-
cat >"${TMP_CONF}" <<EOF
188-
${CNI_NETWORK_CONFIG}
187+
cat <<EOF > "$TMP_CONF"
188+
$CNI_NETWORK_CONFIG
189189
EOF
190190
fi
191191

192192
# Use alternative command character "~", since these include a "/".
193-
sed -i s~__KUBECONFIG_FILEPATH__~"${DEST_CNI_NET_DIR}/${KUBECONFIG_FILE_NAME}"~g ${TMP_CONF}
193+
sed -i s~__KUBECONFIG_FILEPATH__~"$DEST_CNI_NET_DIR/$KUBECONFIG_FILE_NAME"~g "$TMP_CONF"
194194

195-
log "CNI config: $(cat ${TMP_CONF})"
195+
log "CNI config: $(cat "$TMP_CONF")"
196196
}
197197

198198
install_cni_conf() {
@@ -209,15 +209,16 @@ install_cni_conf() {
209209

210210
echo "$conf_data" > "$TMP_CONF"
211211

212-
# If the old config filename ends with .conf, rename it to .conflist, because it has changed to be a list
213-
filename=${cni_conf_path##*/}
214-
extension=${filename##*.}
212+
# If the old config filename ends with .conf, rename it to .conflist because
213+
# it has changed to be a list.
214+
local filename=${cni_conf_path##*/}
215+
local extension=${filename##*.}
215216
# When this variable has a file, we must delete it later.
216217
old_file_path=
217-
if [ "${filename}" != '01-linkerd-cni.conf' ] && [ "${extension}" = 'conf' ]; then
218-
old_file_path=${cni_conf_path}
219-
log "Renaming ${cni_conf_path} extension to .conflist"
220-
cni_conf_path="${cni_conf_path}list"
218+
if [ "$filename" != '01-linkerd-cni.conf' ] && [ "$extension" = 'conf' ]; then
219+
old_file_path=$cni_conf_path
220+
log "Renaming $cni_conf_path extension to .conflist"
221+
cni_conf_path=${cni_conf_path}list
221222
fi
222223

223224
# Store SHA of each patched file in global `CNI_CONF_SHA` variable.
@@ -238,18 +239,19 @@ install_cni_conf() {
238239
CNI_CONF_SHA=$(jq -c --arg f "$cni_conf_path" --arg sha "$new_sha" '. * {$f: $sha}' <<< "$CNI_CONF_SHA")
239240

240241
# Move the temporary CNI config into place.
241-
mv "${TMP_CONF}" "${cni_conf_path}" || exit_with_error 'Failed to mv files.'
242-
[ -n "$old_file_path" ] && rm -f "${old_file_path}" && log "Removing unwanted .conf file"
242+
mv "$TMP_CONF" "$cni_conf_path" || exit_with_error 'Failed to mv files.'
243+
[ -n "$old_file_path" ] && rm -f "$old_file_path" && log "Removing unwanted .conf file"
243244

244-
log "Created CNI config ${cni_conf_path}"
245+
log "Created CNI config $cni_conf_path"
245246
}
246247

247-
# Sync() is responsible for reacting to file system changes. It is used in
248-
# conjunction with inotify events; sync() is called with the event type (which
249-
# can be either 'CREATE', 'MOVED_TO' or 'MODIFY'), and the name of the file that
248+
# `sync()` is responsible for reacting to file system changes. It is used in
249+
# conjunction with inotify events; `sync()` is called with the event type (which
250+
# can be either 'CREATE', 'MOVED_TO', or 'MODIFY') and the name of the file that
250251
# has changed.
251252
#
252-
# Based on the changed file, sync() might re-install the CNI configuration file.
253+
# Based on the changed file, `sync()` might re-install the CNI configuration
254+
# file.
253255
sync() {
254256
local ev=$1
255257
local file=${2//\/\//\/} # replace "//" with "/"
@@ -286,7 +288,7 @@ sync() {
286288

287289
# monitor_cni_config starts a watch on the host's CNI config directory
288290
monitor_cni_config() {
289-
inotifywait -m "${HOST_CNI_NET}" -e create,moved_to,modify |
291+
inotifywait -m "$HOST_CNI_NET" -e create,moved_to,modify |
290292
while read -r directory action filename; do
291293
sync "$action" "$directory/$filename"
292294
done
@@ -307,13 +309,13 @@ monitor_cni_config() {
307309
# > 9. The new data directory symlink is renamed to the data directory; rename is atomic.
308310
# See https://github.com/kubernetes/kubernetes/blob/release-1.32/pkg/volume/util/atomic_writer.go
309311
monitor_service_account_token() {
310-
inotifywait -m "${SERVICEACCOUNT_PATH}" -e moved_to |
311-
while read -r _ _ filename; do
312-
if [[ "$filename" == "..data" ]]; then
312+
inotifywait -m "$SERVICEACCOUNT_PATH" -e moved_to |
313+
while read -r _ _ filename; do
314+
if [[ "$filename" == "..data" ]]; then
313315
log "Detected change in service account files; recreating kubeconfig file"
314316
create_kubeconfig
315-
fi
316-
done
317+
fi
318+
done
317319
}
318320

319321
log() {
@@ -326,7 +328,7 @@ log() {
326328

327329
# Delete old "interface mode" file, possibly left over from previous versions
328330
# TODO(alpeb): remove this on stable-2.15
329-
rm -f "${DEFAULT_CNI_CONF_PATH}"
331+
rm -f "$DEFAULT_CNI_CONF_PATH"
330332

331333
install_cni_bin
332334

@@ -339,24 +341,19 @@ CNI_CONF_SHA='{}'
339341
monitor_cni_config &
340342

341343
# Append our config to any existing config file (*.conflist or *.conf)
342-
config_files=$(find "${HOST_CNI_NET}" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \))
344+
config_files=$(find "$HOST_CNI_NET" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \) | grep -v linkerd || true)
343345
if [ -z "$config_files" ]; then
344-
log "No active CNI configuration files found"
346+
log "No active CNI configuration files found"
345347
else
346-
config_file_count=$(echo "$config_files" | grep -v linkerd | sort | wc -l)
347-
if [ "$config_file_count" -eq 0 ]; then
348-
log "No active CNI configuration files found"
349-
else
350-
find "${HOST_CNI_NET}" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \) -print0 |
351-
while read -r -d $'\0' file; do
352-
log "Trigger CNI config detection for $file"
353-
tmp_file="$(mktemp -u /tmp/linkerd-cni.patch-candidate.XXXXXX)"
354-
cp -fp "$file" "$tmp_file"
355-
# The following will trigger the `sync()` function via filesystem event.
356-
# This requires `monitor_cni_config()` to be up and running!
357-
mv "$tmp_file" "$file" || exit_with_error 'Failed to mv files.'
358-
done
359-
fi
348+
find "$HOST_CNI_NET" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \) -print0 |
349+
while read -r -d $'\0' file; do
350+
log "Trigger CNI config detection for $file"
351+
tmp_file="$(mktemp -u /tmp/linkerd-cni.patch-candidate.XXXXXX)"
352+
cp -fp "$file" "$tmp_file"
353+
# The following will trigger the `sync()` function via filesystem event.
354+
# This requires `monitor_cni_config()` to be up and running!
355+
mv "$tmp_file" "$file" || exit_with_error 'Failed to mv files.'
356+
done
360357
fi
361358

362359
# Watch in bg so we can receive interrupt signals through 'trap'. From 'man

0 commit comments

Comments
 (0)