@@ -17,7 +17,6 @@ import (
1717 "github.com/crunchydata/postgres-operator/internal/feature"
1818 "github.com/crunchydata/postgres-operator/internal/naming"
1919 "github.com/crunchydata/postgres-operator/internal/shell"
20- "github.com/crunchydata/postgres-operator/internal/util"
2120 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2221)
2322
@@ -265,6 +264,25 @@ NAMESPACE=$(cat ${SERVICEACCOUNT}/namespace)
265264TOKEN=$(cat ${SERVICEACCOUNT}/token)
266265CACERT=${SERVICEACCOUNT}/ca.crt
267266
267+ # Manage autogrow annotation.
268+ # Return size in Mebibytes.
269+ manageAutogrowAnnotation() {
270+ local volume=$1
271+
272+ size=$(df --human-readable --block-size=M /"${volume}" | awk 'FNR == 2 {print $2}')
273+ use=$(df --human-readable /"${volume}" | awk 'FNR == 2 {print $5}')
274+ sizeInt="${size//M/}"
275+ # Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
276+ useInt=$(echo $use | sed 's/[[:punct:]]//g')
277+ triggerExpansion="$((useInt > 75))"
278+ if [ $triggerExpansion -eq 1 ]; then
279+ newSize="$(((sizeInt / 2)+sizeInt))"
280+ newSizeMi="${newSize}Mi"
281+ d='[{"op": "add", "path": "/metadata/annotations/suggested-'"${volume}"'-pvc-size", "value": "'"$newSizeMi"'"}]'
282+ 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"
283+ fi
284+ }
285+
268286declare -r directory=%q
269287exec {fd}<> <(:||:)
270288while read -r -t 5 -u "${fd}" ||:; do
@@ -276,6 +294,10 @@ while read -r -t 5 -u "${fd}" ||:; do
276294 exec {fd}>&- && exec {fd}<> <(:||:)
277295 stat --format='Loaded certificates dated %%y' "${directory}"
278296 fi
297+
298+ # manage autogrow annotation for the pgData volume
299+ manageAutogrowAnnotation "pgdata"
300+ done
279301` ,
280302 naming .CertMountPath ,
281303 naming .ReplicationTmp ,
@@ -286,16 +308,12 @@ while read -r -t 5 -u "${fd}" ||:; do
286308
287309 // this is used to close out the while loop started above after adding the required
288310 // auto grow annotation scripts
289- finalDone := `done
290- `
311+ // finalDone := `done
312+ // `
291313
292314 // Elide the above script from `ps` and `top` by wrapping it in a function
293315 // and calling that.
294- wrapper := `monitor() {` +
295- script + // main script body
296- util .AutogrowAnnotationScript ("pgdata" ) + // pgdata annotation script
297- finalDone + // close out the while loop
298- `}; export -f monitor; exec -a "$0" bash -ceu monitor`
316+ wrapper := `monitor() {` + script + `}; export -f monitor; exec -a "$0" bash -ceu monitor`
299317
300318 return []string {"bash" , "-ceu" , "--" , wrapper , name }
301319}
0 commit comments