Skip to content

Commit

Permalink
PVC resizing support
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Makhov <[email protected]>
  • Loading branch information
makhov committed Jun 13, 2024
1 parent 3ca59d4 commit 01405de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/controller/k0smotron.io/k0smotroncluster_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (r *ClusterReconciler) generateEtcdStatefulSet(kmc *km.Cluster, replicas in

size := kmc.Spec.Etcd.Persistence.Size

if n, _ := size.AsInt64(); n == 0 {
if size.IsZero() {
size = resource.MustParse("1Gi")
}
pvc := v1.PersistentVolumeClaim{
Expand Down
19 changes: 15 additions & 4 deletions internal/controller/k0smotron.io/k0smotroncluster_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (r *ClusterReconciler) reconcileControlPlanePVC(ctx context.Context, kmc km
var pvc corev1.PersistentVolumeClaim
err := r.Get(ctx, client.ObjectKey{Namespace: kmc.Namespace, Name: name}, &pvc)
if err != nil {
if apierrors.IsNotFound(err) {
// Do nothing if PVC does not exist yet
return nil
}
return fmt.Errorf("failed to get PVC: %w", err)
}

Expand Down Expand Up @@ -100,6 +104,7 @@ func (r *ClusterReconciler) reconcileControlPlanePVC(ctx context.Context, kmc km
return fmt.Errorf("failed to delete pod for resizing: %w", err)
}
} else {
// Do not check other PVCs if expansion is not allowed
break
}
}
Expand All @@ -108,8 +113,8 @@ func (r *ClusterReconciler) reconcileControlPlanePVC(ctx context.Context, kmc km
}

func (r *ClusterReconciler) reconcileEtcdPVC(ctx context.Context, kmc km.Cluster) error {
var sts appsv1.StatefulSet
err := r.Get(ctx, client.ObjectKey{Namespace: kmc.Namespace, Name: kmc.GetEtcdStatefulSetName()}, &sts)
var etcdSts appsv1.StatefulSet
err := r.Get(ctx, client.ObjectKey{Namespace: kmc.Namespace, Name: kmc.GetEtcdStatefulSetName()}, &etcdSts)
if err != nil {
// Do nothing if StatefulSet does not exist yet
if apierrors.IsNotFound(err) {
Expand All @@ -120,7 +125,8 @@ func (r *ClusterReconciler) reconcileEtcdPVC(ctx context.Context, kmc km.Cluster
}

// Do nothing if the sizes match
if kmc.Spec.Etcd.Persistence.Size.Cmp(*sts.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests.Storage()) == 0 {
if kmc.Spec.Etcd.Persistence.Size.IsZero() ||
kmc.Spec.Etcd.Persistence.Size.Cmp(*etcdSts.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests.Storage()) == 0 {
return nil
}

Expand All @@ -132,6 +138,10 @@ func (r *ClusterReconciler) reconcileEtcdPVC(ctx context.Context, kmc km.Cluster
name := fmt.Sprintf("etcd-data-%s-%d", kmc.GetEtcdStatefulSetName(), i)
err := r.Get(ctx, client.ObjectKey{Namespace: kmc.Namespace, Name: name}, &pvc)
if err != nil {
if apierrors.IsNotFound(err) {
// Do nothing if PVC does not exist yet
return nil
}
return fmt.Errorf("failed to get etcd PVC: %w", err)
}

Expand Down Expand Up @@ -163,9 +173,10 @@ func (r *ClusterReconciler) reconcileEtcdPVC(ctx context.Context, kmc km.Cluster
return fmt.Errorf("failed to delete etcd pod for resizing: %w", err)
}
} else {
// Do not check other PVCs if expansion is not allowed
break
}
}

return r.Delete(ctx, &sts, &client.DeleteOptions{PropagationPolicy: ptr.To(metav1.DeletePropagationOrphan)})
return r.Delete(ctx, &etcdSts, &client.DeleteOptions{PropagationPolicy: ptr.To(metav1.DeletePropagationOrphan)})
}
2 changes: 1 addition & 1 deletion inttest/scaling-etcd/scaling_etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *ScalingSuite) TestK0sGetsUp() {
err = wait.PollImmediateUntilWithContext(s.Context(), 1*time.Second, func(ctx context.Context) (bool, error) {
etcdSts, err := kc.AppsV1().StatefulSets("default").Get(s.Context(), "kmc-scaling-etcd", metav1.GetOptions{})
if err != nil {
return false, err
return false, nil
}

return etcdSts.Status.Replicas == 3, nil
Expand Down

0 comments on commit 01405de

Please sign in to comment.