From 32b8fbbd9cc1b4ea2b024e04f6f1a82a57282d2f Mon Sep 17 00:00:00 2001 From: d-kuro Date: Tue, 29 Aug 2023 21:50:16 +0900 Subject: [PATCH] Fix pvc reduce tests Signed-off-by: d-kuro --- controllers/pvc.go | 3 +++ controllers/pvc_test.go | 46 ++++++++++++++++++++++++++++++++++++++--- e2e/pvc_test.go | 4 ++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/controllers/pvc.go b/controllers/pvc.go index b410e60a8..bd103385a 100644 --- a/controllers/pvc.go +++ b/controllers/pvc.go @@ -131,6 +131,9 @@ func (r *MySQLClusterReconciler) resizePVCs(ctx context.Context, cluster *mocov1 case i == 0: // volume size is equal continue case i == 1: // current volume size is greater than new size + // The size of the Persistent Volume Claims (PVC) cannot be reduced. + // Although MOCO permits the reduction of PVC, an automatic resize is not performed. + // An error arises if a PVC involving reduction is passed, as it's unexpected. return resizedPVC, fmt.Errorf("failed to resize pvc %q, want size: %s, deployed size: %s: %w", pvc.Name, newSize.String(), pvc.Spec.Resources.Requests.Storage().String(), ErrReduceVolumeSize) case i == -1: // current volume size is smaller than new size pvc.Spec.Resources.Requests[corev1.ResourceStorage] = *newSize diff --git a/controllers/pvc_test.go b/controllers/pvc_test.go index c2ec19cc3..dc6733213 100644 --- a/controllers/pvc_test.go +++ b/controllers/pvc_test.go @@ -159,7 +159,6 @@ func TestNeedResizePVC(t *testing.T) { cluster: newMySQLClusterWithVolumeSize(resource.MustParse("1Gi")), sts: newStatefulSetWithVolumeSize(resource.MustParse("2Gi")), wantResize: false, - wantError: ErrReduceVolumeSize, }, { name: "StatefulSet has more PVCs", @@ -201,6 +200,47 @@ func TestNeedResizePVC(t *testing.T) { sts: newStatefulSetWithVolumeSize(resource.MustParse("1Gi")), wantResize: false, }, + { + name: "Mix expansion and reduction", + cluster: func() *mocov1beta2.MySQLCluster { + cluster := newMySQLClusterWithVolumeSize(resource.MustParse("2Gi")) + 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: 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("2Gi")}, + }, + }, + } + + sts.Spec.VolumeClaimTemplates = append(sts.Spec.VolumeClaimTemplates, pvc) + + return sts + }(), + wantResizeTarget: func() map[string]corev1.PersistentVolumeClaim { + sts := newStatefulSetWithVolumeSize(resource.MustParse("1Gi")) + pvc := sts.Spec.VolumeClaimTemplates[0] + m := make(map[string]corev1.PersistentVolumeClaim) + m[pvc.Name] = pvc + return m + }(), + wantResize: true, + }, } for _, tt := range tests { @@ -208,7 +248,7 @@ func TestNeedResizePVC(t *testing.T) { t.Run(tt.name, func(t *testing.T) { r := &MySQLClusterReconciler{} resizeTarget, resize, err := r.needResizePVC(tt.cluster, tt.sts) - if err != nil { + if tt.wantError != nil { if !errors.Is(err, tt.wantError) { t.Fatalf("want error %v, got %v", tt.wantError, err) } @@ -224,7 +264,7 @@ func TestNeedResizePVC(t *testing.T) { 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]) + t.Fatalf("unexpected resize target: %s", diff) } } }) diff --git a/e2e/pvc_test.go b/e2e/pvc_test.go index 89f1c012b..f2461176d 100644 --- a/e2e/pvc_test.go +++ b/e2e/pvc_test.go @@ -102,7 +102,7 @@ var _ = Context("pvc_test", func() { if cond.Type != mocov1beta2.ConditionHealthy { continue } - if cond.Status == corev1.ConditionTrue { + if cond.Status == metav1.ConditionTrue { return nil } return fmt.Errorf("cluster is not healthy: %s", cond.Status) @@ -136,7 +136,7 @@ var _ = Context("pvc_test", func() { if cond.Type != mocov1beta2.ConditionHealthy { continue } - if cond.Status == corev1.ConditionTrue { + if cond.Status == metav1.ConditionTrue { return nil } return fmt.Errorf("cluster is not healthy: %s", cond.Status)