diff --git a/pkg/manager/member/pvc_resizer.go b/pkg/manager/member/pvc_resizer.go index ca1b50f3e56..8084145e159 100644 --- a/pkg/manager/member/pvc_resizer.go +++ b/pkg/manager/member/pvc_resizer.go @@ -64,6 +64,7 @@ type PVCResizerInterface interface { var ( pdRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.PDLabelVal}) + tidbRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.TiDBLabelVal}) tikvRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.TiKVLabelVal}) tiflashRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.TiFlashLabelVal}) pumpRequirement = util.MustNewRequirement(label.ComponentLabelKey, selection.Equals, []string{label.PumpLabelVal}) @@ -86,14 +87,14 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error { return err } - // First we compose a map, with PVC name prefix for current component as keys, + // For each component, we compose a map called pvcPrefix2Quantity, with PVC name prefix for current component as keys, // for example "pd-${tcName}-pd" (for tc.Spec.PD.Requests) or "pd-log-${tcName}-pd" (for tc.Spec.PD.storageVolumes elements with name "log"). // Reference implementation of BuildStorageVolumeAndVolumeMount(). // Note: for TiFlash, it is currently "data0-${tcName}-tiflash" (for tc.Spec.TiFlash.StorageClaims elements, in list definition order) - pvcPrefix2Quantity := make(map[string]resource.Quantity) // patch PD PVCs if tc.Spec.PD != nil { + pvcPrefix2Quantity := make(map[string]resource.Quantity) pdMemberType := v1alpha1.PDMemberType.String() if quantity, ok := tc.Spec.PD.Requests[corev1.ResourceStorage]; ok { key := fmt.Sprintf("%s-%s-%s", pdMemberType, tc.Name, pdMemberType) @@ -111,8 +112,25 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error { return err } } + // patch TiDB PVCs + if tc.Spec.TiDB != nil { + pvcPrefix2Quantity := make(map[string]resource.Quantity) + tidbMemberType := v1alpha1.TiDBMemberType.String() + for _, sv := range tc.Spec.TiDB.StorageVolumes { + key := fmt.Sprintf("%s-%s-%s-%s", tidbMemberType, sv.Name, tc.Name, tidbMemberType) + if quantity, err := resource.ParseQuantity(sv.StorageSize); err == nil { + pvcPrefix2Quantity[key] = quantity + } else { + klog.Warningf("StorageVolume %q in %s/%s .Spec.TiDB is invalid", sv.Name, ns, tc.Name) + } + } + if err := p.patchPVCs(ns, selector.Add(*tidbRequirement), pvcPrefix2Quantity); err != nil { + return err + } + } // patch TiKV PVCs if tc.Spec.TiKV != nil { + pvcPrefix2Quantity := make(map[string]resource.Quantity) tikvMemberType := v1alpha1.TiKVMemberType.String() if quantity, ok := tc.Spec.TiKV.Requests[corev1.ResourceStorage]; ok { key := fmt.Sprintf("%s-%s-%s", tikvMemberType, tc.Name, tikvMemberType) @@ -132,6 +150,7 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error { } // patch TiFlash PVCs if tc.Spec.TiFlash != nil { + pvcPrefix2Quantity := make(map[string]resource.Quantity) tiflashMemberType := v1alpha1.TiFlashMemberType.String() for i, claim := range tc.Spec.TiFlash.StorageClaims { key := fmt.Sprintf("data%d-%s-%s", i, tc.Name, tiflashMemberType) @@ -145,6 +164,7 @@ func (p *pvcResizer) Resize(tc *v1alpha1.TidbCluster) error { } // patch Pump PVCs if tc.Spec.Pump != nil { + pvcPrefix2Quantity := make(map[string]resource.Quantity) pumpMemberType := v1alpha1.PumpMemberType.String() if quantity, ok := tc.Spec.Pump.Requests[corev1.ResourceStorage]; ok { key := fmt.Sprintf("data-%s-%s", tc.Name, pumpMemberType) @@ -164,20 +184,23 @@ func (p *pvcResizer) ResizeDM(dc *v1alpha1.DMCluster) error { if err != nil { return err } - pvcPrefix2Quantity := make(map[string]resource.Quantity) // patch dm-master PVCs - if quantity, err := resource.ParseQuantity(dc.Spec.Master.StorageSize); err == nil { - dmMasterMemberType := v1alpha1.DMMasterMemberType.String() - key := fmt.Sprintf("%s-%s-%s", dmMasterMemberType, dc.Name, dmMasterMemberType) - pvcPrefix2Quantity[key] = quantity - } - if err := p.patchPVCs(ns, selector.Add(*dmMasterRequirement), pvcPrefix2Quantity); err != nil { - return err + { + pvcPrefix2Quantity := make(map[string]resource.Quantity) + if quantity, err := resource.ParseQuantity(dc.Spec.Master.StorageSize); err == nil { + dmMasterMemberType := v1alpha1.DMMasterMemberType.String() + key := fmt.Sprintf("%s-%s-%s", dmMasterMemberType, dc.Name, dmMasterMemberType) + pvcPrefix2Quantity[key] = quantity + } + if err := p.patchPVCs(ns, selector.Add(*dmMasterRequirement), pvcPrefix2Quantity); err != nil { + return err + } } // patch dm-worker PVCs if dc.Spec.Worker != nil { + pvcPrefix2Quantity := make(map[string]resource.Quantity) if quantity, err := resource.ParseQuantity(dc.Spec.Worker.StorageSize); err == nil { dmWorkerMemberType := v1alpha1.DMWorkerMemberType.String() key := fmt.Sprintf("%s-%s-%s", dmWorkerMemberType, dc.Name, dmWorkerMemberType) @@ -211,9 +234,6 @@ func (p *pvcResizer) patchPVCs(ns string, selector labels.Selector, pvcQuantityI return err } - if err != nil { - return err - } // the PVC name for StatefulSet will be ${pvcNameInTemplate}-${stsName}-${ordinal}, here we want to drop the ordinal rePvcPrefix := regexp.MustCompile(`^(.+)-\d+$`) for _, pvc := range pvcs { diff --git a/pkg/manager/member/pvc_resizer_test.go b/pkg/manager/member/pvc_resizer_test.go index b443980e125..f24cd5a926c 100644 --- a/pkg/manager/member/pvc_resizer_test.go +++ b/pkg/manager/member/pvc_resizer_test.go @@ -126,6 +126,36 @@ func TestPVCResizer(t *testing.T) { newPVCWithStorage("pd-log-tc-pd-2", label.PDLabelVal, "sc", "2Gi"), }, }, + { + name: "resize TiDB PVCs", + tc: &v1alpha1.TidbCluster{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: v1.NamespaceDefault, + Name: "tc", + }, + Spec: v1alpha1.TidbClusterSpec{ + TiDB: &v1alpha1.TiDBSpec{ + StorageVolumes: []v1alpha1.StorageVolume{ + { + Name: "log", + StorageSize: "2Gi", + }, + }, + }, + }, + }, + sc: newStorageClass("sc", true), + pvcs: []*v1.PersistentVolumeClaim{ + newPVCWithStorage("tidb-log-tc-tidb-0", label.TiDBLabelVal, "sc", "1Gi"), + newPVCWithStorage("tidb-log-tc-tidb-1", label.TiDBLabelVal, "sc", "1Gi"), + newPVCWithStorage("tidb-log-tc-tidb-2", label.TiDBLabelVal, "sc", "1Gi"), + }, + wantPVCs: []*v1.PersistentVolumeClaim{ + newPVCWithStorage("tidb-log-tc-tidb-0", label.TiDBLabelVal, "sc", "2Gi"), + newPVCWithStorage("tidb-log-tc-tidb-1", label.TiDBLabelVal, "sc", "2Gi"), + newPVCWithStorage("tidb-log-tc-tidb-2", label.TiDBLabelVal, "sc", "2Gi"), + }, + }, { name: "resize TiKV PVCs", tc: &v1alpha1.TidbCluster{