Skip to content

Commit

Permalink
Fix pvc reduce tests
Browse files Browse the repository at this point in the history
Signed-off-by: d-kuro <[email protected]>
  • Loading branch information
d-kuro committed Sep 1, 2023
1 parent edcdf36 commit 1bb84c0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
3 changes: 3 additions & 0 deletions controllers/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 43 additions & 3 deletions controllers/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -201,14 +200,55 @@ 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 {
tt := tt
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)
}
Expand All @@ -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)
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions e2e/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1bb84c0

Please sign in to comment.