Skip to content

Commit 70edf5e

Browse files
committed
Proper Deletion Behavior for Logical Backup PVCs
Logical backup PVCs are now properly identified when rmdata is run. This ensures any PVCs for logical backups are properly preserved or deleted depending on the specific settings specified when deleting a cluster. For instance, if '--keep-data' only is specified, then the logical backups will now be deleted. Additionally, if '--keep-backups' is specified, then the logical backup PVCs will now be preserved. This is specifically accomplished by now matching on a prefix for the name of any logical backup PVCs (prefix 'backup-<clusterName>-pgdump'), instead of matching on the full name of the PVC (which includes the name of the database name used for the logical backup, e.g. 'backup-<clusterName>-pgdump-<database>-pvc', and therefore could vary). Issue: [ch9381]
1 parent 110bd43 commit 70edf5e

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

pgo-rmdata/rmdata/process.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333
MAX_TRIES = 16
3434
pgBackRestPathFormat = "/backrestrepo/%s"
3535
pgBackRestRepoPVC = "%s-pgbr-repo"
36-
pgDumpPVC = "backup-%s-pgdump-pvc"
36+
pgDumpPVCPrefix = "backup-%s-pgdump"
3737
pgDataPathFormat = "/pgdata/%s"
3838
tablespacePathFormat = "/tablespaces/%s/%s"
3939
// the tablespace on a replcia follows the pattern "<replicaName-tablespace-.."
@@ -477,7 +477,7 @@ func removePgtasks(request Request) {
477477
func getInstancePVCs(request Request) ([]string, error) {
478478
pvcList := make([]string, 0)
479479
selector := fmt.Sprintf("%s=%s", config.LABEL_PG_CLUSTER, request.ClusterName)
480-
pgDump, pgBackRest := fmt.Sprintf(pgDumpPVC, request.ClusterName),
480+
pgDump, pgBackRest := fmt.Sprintf(pgDumpPVCPrefix, request.ClusterName),
481481
fmt.Sprintf(pgBackRestRepoPVC, request.ClusterName)
482482

483483
log.Debugf("instance pvcs overall selector: [%s]", selector)
@@ -505,7 +505,7 @@ func getInstancePVCs(request Request) ([]string, error) {
505505

506506
log.Debugf("found pvc: [%s]", pvcName)
507507

508-
if pvcName == pgDump || pvcName == pgBackRest {
508+
if strings.HasPrefix(pvcName, pgDump) || pvcName == pgBackRest {
509509
log.Debug("skipping...")
510510
continue
511511
}
@@ -658,14 +658,35 @@ func removeBackupJobs(request Request) {
658658
// "rm -rf" like in other commands). Well, we could...we could write a job to do
659659
// this, but that will be saved for future work
660660
func removeLogicalBackupPVCs(request Request) {
661-
// get the name of the PVC, which uses a format that is fixed
662-
pvcName := fmt.Sprintf(pgDumpPVC, request.ClusterName)
663661

664-
log.Debugf("remove pgdump pvc name [%s]", pvcName)
662+
pvcList := make([]string, 0)
663+
selector := fmt.Sprintf("%s=%s", config.LABEL_PG_CLUSTER, request.ClusterName)
664+
dumpPrefix := fmt.Sprintf(pgDumpPVCPrefix, request.ClusterName)
665+
666+
// get all of the PVCs to analyze (see the step below)
667+
pvcs, err := request.Clientset.
668+
CoreV1().PersistentVolumeClaims(request.Namespace).
669+
List(metav1.ListOptions{LabelSelector: selector})
670+
if err != nil {
671+
log.Error(err)
672+
return
673+
}
674+
675+
// Now iterate through all the PVCs to identify those that are for a logical backup and add
676+
// them to the PVC list for deletion. This pattern matching will be utilized until better
677+
// labeling is in place to uniquely identify logical backup PVCs.
678+
for _, pvc := range pvcs.Items {
679+
pvcName := pvc.GetName()
680+
681+
if !strings.HasPrefix(pvcName, dumpPrefix) {
682+
continue
683+
}
684+
685+
pvcList = append(pvcList, pvcName)
686+
}
687+
688+
log.Debugf("logical backup pvcs found: [%v]", pvcList)
665689

666-
// make a simple list of the PVCs that can be applied to the "removePVC"
667-
// command
668-
pvcList := []string{pvcName}
669690
removePVCs(pvcList, request)
670691
}
671692

0 commit comments

Comments
 (0)