Skip to content

Commit bbdd0e4

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 9d1afe4 commit bbdd0e4

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

pgo-rmdata/rmdata/process.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
log "github.com/sirupsen/logrus"
2929
kerror "k8s.io/apimachinery/pkg/api/errors"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031

3132
"time"
3233
)
@@ -35,7 +36,7 @@ const (
3536
MAX_TRIES = 16
3637
pgBackRestPathFormat = "/backrestrepo/%s"
3738
pgBackRestRepoPVC = "%s-pgbr-repo"
38-
pgDumpPVC = "backup-%s-pgdump-pvc"
39+
pgDumpPVCPrefix = "backup-%s-pgdump"
3940
pgDataPathFormat = "/pgdata/%s"
4041
tablespacePathFormat = "/tablespaces/%s/%s"
4142
// the tablespace on a replcia follows the pattern "<replicaName-tablespace-.."
@@ -459,7 +460,7 @@ func removePgtasks(request Request) {
459460
func getInstancePVCs(request Request) ([]string, error) {
460461
pvcList := make([]string, 0)
461462
selector := fmt.Sprintf("%s=%s", config.LABEL_PG_CLUSTER, request.ClusterName)
462-
pgDump, pgBackRest := fmt.Sprintf(pgDumpPVC, request.ClusterName),
463+
pgDump, pgBackRest := fmt.Sprintf(pgDumpPVCPrefix, request.ClusterName),
463464
fmt.Sprintf(pgBackRestRepoPVC, request.ClusterName)
464465

465466
log.Debugf("instance pvcs overall selector: [%s]", selector)
@@ -485,7 +486,7 @@ func getInstancePVCs(request Request) ([]string, error) {
485486

486487
log.Debugf("found pvc: [%s]", pvcName)
487488

488-
if pvcName == pgDump || pvcName == pgBackRest {
489+
if strings.HasPrefix(pvcName, pgDump) || pvcName == pgBackRest {
489490
log.Debug("skipping...")
490491
continue
491492
}
@@ -627,14 +628,35 @@ func removeBackupJobs(request Request) {
627628
// "rm -rf" like in other commands). Well, we could...we could write a job to do
628629
// this, but that will be saved for future work
629630
func removeLogicalBackupPVCs(request Request) {
630-
// get the name of the PVC, which uses a format that is fixed
631-
pvcName := fmt.Sprintf(pgDumpPVC, request.ClusterName)
632631

633-
log.Debugf("remove pgdump pvc name [%s]", pvcName)
632+
pvcList := make([]string, 0)
633+
selector := fmt.Sprintf("%s=%s", config.LABEL_PG_CLUSTER, request.ClusterName)
634+
dumpPrefix := fmt.Sprintf(pgDumpPVCPrefix, request.ClusterName)
635+
636+
// get all of the PVCs to analyze (see the step below)
637+
pvcs, err := request.Clientset.
638+
CoreV1().PersistentVolumeClaims(request.Namespace).
639+
List(metav1.ListOptions{LabelSelector: selector})
640+
if err != nil {
641+
log.Error(err)
642+
return
643+
}
644+
645+
// Now iterate through all the PVCs to identify those that are for a logical backup and add
646+
// them to the PVC list for deletion. This pattern matching will be utilized until better
647+
// labeling is in place to uniquely identify logical backup PVCs.
648+
for _, pvc := range pvcs.Items {
649+
pvcName := pvc.GetName()
650+
651+
if !strings.HasPrefix(pvcName, dumpPrefix) {
652+
continue
653+
}
654+
655+
pvcList = append(pvcList, pvcName)
656+
}
657+
658+
log.Debugf("logical backup pvcs found: [%v]", pvcList)
634659

635-
// make a simple list of the PVCs that can be applied to the "removePVC"
636-
// command
637-
pvcList := []string{pvcName}
638660
removePVCs(pvcList, request)
639661
}
640662

0 commit comments

Comments
 (0)