Skip to content

Commit 66cbbee

Browse files
armruNiccoloFeijsilvela
authored andcommitted
feat: change naming convention for backup VolumeSnapshots (cloudnative-pg#2885)
Aligns the backup names created by the plugin's backup command and by scheduled backups for clarity. Closes cloudnative-pg#2883 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Jaime Silvela <[email protected]> Co-authored-by: Niccolò Fei <[email protected]> Co-authored-by: Jaime Silvela <[email protected]> Signed-off-by: Lennart Krauch <[email protected]>
1 parent 8bc0519 commit 66cbbee

2 files changed

Lines changed: 65 additions & 23 deletions

File tree

‎pkg/reconciler/backup/volumesnapshot/reconciler.go‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,11 @@ func (se *Reconciler) createSnapshotPVCGroupStep(
282282
backup *apiv1.Backup,
283283
targetPod *corev1.Pod,
284284
) error {
285-
snapshotSuffix := fmt.Sprintf("%d", time.Now().Unix())
286-
287285
for i := range pvcs {
288286
se.recorder.Eventf(backup, "Normal", "CreateSnapshot",
289287
"Creating VolumeSnapshot for PVC %v", pvcs[i].Name)
290288

291-
err := se.createSnapshot(ctx, cluster, backup, targetPod, &pvcs[i], snapshotSuffix)
289+
err := se.createSnapshot(ctx, cluster, backup, targetPod, &pvcs[i])
292290
if err != nil {
293291
return err
294292
}
@@ -319,12 +317,15 @@ func (se *Reconciler) createSnapshot(
319317
backup *apiv1.Backup,
320318
targetPod *corev1.Pod,
321319
pvc *corev1.PersistentVolumeClaim,
322-
snapshotSuffix string,
323320
) error {
321+
role := utils.PVCRole(pvc.Labels[utils.PvcRoleLabelName])
322+
name, err := getSnapshotName(backup.Name, role)
323+
if err != nil {
324+
return err
325+
}
326+
324327
snapshotConfig := *cluster.Spec.Backup.VolumeSnapshot
325-
name := se.getSnapshotName(pvc.Name, snapshotSuffix)
326328
var snapshotClassName *string
327-
role := utils.PVCRole(pvc.Labels[utils.PvcRoleLabelName])
328329
if role == utils.PVCRolePgWal && snapshotConfig.WalClassName != "" {
329330
snapshotClassName = &snapshotConfig.WalClassName
330331
}
@@ -364,8 +365,7 @@ func (se *Reconciler) createSnapshot(
364365
return err
365366
}
366367

367-
err := se.cli.Create(ctx, &snapshot)
368-
if err != nil {
368+
if err := se.cli.Create(ctx, &snapshot); err != nil {
369369
return fmt.Errorf("while creating VolumeSnapshot %s: %w", snapshot.Name, err)
370370
}
371371

@@ -394,6 +394,13 @@ func (se *Reconciler) waitSnapshot(
394394
}
395395

396396
// getSnapshotName gets the snapshot name for a certain PVC
397-
func (se *Reconciler) getSnapshotName(pvcName string, snapshotSuffix string) string {
398-
return fmt.Sprintf("%s-%s", pvcName, snapshotSuffix)
397+
func getSnapshotName(backupName string, role utils.PVCRole) (string, error) {
398+
switch role {
399+
case utils.PVCRolePgData, "":
400+
return backupName, nil
401+
case utils.PVCRolePgWal:
402+
return fmt.Sprintf("%s-wal", backupName), nil
403+
default:
404+
return "", fmt.Errorf("unhandled PVCRole type: %s", role)
405+
}
399406
}

‎pkg/reconciler/backup/volumesnapshot/reconciler_test.go‎

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package volumesnapshot
1818

1919
import (
2020
"fmt"
21+
"strconv"
2122

2223
storagesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2324
v1 "k8s.io/api/core/v1"
@@ -35,17 +36,44 @@ import (
3536
. "github.com/onsi/gomega"
3637
)
3738

39+
var _ = Describe("getSnapshotName", func() {
40+
It("should return only the backup name when the role is PVCRolePgData", func() {
41+
name, err := getSnapshotName("backup123", utils.PVCRolePgData)
42+
Expect(err).NotTo(HaveOccurred())
43+
Expect(name).To(Equal("backup123"))
44+
})
45+
46+
It("should return only the backup name when the role is an empty string", func() {
47+
name, err := getSnapshotName("backup123", "")
48+
Expect(err).NotTo(HaveOccurred())
49+
Expect(name).To(Equal("backup123"))
50+
})
51+
52+
It("should append '-wal' to the backup name when the role is PVCRolePgWal", func() {
53+
name, err := getSnapshotName("backup123", utils.PVCRolePgWal)
54+
Expect(err).NotTo(HaveOccurred())
55+
Expect(name).To(Equal("backup123-wal"))
56+
})
57+
58+
It("should return an error for unhandled PVCRole types", func() {
59+
_, err := getSnapshotName("backup123", "UNKNOWN_ROLE")
60+
Expect(err).To(HaveOccurred())
61+
Expect(err.Error()).To(Equal("unhandled PVCRole type: UNKNOWN_ROLE"))
62+
})
63+
})
64+
3865
var _ = Describe("Volumesnapshot reconciler", func() {
3966
const (
4067
namespace = "test-namespace"
4168
clusterName = "clusterName"
4269
backupName = "theBakcup"
4370
)
4471
var (
45-
cluster *apiv1.Cluster
46-
targetPod *v1.Pod
47-
pvcs []v1.PersistentVolumeClaim
48-
backup *apiv1.Backup
72+
cluster *apiv1.Cluster
73+
targetPod *v1.Pod
74+
pvcs []v1.PersistentVolumeClaim
75+
backup *apiv1.Backup
76+
backupCounter = 1
4977
)
5078

5179
BeforeEach(func() {
@@ -75,21 +103,28 @@ var _ = Describe("Volumesnapshot reconciler", func() {
75103
ObjectMeta: metav1.ObjectMeta{
76104
Name: clusterName + "-2",
77105
Namespace: namespace,
106+
Labels: map[string]string{
107+
utils.PvcRoleLabelName: string(utils.PVCRolePgData),
108+
},
78109
},
79110
},
80111
{
81112
ObjectMeta: metav1.ObjectMeta{
82113
Name: clusterName + "-2-wal",
83114
Namespace: namespace,
115+
Labels: map[string]string{
116+
utils.PvcRoleLabelName: string(utils.PVCRolePgWal),
117+
},
84118
},
85119
},
86120
}
87121
backup = &apiv1.Backup{
88122
ObjectMeta: metav1.ObjectMeta{
89123
Namespace: namespace,
90-
Name: backupName,
124+
Name: backupName + strconv.Itoa(backupCounter),
91125
},
92126
}
127+
backupCounter++
93128
})
94129

95130
It("should fence the target pod when there are no volumesnapshots", func(ctx SpecContext) {
@@ -128,18 +163,18 @@ var _ = Describe("Volumesnapshot reconciler", func() {
128163
{
129164
ObjectMeta: metav1.ObjectMeta{
130165
Namespace: namespace,
131-
Name: clusterName + "-2",
166+
Name: backup.Name,
132167
Labels: map[string]string{
133-
utils.BackupNameLabelName: backupName,
168+
utils.BackupNameLabelName: backup.Name,
134169
},
135170
},
136171
},
137172
{
138173
ObjectMeta: metav1.ObjectMeta{
139174
Namespace: namespace,
140-
Name: clusterName + "-2-wal",
175+
Name: backup.Name + "-wal",
141176
Labels: map[string]string{
142-
utils.BackupNameLabelName: backupName,
177+
utils.BackupNameLabelName: backup.Name,
143178
},
144179
},
145180
},
@@ -178,9 +213,9 @@ var _ = Describe("Volumesnapshot reconciler", func() {
178213
{
179214
ObjectMeta: metav1.ObjectMeta{
180215
Namespace: namespace,
181-
Name: clusterName + "-2",
216+
Name: backup.Name,
182217
Labels: map[string]string{
183-
utils.BackupNameLabelName: backupName,
218+
utils.BackupNameLabelName: backup.Name,
184219
},
185220
},
186221
Status: &storagesnapshotv1.VolumeSnapshotStatus{
@@ -191,9 +226,9 @@ var _ = Describe("Volumesnapshot reconciler", func() {
191226
{
192227
ObjectMeta: metav1.ObjectMeta{
193228
Namespace: namespace,
194-
Name: clusterName + "-2-wal",
229+
Name: backup.Name + "-wal",
195230
Labels: map[string]string{
196-
utils.BackupNameLabelName: backupName,
231+
utils.BackupNameLabelName: backup.Name,
197232
},
198233
},
199234
Status: &storagesnapshotv1.VolumeSnapshotStatus{

0 commit comments

Comments
 (0)