Skip to content

Commit

Permalink
Fix find resize target.
Browse files Browse the repository at this point in the history
Signed-off-by: d-kuro <[email protected]>
  • Loading branch information
d-kuro committed Jun 22, 2022
1 parent 5b1d091 commit 4e86677
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
9 changes: 5 additions & 4 deletions controllers/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions controllers/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])
Expand Down

0 comments on commit 4e86677

Please sign in to comment.