diff --git a/controllers/pvc.go b/controllers/pvc.go index 5b46aa3de..454709ce8 100644 --- a/controllers/pvc.go +++ b/controllers/pvc.go @@ -147,9 +147,10 @@ func (*MySQLClusterReconciler) needResizePVC(cluster *mocov1beta2.MySQLCluster, pvcSet[pvc.Name] = pvc } + resizeTarget := make(map[string]corev1.PersistentVolumeClaim) + for _, pvc := range cluster.Spec.VolumeClaimTemplates { if _, ok := pvcSet[pvc.Name]; !ok { - delete(pvcSet, pvc.Name) continue } @@ -160,20 +161,20 @@ func (*MySQLClusterReconciler) needResizePVC(cluster *mocov1beta2.MySQLCluster, switch i := deployedSize.Cmp(wantSize.DeepCopy()); { case i == 0: // volume size is equal - delete(pvcSet, pvc.Name) continue case i == 1: // volume size is greater return nil, false, fmt.Errorf("failed to resize pvc %q, want size: %s, deployed size: %s: %w", pvc.Name, wantSize, deployedSize, ErrReduceVolumeSize) case i == -1: // volume size is smaller + resizeTarget[pvc.Name] = pvcSet[pvc.Name] continue } } - if len(pvcSet) == 0 { + if len(resizeTarget) == 0 { return nil, false, nil } - return pvcSet, true, nil + return resizeTarget, true, nil } func (r *MySQLClusterReconciler) isVolumeExpansionSupported(ctx context.Context, pvc *corev1.PersistentVolumeClaim) (bool, error) { diff --git a/controllers/pvc_test.go b/controllers/pvc_test.go index 29e855cca..f578b3aad 100644 --- a/controllers/pvc_test.go +++ b/controllers/pvc_test.go @@ -161,6 +161,46 @@ func TestNeedResizePVC(t *testing.T) { wantResize: false, wantError: ErrReduceVolumeSize, }, + { + name: "StatefulSet has more PVCs", + cluster: newMySQLClusterWithVolumeSize(resource.MustParse("1Gi")), + sts: func() *appsv1.StatefulSet { + sts := newStatefulSetWithVolumeSize(resource.MustParse("1Gi")) + pvc := corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: "new-data", + }, + Spec: corev1.PersistentVolumeClaimSpec{ + StorageClassName: pointer.String("default"), + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{corev1.ResourceStorage: resource.MustParse("1Gi")}, + }, + }, + } + + sts.Spec.VolumeClaimTemplates = append(sts.Spec.VolumeClaimTemplates, pvc) + + return sts + }(), + wantResize: false, + }, + { + name: "MySQLCluster has more PVCs", + cluster: func() *mocov1beta2.MySQLCluster { + cluster := newMySQLClusterWithVolumeSize(resource.MustParse("1Gi")) + pvc := mocov1beta2.PersistentVolumeClaim{ + ObjectMeta: mocov1beta2.ObjectMeta{Name: "new-data"}, + Spec: mocov1beta2.PersistentVolumeClaimSpecApplyConfiguration(*corev1ac.PersistentVolumeClaimSpec(). + WithStorageClassName("default").WithResources(corev1ac.ResourceRequirements(). + WithRequests(corev1.ResourceList{corev1.ResourceStorage: resource.MustParse("1Gi")}), + )), + } + cluster.Spec.VolumeClaimTemplates = append(cluster.Spec.VolumeClaimTemplates, pvc) + return cluster + }(), + sts: newStatefulSetWithVolumeSize(resource.MustParse("1Gi")), + wantResize: false, + }, } for _, tt := range tests { @@ -178,6 +218,10 @@ func TestNeedResizePVC(t *testing.T) { t.Fatalf("want resize %v, got %v", tt.wantResize, resize) } + if len(tt.wantResizeTarget) != len(resizeTarget) { + t.Fatalf("want resize target length %v, got %v", len(tt.wantResizeTarget), len(resizeTarget)) + } + for key, value := range tt.wantResizeTarget { if diff := cmp.Diff(value, resizeTarget[key]); len(diff) != 0 { t.Fatalf("want resize target %v, got %v", value, resizeTarget[key])