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 19b279a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 11 additions & 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 Expand Up @@ -299,6 +299,16 @@ func (r *ClusterReconciler) initialCluster(kmc *km.Cluster, replicas int32) stri

func (r *ClusterReconciler) generateEtcdInitContainers(kmc *km.Cluster) []v1.Container {
return []v1.Container{
{
Name: "dns-check",
Image: kmc.Spec.GetImage(),
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{"/bin/bash", "-c"},
Args: []string{"getent ahostsv4 ${HOSTNAME}.${SVC_NAME}." + kmc.Namespace + ".svc"},
Env: []v1.EnvVar{
{Name: "SVC_NAME", Value: kmc.GetEtcdServiceName()},
},
},
{
Name: "init",
Image: kmc.Spec.Etcd.Image,
Expand Down
22 changes: 17 additions & 5 deletions internal/controller/k0smotron.io/k0smotroncluster_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (r *ClusterReconciler) reconcileControlPlanePVC(ctx context.Context, kmc km
}

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

Expand All @@ -69,6 +70,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 +105,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 +114,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 +126,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 +139,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 +174,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 19b279a

Please sign in to comment.