@@ -170,7 +170,7 @@ func Environment(cluster *v1beta1.PostgresCluster) []corev1.EnvVar {
170170// reloadCommand returns an entrypoint that convinces PostgreSQL to reload
171171// certificate files when they change. The process will appear as name in `ps`
172172// and `top`.
173- func reloadCommand (name string , post250 bool ) []string {
173+ func reloadCommand (cluster * v1beta1. PostgresCluster , name string , post250 bool , AutoGrowVolumes bool ) []string {
174174 // Use a Bash loop to periodically check the mtime of the mounted
175175 // certificate volume. When it changes, copy the replication certificate,
176176 // signal PostgreSQL, and print the observed timestamp.
@@ -193,6 +193,7 @@ func reloadCommand(name string, post250 bool) []string {
193193 // descriptor gets closed and reopened to use the builtin `[ -nt` to check
194194 // mtimes.
195195 // - https://unix.stackexchange.com/a/407383
196+
196197 script := fmt .Sprintf (`
197198declare -r directory=%q
198199exec {fd}<> <(:)
@@ -214,6 +215,27 @@ done
214215 )
215216
216217 if post250 {
218+ // Only add annotation update logic if AutoGrowVolumes is true
219+ autogrowScript := ""
220+ if AutoGrowVolumes || cluster .CompareVersion ("2.8.0" ) < 0 {
221+ autogrowScript = strings .TrimSuffix (`
222+ # Manage autogrow annotation.
223+ # Return size in Mebibytes.
224+ size=$(df --human-readable --block-size=M /pgdata | awk 'FNR == 2 {print $2}')
225+ use=$(df --human-readable /pgdata | awk 'FNR == 2 {print $5}')
226+ sizeInt="${size//M/}"
227+ # Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
228+ useInt=$(echo $use | sed 's/[[:punct:]]//g')
229+ triggerExpansion="$((useInt > 75))"
230+ if [ $triggerExpansion -eq 1 ]; then
231+ newSize="$(((sizeInt / 2)+sizeInt))"
232+ newSizeMi="${newSize}Mi"
233+ d='[{"op": "add", "path": "/metadata/annotations/suggested-pgdata-pvc-size", "value": "'"$newSizeMi"'"}]'
234+ 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"
235+ fi
236+ ` , "\n " )
237+ }
238+
217239 script = fmt .Sprintf (`
218240# Parameters for curl when managing autogrow annotation.
219241APISERVER="https://kubernetes.default.svc"
@@ -233,28 +255,15 @@ while read -r -t 5 -u "${fd}" ||:; do
233255 exec {fd}>&- && exec {fd}<> <(:||:)
234256 stat --format='Loaded certificates dated %%y' "${directory}"
235257 fi
236-
237- # Manage autogrow annotation.
238- # Return size in Mebibytes.
239- size=$(df --human-readable --block-size=M /pgdata | awk 'FNR == 2 {print $2}')
240- use=$(df --human-readable /pgdata | awk 'FNR == 2 {print $5}')
241- sizeInt="${size//M/}"
242- # Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
243- useInt=$(echo $use | sed 's/[[:punct:]]//g')
244- triggerExpansion="$((useInt > 75))"
245- if [ $triggerExpansion -eq 1 ]; then
246- newSize="$(((sizeInt / 2)+sizeInt))"
247- newSizeMi="${newSize}Mi"
248- d='[{"op": "add", "path": "/metadata/annotations/suggested-pgdata-pvc-size", "value": "'"$newSizeMi"'"}]'
249- 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"
250- fi
258+ %s
251259done
252260` ,
253261 naming .CertMountPath ,
254262 naming .ReplicationTmp ,
255263 naming .ReplicationCertPath ,
256264 naming .ReplicationPrivateKeyPath ,
257265 naming .ReplicationCACertPath ,
266+ autogrowScript , // This will be empty if AutoGrowVolumes is false
258267 )
259268 }
260269
0 commit comments