@@ -298,13 +298,24 @@ func reloadCommand(
298298 // descriptor gets closed and reopened to use the builtin `[ -nt` to check
299299 // mtimes.
300300 // - https://unix.stackexchange.com/a/407383
301+ //
302+ // In the manageAutogrowAnnotation function below, df is used to return the
303+ // relevant volume size in Mebibytes. The 'read' variable gets the value from
304+ // the '1M-blocks' output (second column) and the 'use' variable gets the value
305+ // from the 'Use%' column (fifth column). This value is grabbed after stripping
306+ // out the column headers (before the '\n') and then getting the respective
307+ // value delimited by the white spaces by using the 'read -r' command.
308+ // The underscores (_) discard fields and the variables store them. This allows
309+ // for selective parsing of the provided lines. The percent value is stripped of
310+ // the '%' and then used to determine if a expansion should be triggered by
311+ // setting the calculated volume size using the 'size' variable.
301312 script := fmt .Sprintf (`
302313# Parameters for curl when managing autogrow annotation.
303314APISERVER="https://kubernetes.default.svc"
304315SERVICEACCOUNT="/var/run/secrets/kubernetes.io/serviceaccount"
305- NAMESPACE=$(cat ${SERVICEACCOUNT}/namespace)
306- TOKEN=$(cat ${SERVICEACCOUNT}/token)
307- CACERT=${SERVICEACCOUNT}/ca.crt
316+ NAMESPACE=$(cat " ${SERVICEACCOUNT}/namespace" )
317+ TOKEN=$(cat " ${SERVICEACCOUNT}/token" )
318+ CACERT=" ${SERVICEACCOUNT}/ca.crt"
308319
309320# Manage autogrow annotation.
310321# Return size in Mebibytes.
@@ -313,27 +324,29 @@ manageAutogrowAnnotation() {
313324 local trigger=$2
314325 local maxGrow=$3
315326
316- size=$(df --block-size=M /"${volume}" | awk 'FNR == 2 {print $2}')
317- use=$(df /"${volume}" | awk 'FNR == 2 {print $5}')
327+ size=$(df --block-size=M /"${volume}")
328+ read -r _ size _ <<< "${size#*$'\n'}"
329+ use=$(df /"${volume}")
330+ read -r _ _ _ _ use _ <<< "${use#*$'\n'}"
318331 sizeInt="${size//M/}"
319332 # Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
320- useInt=$(echo $ use | sed 's/ [[:punct:]]//g')
333+ useInt=${ use// [[:punct:]]/}
321334 triggerExpansion="$((useInt > trigger))"
322- if [[ $triggerExpansion -eq 1 ]]; then
335+ if [[ ${ triggerExpansion} -eq 1 ]]; then
323336 newSize="$(((sizeInt / 2)+sizeInt))"
324337 # Only compare with maxGrow if it is set (not empty)
325- if [[ -n "$maxGrow" ]]; then
338+ if [[ -n "${ maxGrow} " ]]; then
326339 # check to see how much we would normally grow
327340 sizeDiff=$((newSize - sizeInt))
328341
329342 # Compare the size difference to the maxGrow; if it is greater, cap it to maxGrow
330- if [[ $sizeDiff -gt $maxGrow ]]; then
343+ if [[ ${ sizeDiff} -gt ${ maxGrow} ]]; then
331344 newSize=$((sizeInt + maxGrow))
332345 fi
333346 fi
334347 newSizeMi="${newSize}Mi"
335- d='[{"op": "add", "path": "/metadata/annotations/suggested-'"${volume}"'-pvc-size", "value": "'"$newSizeMi"'"}]'
336- curl --cacert ${CACERT} --header "Authorization: Bearer ${TOKEN}" -XPATCH "${APISERVER}/api/v1/namespaces/${NAMESPACE}/pods/${HOSTNAME}?fieldManager=kubectl-annotate" -H "Content-Type: application/json-patch+json" --data "$d "
348+ d='[{"op": "add", "path": "/metadata/annotations/suggested-'"${volume}"'-pvc-size", "value": "'"${ newSizeMi} "'"}]'
349+ curl --cacert " ${CACERT}" --header "Authorization: Bearer ${TOKEN}" -XPATCH "${APISERVER}/api/v1/namespaces/${NAMESPACE}/pods/${HOSTNAME}?fieldManager=kubectl-annotate" -H "Content-Type: application/json-patch+json" --data "${d} "
337350 fi
338351}
339352
0 commit comments