Skip to content

Commit cd2db63

Browse files
committed
refactor manage autogrow annotation script
1 parent 9b617c1 commit cd2db63

File tree

5 files changed

+48
-70
lines changed

5 files changed

+48
-70
lines changed

internal/controller/postgrescluster/autogrow.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func (r *Reconciler) setVolumeSize(ctx context.Context, cluster *v1beta1.Postgre
103103
// Store the limit for this instance set. This value will not change below.
104104
volumeLimitFromSpec := pvc.Spec.Resources.Limits.Storage()
105105

106-
// Capture the largest volume size currently defined for a given instance set.
107106
// This value will capture our desired update.
108107
volumeRequestSize := pvc.Spec.Resources.Requests.Storage()
109108

@@ -122,7 +121,7 @@ func (r *Reconciler) setVolumeSize(ctx context.Context, cluster *v1beta1.Postgre
122121
pvc.Spec.Resources.Requests = corev1.ResourceList{
123122
corev1.ResourceStorage: *resource.NewQuantity(volumeLimitFromSpec.Value(), resource.BinarySI),
124123
}
125-
// Otherwise, if the limit is not set or the feature gate is not enabled, do not autogrow.
124+
// Otherwise, if the feature gate is not enabled, do not autogrow.
126125
} else if feature.Enabled(ctx, feature.AutoGrowVolumes) {
127126

128127
// determine the appropriate volume request based on what's set in the status

internal/postgres/config.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
265264
TOKEN=$(cat ${SERVICEACCOUNT}/token)
266265
CACERT=${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+
268286
declare -r directory=%q
269287
exec {fd}<> <(:||:)
270288
while 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
}

internal/postgres/reconcile_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ containers:
175175
TOKEN=$(cat ${SERVICEACCOUNT}/token)
176176
CACERT=${SERVICEACCOUNT}/ca.crt
177177
178+
# Manage autogrow annotation.
179+
# Return size in Mebibytes.
180+
manageAutogrowAnnotation() {
181+
local volume=$1
182+
183+
size=$(df --human-readable --block-size=M /"${volume}" | awk 'FNR == 2 {print $2}')
184+
use=$(df --human-readable /"${volume}" | awk 'FNR == 2 {print $5}')
185+
sizeInt="${size//M/}"
186+
# Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
187+
useInt=$(echo $use | sed 's/[[:punct:]]//g')
188+
triggerExpansion="$((useInt > 75))"
189+
if [ $triggerExpansion -eq 1 ]; then
190+
newSize="$(((sizeInt / 2)+sizeInt))"
191+
newSizeMi="${newSize}Mi"
192+
d='[{"op": "add", "path": "/metadata/annotations/suggested-'"${volume}"'-pvc-size", "value": "'"$newSizeMi"'"}]'
193+
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"
194+
fi
195+
}
196+
178197
declare -r directory="/pgconf/tls"
179198
exec {fd}<> <(:||:)
180199
while read -r -t 5 -u "${fd}" ||:; do
@@ -187,20 +206,8 @@ containers:
187206
stat --format='Loaded certificates dated %y' "${directory}"
188207
fi
189208
190-
# Manage autogrow annotation.
191-
# Return size in Mebibytes.
192-
size=$(df --human-readable --block-size=M /pgdata | awk 'FNR == 2 {print $2}')
193-
use=$(df --human-readable /pgdata | awk 'FNR == 2 {print $5}')
194-
sizeInt="${size//M/}"
195-
# Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
196-
useInt=$(echo $use | sed 's/[[:punct:]]//g')
197-
triggerExpansion="$((useInt > 75))"
198-
if [ $triggerExpansion -eq 1 ]; then
199-
newSize="$(((sizeInt / 2)+sizeInt))"
200-
newSizeMi="${newSize}Mi"
201-
d='[{"op": "add", "path": "/metadata/annotations/suggested-pgdata-pvc-size", "value": "'"$newSizeMi"'"}]'
202-
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"
203-
fi
209+
# manage autogrow annotation for the pgData volume
210+
manageAutogrowAnnotation "pgdata"
204211
done
205212
}; export -f monitor; exec -a "$0" bash -ceu monitor
206213
- replication-cert-copy

internal/util/volumes.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,3 @@ func AddVolumeAndMountsToPod(podSpec *corev1.PodSpec, volume *corev1.PersistentV
4040
})
4141
}
4242
}
43-
44-
// autogrowAnnotationScript returns a script to add the suggested PVC size
45-
// annotation to the relevant Pod
46-
func AutogrowAnnotationScript(volType string) string {
47-
return fmt.Sprintf(`
48-
# Manage autogrow annotation.
49-
# Return size in Mebibytes.
50-
size=$(df --human-readable --block-size=M /%s | awk 'FNR == 2 {print $2}')
51-
use=$(df --human-readable /%s | awk 'FNR == 2 {print $5}')
52-
sizeInt="${size//M/}"
53-
# Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
54-
useInt=$(echo $use | sed 's/[[:punct:]]//g')
55-
triggerExpansion="$((useInt > 75))"
56-
if [ $triggerExpansion -eq 1 ]; then
57-
newSize="$(((sizeInt / 2)+sizeInt))"
58-
newSizeMi="${newSize}Mi"
59-
d='[{"op": "add", "path": "/metadata/annotations/suggested-%s-pvc-size", "value": "'"$newSizeMi"'"}]'
60-
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"
61-
fi
62-
`,
63-
volType,
64-
volType,
65-
volType,
66-
)
67-
}

internal/util/volumes_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,3 @@ func TestAddVolumeAndMountsToPod(t *testing.T) {
7676
AddVolumeAndMountsToPod(out, volume)
7777
alwaysExpect(t, out)
7878
}
79-
80-
func TestAutogrowAnnotationScript(t *testing.T) {
81-
script := AutogrowAnnotationScript("pgdata")
82-
83-
assert.Equal(t, script, `
84-
# Manage autogrow annotation.
85-
# Return size in Mebibytes.
86-
size=$(df --human-readable --block-size=M /pgdata | awk 'FNR == 2 {print $2}')
87-
use=$(df --human-readable /pgdata | awk 'FNR == 2 {print $5}')
88-
sizeInt="${size//M/}"
89-
# Use the sed punctuation class, because the shell will not accept the percent sign in an expansion.
90-
useInt=$(echo $use | sed 's/[[:punct:]]//g')
91-
triggerExpansion="$((useInt > 75))"
92-
if [ $triggerExpansion -eq 1 ]; then
93-
newSize="$(((sizeInt / 2)+sizeInt))"
94-
newSizeMi="${newSize}Mi"
95-
d='[{"op": "add", "path": "/metadata/annotations/suggested-pgdata-pvc-size", "value": "'"$newSizeMi"'"}]'
96-
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"
97-
fi
98-
`)
99-
}

0 commit comments

Comments
 (0)