Skip to content

Commit 3c39d19

Browse files
authored
fix: use correct size to update WAL PVC (cloudnative-pg#1205)
Previously we used the main PVC size to update the one dedicated to WAL storage. This led to "-wal" PVCs being created with the correct size at first, but then, in case the main PVC had a different size, to be resized to the main PVC's size. Closes: cloudnative-pg#1190 Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
1 parent 2aaa31c commit 3c39d19

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

api/v1/cluster_types.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ const (
8282
// get the name of the pull secret
8383
ClusterSecretSuffix = "-pull-secret"
8484

85+
// WalArchiveVolumeSuffix is the suffix appended to the instance name to
86+
// get the name of the PVC dedicated to WAL files.
87+
WalArchiveVolumeSuffix = "-wal"
88+
8589
// StreamingReplicationUser is the name of the user we'll use for
8690
// streaming replication purposes
8791
StreamingReplicationUser = "streaming_replica"
@@ -1977,7 +1981,7 @@ func (cluster *Cluster) ShouldCreateWalArchiveVolume() bool {
19771981

19781982
// GetWalArchiveVolumeSuffix gets the wal archive volume name suffix
19791983
func (cluster *Cluster) GetWalArchiveVolumeSuffix() string {
1980-
return "-wal"
1984+
return WalArchiveVolumeSuffix
19811985
}
19821986

19831987
// GetPostgresUID returns the UID that is being used for the "postgres"

controllers/cluster_controller.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/http"
2626
"reflect"
2727
goruntime "runtime"
28+
"strings"
2829
"time"
2930

3031
batchv1 "k8s.io/api/batch/v1"
@@ -600,22 +601,36 @@ func (r *ClusterReconciler) ReconcilePVCs(ctx context.Context, cluster *apiv1.Cl
600601
return fmt.Errorf("while parsing PVC size %v: %w", cluster.Spec.StorageConfiguration.Size, err)
601602
}
602603

604+
var walQuantity *resource.Quantity
605+
606+
if cluster.Spec.WalStorage != nil {
607+
q, err := resource.ParseQuantity(cluster.Spec.WalStorage.Size)
608+
if err != nil {
609+
return fmt.Errorf("while parsing WAL PVC size %v: %w", cluster.Spec.WalStorage.Size, err)
610+
}
611+
walQuantity = &q
612+
}
613+
603614
for idx := range resources.pvcs.Items {
604615
oldPVC := resources.pvcs.Items[idx].DeepCopy()
616+
q := quantity
617+
if strings.HasSuffix(oldPVC.Name, apiv1.WalArchiveVolumeSuffix) && walQuantity != nil {
618+
q = *walQuantity
619+
}
605620
oldQuantity, ok := resources.pvcs.Items[idx].Spec.Resources.Requests["storage"]
606621

607622
switch {
608623
case !ok:
609624
// Missing storage requirement for PVC
610625
fallthrough
611626

612-
case oldQuantity.AsDec().Cmp(quantity.AsDec()) == -1:
627+
case oldQuantity.AsDec().Cmp(q.AsDec()) == -1:
613628
// Increasing storage resources
614-
resources.pvcs.Items[idx].Spec.Resources.Requests["storage"] = quantity
629+
resources.pvcs.Items[idx].Spec.Resources.Requests["storage"] = q
615630
if err = r.Patch(ctx, &resources.pvcs.Items[idx], client.MergeFrom(oldPVC)); err != nil {
616631
// Decreasing resources is not possible
617632
contextLogger.Error(err, "error while changing PVC storage requirement",
618-
"from", oldQuantity, "to", quantity,
633+
"from", oldQuantity, "to", q,
619634
"pvcName", resources.pvcs.Items[idx].Name)
620635

621636
// We are reaching two errors in two different conditions:
@@ -625,10 +640,10 @@ func (r *ClusterReconciler) ReconcilePVCs(ctx context.Context, cluster *apiv1.Cl
625640
// about it
626641
}
627642

628-
case oldQuantity.AsDec().Cmp(quantity.AsDec()) == 1:
643+
case oldQuantity.AsDec().Cmp(q.AsDec()) == 1:
629644
// Decreasing resources is not possible
630645
contextLogger.Info("cannot decrease storage requirement",
631-
"from", oldQuantity, "to", quantity,
646+
"from", oldQuantity, "to", q,
632647
"pvcName", resources.pvcs.Items[idx].Name)
633648
}
634649
}

0 commit comments

Comments
 (0)