From f0d841fed7edb7d216fcc6ac9d96ec36f5f30df2 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 30 Jun 2023 18:16:50 +0800 Subject: [PATCH] br: fix renaming pvc when there are multiple volumes in tikv pod (#4951) (#5112) Signed-off-by: WangLe1321 Co-authored-by: WangLe1321 --- pkg/backup/snapshotter/snapshotter.go | 77 ++-- pkg/backup/snapshotter/snapshotter_test.go | 446 +++++++++++++++++++-- pkg/backup/testutils/br.go | 13 +- 3 files changed, 472 insertions(+), 64 deletions(-) diff --git a/pkg/backup/snapshotter/snapshotter.go b/pkg/backup/snapshotter/snapshotter.go index ddf66748ba..29383a93db 100644 --- a/pkg/backup/snapshotter/snapshotter.go +++ b/pkg/backup/snapshotter/snapshotter.go @@ -508,39 +508,53 @@ func (m *StoresMixture) ProcessCSBPVCsAndPVs(r *v1alpha1.Restore, csb *CloudSnap } // If a backup cluster has scaled in and the tidb-operator enabled advanced-statefulset(ref: https://docs.pingcap.com/zh/tidb-in-kubernetes/stable/advanced-statefulset) -// the name of pvc can be not sequential, eg: [tikv-db-tikv-0, tikv-db-tikv-1, tikv-db-tikv-2, tikv-db-tikv-6, tikv-db-tikv-7, tikv-db-tikv-8] -// When create cluster, we should ensure pvc name sequential, so we should reset it to -// [tikv-db-tikv-0, tikv-db-tikv-1, tikv-db-tikv-2, tikv-db-tikv-3, tikv-db-tikv-4, tikv-db-tikv-5] +// the name of pvc can be not sequential, and every tikv pod can have multiple pvc, eg: +// [tikv-db-tikv-0, tikv-db-tikv-2, tikv-db-tikv-3, tikv-raft-db-tikv-0, tikv-raft-db-tikv-2, tikv-raft-db-tikv-3] +// the format of tikv pvc name is {volume_name}-{tc_name}-tikv-{index} +// When create cluster, we should ensure pvc name with same volume sequential, so we should reset it to +// [tikv-db-tikv-0, tikv-db-tikv-1, tikv-db-tikv-2, tikv-raft-db-tikv-0, tikv-raft-db-tikv-1, tikv-raft-db-tikv-2] func resetPVCSequence(stsName string, pvcs []*corev1.PersistentVolumeClaim, pvs []*corev1.PersistentVolume) ( []*corev1.PersistentVolumeClaim, []*corev1.PersistentVolume, error) { type indexedPVC struct { index int pvc *corev1.PersistentVolumeClaim } - indexedPVCs := make([]*indexedPVC, 0, len(pvcs)) - reStr := fmt.Sprintf(`%s-(\d+)$`, stsName) + indexedPVCsGroups := make(map[string][]*indexedPVC, 8) + reStr := fmt.Sprintf(`^(.+)-%s-(\d+)$`, stsName) re := regexp.MustCompile(reStr) + // because one tikv may have multiple volumes, corresponding multiple pvc, and every pvc has serial number. + // we should split pvc to multiple groups by volume name, and make the pvc sequential in every group for _, pvc := range pvcs { subMatches := re.FindStringSubmatch(pvc.Name) // subMatches contains full text that matches regex and the matches in brackets. - // so if the pvc matches regex, it should contain 2 items. - if len(subMatches) != 2 { + // so if the pvc matches regex, it should contain 3 items. + if len(subMatches) != 3 { return nil, nil, fmt.Errorf("pvc name %s doesn't match regex %s", pvc.Name, reStr) } + volumeName := subMatches[1] + pvcNumberStr := subMatches[2] // get the number of pvc, for example, there is a pvc "tikv-db-tikv-0", try to get the number "0" - index, err := strconv.Atoi(subMatches[1]) + index, err := strconv.Atoi(pvcNumberStr) if err != nil { - return nil, nil, fmt.Errorf("parse index %s of pvc %s to int: %s", subMatches[1], pvc.Name, err.Error()) + return nil, nil, fmt.Errorf("parse index %s of pvc %s to int: %s", pvcNumberStr, pvc.Name, err.Error()) } - indexedPVCs = append(indexedPVCs, &indexedPVC{ + // get the volume name of pod from pvc, for example, there is a pvc "tikv-db-tikv-0", get "tikv" + // there can be multiple volumes in a pod, so we should group pvc by volume name + indexedPVCs, ok := indexedPVCsGroups[volumeName] + if !ok { + indexedPVCs = make([]*indexedPVC, 0, len(pvcs)) + } + indexedPVCsGroups[volumeName] = append(indexedPVCs, &indexedPVC{ index: index, pvc: pvc, }) } - sort.Slice(indexedPVCs, func(i, j int) bool { - return indexedPVCs[i].index < indexedPVCs[j].index - }) + for _, indexedPVCs := range indexedPVCsGroups { + sort.Slice(indexedPVCs, func(i, j int) bool { + return indexedPVCs[i].index < indexedPVCs[j].index + }) + } pvc2pv := make(map[string]*corev1.PersistentVolume, len(pvs)) for _, pv := range pvs { pvc2pv[pv.Spec.ClaimRef.Name] = pv @@ -548,24 +562,27 @@ func resetPVCSequence(stsName string, pvcs []*corev1.PersistentVolumeClaim, pvs sequentialPVCs := make([]*corev1.PersistentVolumeClaim, 0, len(pvcs)) sequentialPVs := make([]*corev1.PersistentVolume, 0, len(pvs)) - for i, iPVC := range indexedPVCs { - pv, ok := pvc2pv[iPVC.pvc.Name] - if !ok { - return nil, nil, fmt.Errorf("pv with claim %s not found", iPVC.pvc.Name) - } - // when create cluster, tidb-operator will create tikv instances from 0 to n-1, and pvcs are also [0, n-1] - // for backup cluster, advanced statefulset allows scaling in at arbitrary position - // so pvcs of backup might not be [0, n-1], we should reset it to [0, n-1] - if i != iPVC.index { - names := strings.Split(iPVC.pvc.Name, "-") - restorePVCName := fmt.Sprintf("%s-%d", strings.Join(names[:len(names)-1], "-"), i) - - klog.Infof("reset pvc name %s to %s", iPVC.pvc.Name, restorePVCName) - iPVC.pvc.Name = restorePVCName - pv.Spec.ClaimRef.Name = restorePVCName + // for every pvc group, make the pvc list in the group sequential + for _, indexedPVCs := range indexedPVCsGroups { + for i, iPVC := range indexedPVCs { + pv, ok := pvc2pv[iPVC.pvc.Name] + if !ok { + return nil, nil, fmt.Errorf("pv with claim %s not found", iPVC.pvc.Name) + } + // when create cluster, tidb-operator will create tikv instances from 0 to n-1, and pvcs are also [0, n-1] + // for backup cluster, advanced statefulset allows scaling in at arbitrary position + // so pvcs of backup might not be [0, n-1], we should reset it to [0, n-1] + if i != iPVC.index { + names := strings.Split(iPVC.pvc.Name, "-") + restorePVCName := fmt.Sprintf("%s-%d", strings.Join(names[:len(names)-1], "-"), i) + + klog.Infof("reset pvc name %s to %s", iPVC.pvc.Name, restorePVCName) + iPVC.pvc.Name = restorePVCName + pv.Spec.ClaimRef.Name = restorePVCName + } + sequentialPVCs = append(sequentialPVCs, iPVC.pvc) + sequentialPVs = append(sequentialPVs, pv) } - sequentialPVCs = append(sequentialPVCs, iPVC.pvc) - sequentialPVs = append(sequentialPVs, pv) } return sequentialPVCs, sequentialPVs, nil } diff --git a/pkg/backup/snapshotter/snapshotter_test.go b/pkg/backup/snapshotter/snapshotter_test.go index c932897924..f44ab8b3b0 100644 --- a/pkg/backup/snapshotter/snapshotter_test.go +++ b/pkg/backup/snapshotter/snapshotter_test.go @@ -15,6 +15,7 @@ package snapshotter import ( "encoding/json" + "sort" "strconv" "strings" "testing" @@ -890,6 +891,11 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { SnapshotID: "snap-1234567890abcdef0", RestoreVolumeID: "vol-0e65f40961a9f0001", }, + { + VolumeID: "vol-1e65f40961a9f6244", + SnapshotID: "snap-2234567890abcdef0", + RestoreVolumeID: "vol-1e65f40961a9f0001", + }, }, }, { @@ -900,6 +906,11 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { SnapshotID: "snap-1234567890abcdef1", RestoreVolumeID: "vol-0e65f40961a9f0002", }, + { + VolumeID: "vol-1e65f40961a9f6245", + SnapshotID: "snap-2234567890abcdef1", + RestoreVolumeID: "vol-1e65f40961a9f0002", + }, }, }, { @@ -910,6 +921,11 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { SnapshotID: "snap-1234567890abcdef2", RestoreVolumeID: "vol-0e65f40961a9f0003", }, + { + VolumeID: "vol-1e65f40961a9f6246", + SnapshotID: "snap-2234567890abcdef2", + RestoreVolumeID: "vol-1e65f40961a9f0003", + }, }, }, }, @@ -918,7 +934,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { PVs: []*corev1.PersistentVolume{ { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-1", + Name: "pv-1-1", Labels: map[string]string{ "test/label": "retained", }, @@ -942,7 +958,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-1", + Name: "tikv-test-tikv-1", UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3121", ResourceVersion: "1957", }, @@ -953,7 +969,42 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-2", + Name: "pv-1-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + constants.AnnTemporaryVolumeID: "vol-1e65f40961a9f6244", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3122", + ResourceVersion: "1958", + Finalizers: []string{ + "kubernetes.io/pv-protection", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9f6244", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-1", + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3121", + ResourceVersion: "1957", + }, + }, + Status: corev1.PersistentVolumeStatus{ + Phase: corev1.VolumeBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-2-1", Labels: map[string]string{ "test/label": "retained", }, @@ -977,7 +1028,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-2", + Name: "tikv-test-tikv-2", UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3123", ResourceVersion: "1959", }, @@ -988,7 +1039,42 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-3", + Name: "pv-2-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + constants.AnnTemporaryVolumeID: "vol-1e65f40961a9f6245", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3124", + ResourceVersion: "1960", + Finalizers: []string{ + "kubernetes.io/pv-protection", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9f6245", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-2", + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3123", + ResourceVersion: "1959", + }, + }, + Status: corev1.PersistentVolumeStatus{ + Phase: corev1.VolumeBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-3-1", Labels: map[string]string{ "test/label": "retained", }, @@ -1012,7 +1098,42 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-3", + Name: "tikv-test-tikv-3", + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3125", + ResourceVersion: "1961", + }, + }, + Status: corev1.PersistentVolumeStatus{ + Phase: corev1.VolumeBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-3-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + constants.AnnTemporaryVolumeID: "vol-1e65f40961a9f6246", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3126", + ResourceVersion: "1962", + Finalizers: []string{ + "kubernetes.io/pv-protection", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9f6246", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-3", UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3125", ResourceVersion: "1961", }, @@ -1023,7 +1144,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-4", + Name: "pv-4-1", Labels: map[string]string{ "test/label": "retained", }, @@ -1047,7 +1168,42 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-4", + Name: "tikv-test-tikv-4", + UID: "301b0e8b-3538-4f61-a0fd-a25abd9acd23", + ResourceVersion: "1961", + }, + }, + Status: corev1.PersistentVolumeStatus{ + Phase: corev1.VolumeBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-4-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + constants.AnnTemporaryVolumeID: "vol-1e65f40961a9fcd23", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9acd23", + ResourceVersion: "1962", + Finalizers: []string{ + "kubernetes.io/pv-protection", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9fcd23", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-4", UID: "301b0e8b-3538-4f61-a0fd-a25abd9acd23", ResourceVersion: "1961", }, @@ -1060,7 +1216,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { PVCs: []*corev1.PersistentVolumeClaim{ { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-1", + Name: "tikv-test-tikv-1", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1077,7 +1233,57 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-1", + VolumeName: "pv-1-1", + }, + Status: corev1.PersistentVolumeClaimStatus{ + Phase: corev1.ClaimBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-raft-test-tikv-1", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnBindCompleted: "yes", + constants.KubeAnnBoundByController: "yes", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3121", + ResourceVersion: "1957", + Finalizers: []string{ + "kubernetes.io/pvc-protection", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-1-2", + }, + Status: corev1.PersistentVolumeClaimStatus{ + Phase: corev1.ClaimBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-test-tikv-2", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnBindCompleted: "yes", + constants.KubeAnnBoundByController: "yes", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3123", + ResourceVersion: "1959", + Finalizers: []string{ + "kubernetes.io/pvc-protection", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-2-1", }, Status: corev1.PersistentVolumeClaimStatus{ Phase: corev1.ClaimBound, @@ -1085,7 +1291,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-2", + Name: "tikv-raft-test-tikv-2", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1102,7 +1308,32 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-2", + VolumeName: "pv-2-2", + }, + Status: corev1.PersistentVolumeClaimStatus{ + Phase: corev1.ClaimBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-test-tikv-3", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnBindCompleted: "yes", + constants.KubeAnnBoundByController: "yes", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9a3125", + ResourceVersion: "1961", + Finalizers: []string{ + "kubernetes.io/pvc-protection", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-3-1", }, Status: corev1.PersistentVolumeClaimStatus{ Phase: corev1.ClaimBound, @@ -1110,7 +1341,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-3", + Name: "tikv-raft-test-tikv-3", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1127,7 +1358,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-3", + VolumeName: "pv-3-2", }, Status: corev1.PersistentVolumeClaimStatus{ Phase: corev1.ClaimBound, @@ -1135,7 +1366,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-4", + Name: "tikv-test-tikv-4", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1152,7 +1383,32 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-4", + VolumeName: "pv-4-1", + }, + Status: corev1.PersistentVolumeClaimStatus{ + Phase: corev1.ClaimBound, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-raft-test-tikv-4", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnBindCompleted: "yes", + constants.KubeAnnBoundByController: "yes", + "test/annotation": "retained", + }, + UID: "301b0e8b-3538-4f61-a0fd-a25abd9acd23", + ResourceVersion: "1961", + Finalizers: []string{ + "kubernetes.io/pvc-protection", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-4-2", }, Status: corev1.PersistentVolumeClaimStatus{ Phase: corev1.ClaimBound, @@ -1194,6 +1450,9 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { "vol-0e65f40961a9f6244": "vol-0e65f40961a9f0001", "vol-0e65f40961a9f6245": "vol-0e65f40961a9f0002", "vol-0e65f40961a9f6246": "vol-0e65f40961a9f0003", + "vol-1e65f40961a9f6244": "vol-1e65f40961a9f0001", + "vol-1e65f40961a9f6245": "vol-1e65f40961a9f0002", + "vol-1e65f40961a9f6246": "vol-1e65f40961a9f0003", } require.Equal(t, volIDMapWanted, m.rsVolIDMap) @@ -1201,7 +1460,7 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { pvsWanted := []*corev1.PersistentVolume{ { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-1", + Name: "pv-1-1", Labels: map[string]string{ "test/label": "retained", }, @@ -1219,13 +1478,37 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-0", + Name: "tikv-test-tikv-0", }, }, }, { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-2", + Name: "pv-1-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + "test/annotation": "retained", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9f0001", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-0", + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-2-1", Labels: map[string]string{ "test/label": "retained", }, @@ -1243,13 +1526,37 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-1", + Name: "tikv-test-tikv-1", + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-2-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + "test/annotation": "retained", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9f0002", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-1", }, }, }, { ObjectMeta: metav1.ObjectMeta{ - Name: "pv-3", + Name: "pv-3-1", Labels: map[string]string{ "test/label": "retained", }, @@ -1267,18 +1574,49 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, ClaimRef: &corev1.ObjectReference{ - Name: "test-tikv-2", + Name: "tikv-test-tikv-2", + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pv-3-2", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + constants.KubeAnnDynamicallyProvisioned: "ebs.csi.aws.com", + "test/annotation": "retained", + }, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + Driver: "ebs.csi.aws.com", + VolumeHandle: "vol-1e65f40961a9f0003", + FSType: "ext4", + }, + }, + ClaimRef: &corev1.ObjectReference{ + Name: "tikv-raft-test-tikv-2", }, }, }, } + + sort.Slice(pvsWanted, func(i, j int) bool { + return pvsWanted[i].Name < pvsWanted[j].Name + }) + sort.Slice(csb.Kubernetes.PVs, func(i, j int) bool { + return csb.Kubernetes.PVs[i].Name < csb.Kubernetes.PVs[j].Name + }) assert.Equal(t, pvsWanted, csb.Kubernetes.PVs) // happy path for reformed PVCs as the reborn resource pvcsWanted := []*corev1.PersistentVolumeClaim{ { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-0", + Name: "tikv-test-tikv-0", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1288,12 +1626,12 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-1", + VolumeName: "pv-1-1", }, }, { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-1", + Name: "tikv-raft-test-tikv-0", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1303,12 +1641,12 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-2", + VolumeName: "pv-1-2", }, }, { ObjectMeta: metav1.ObjectMeta{ - Name: "test-tikv-2", + Name: "tikv-test-tikv-1", Namespace: "default", Labels: map[string]string{ "test/label": "retained", @@ -1318,10 +1656,62 @@ func TestProcessCSBPVCsAndPVs(t *testing.T) { }, }, Spec: corev1.PersistentVolumeClaimSpec{ - VolumeName: "pv-3", + VolumeName: "pv-2-1", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-raft-test-tikv-1", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + "test/annotation": "retained", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-2-2", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-test-tikv-2", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + "test/annotation": "retained", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-3-1", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "tikv-raft-test-tikv-2", + Namespace: "default", + Labels: map[string]string{ + "test/label": "retained", + }, + Annotations: map[string]string{ + "test/annotation": "retained", + }, + }, + Spec: corev1.PersistentVolumeClaimSpec{ + VolumeName: "pv-3-2", }, }, } + + sort.Slice(pvcsWanted, func(i, j int) bool { + return pvcsWanted[i].Name < pvcsWanted[j].Name + }) + sort.Slice(csb.Kubernetes.PVCs, func(i, j int) bool { + return csb.Kubernetes.PVCs[i].Name < csb.Kubernetes.PVCs[j].Name + }) assert.Equal(t, pvcsWanted, csb.Kubernetes.PVCs) } diff --git a/pkg/backup/testutils/br.go b/pkg/backup/testutils/br.go index a862a6a429..aaabeeae3a 100644 --- a/pkg/backup/testutils/br.go +++ b/pkg/backup/testutils/br.go @@ -115,7 +115,7 @@ func ConstructRestoreMetaStr() string { "kubernetes": { "pvcs": [{ "metadata": { - "name": "test-tikv-1", + "name": "tikv-test-tikv-1", "uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3121", "resourceVersion": "1957", "creationTimestamp": null, @@ -138,7 +138,7 @@ func ConstructRestoreMetaStr() string { } }, { "metadata": { - "name": "test-tikv-2", + "name": "tikv-test-tikv-2", "uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3123", "resourceVersion": "1959", "creationTimestamp": null, @@ -161,7 +161,7 @@ func ConstructRestoreMetaStr() string { } }, { "metadata": { - "name": "test-tikv-3", + "name": "tikv-test-tikv-3", "uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3125", "resourceVersion": "1961", "creationTimestamp": null, @@ -206,7 +206,7 @@ func ConstructRestoreMetaStr() string { "fsType": "ext4" }, "claimRef": { - "name": "test-tikv-1", + "name": "tikv-test-tikv-1", "uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3121", "resourceVersion": "1957" } @@ -237,7 +237,7 @@ func ConstructRestoreMetaStr() string { "fsType": "ext4" }, "claimRef": { - "name": "test-tikv-2", + "name": "tikv-test-tikv-2", "uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3123", "resourceVersion": "1959" } @@ -268,7 +268,7 @@ func ConstructRestoreMetaStr() string { "fsType": "ext4" }, "claimRef": { - "name": "test-tikv-3", + "name": "tikv-test-tikv-3", "uid": "301b0e8b-3538-4f61-a0fd-a25abd9a3125", "resourceVersion": "1961" } @@ -279,6 +279,7 @@ func ConstructRestoreMetaStr() string { }], "crd_tidb_cluster": { "metadata": { + "name": "test", "creationTimestamp": null }, "spec": {