Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K8SPXC-1341: Resize PVCs on user request #1588

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
537 changes: 417 additions & 120 deletions config/crd/bases/pxc.percona.com_perconaxtradbclusters.yaml

Large diffs are not rendered by default.

537 changes: 417 additions & 120 deletions deploy/bundle.yaml

Large diffs are not rendered by default.

537 changes: 417 additions & 120 deletions deploy/crd.yaml

Large diffs are not rendered by default.

537 changes: 417 additions & 120 deletions deploy/cw-bundle.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions e2e-tests/pvc-resize/compare/select-1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100500
38 changes: 38 additions & 0 deletions e2e-tests/pvc-resize/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

set -o errexit

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

set_debug

create_infra ${namespace}

desc 'create first PXC cluster'
cluster="some-name"
spinup_pxc "${cluster}" "$conf_dir/$cluster.yml" "3" "10" "${conf_dir}/secrets.yml"

kubectl_bin patch pxc "$cluster" --type=merge --patch '{
"spec": { "pxc": { "volumeSpec": { "persistentVolumeClaim": { "resources": { "requests": { "storage": "4Gi" } } } } } }
}'
wait_cluster_consistency "$cluster" 3 2

for pvc in $(kubectl_bin get pvc -l app.kubernetes.io/component=pxc -o name); do
retry=0
until [[ $(kubectl_bin get ${pvc} -o jsonpath={.status.capacity.storage}) == "4Gi" ]]; do
egegunes marked this conversation as resolved.
Show resolved Hide resolved
if [[ $retry -ge 60 ]]; then
echo "PVC ${pvc} was not resized, max retries exceeded"
exit 1
fi
egegunes marked this conversation as resolved.
Show resolved Hide resolved

echo "Waiting for PVC ${pvc} to be resized"
sleep 5
egegunes marked this conversation as resolved.
Show resolved Hide resolved

retry=$((retry+1))
done
echo "PVC ${pvc} was resized"
egegunes marked this conversation as resolved.
Show resolved Hide resolved
done

destroy "${namespace}"
desc "test passed"
egegunes marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions e2e-tests/run-pr.csv
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ upgrade-proxysql,8.0
users,5.7
users,8.0
validation-hook,8.0
pvc-resize,5.7
pvc-resize,8.0
5 changes: 5 additions & 0 deletions pkg/controller/pxc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ func (r *ReconcilePerconaXtraDBCluster) Reconcile(ctx context.Context, request r
log.Info("failed to ensure version, running with default", "error", err)
}
}

egegunes marked this conversation as resolved.
Show resolved Hide resolved
err = r.reconcilePersistentVolumes(ctx, o)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "reconcile persistent volumes")
}

err = r.deploy(ctx, o)
if err != nil {
Expand Down
95 changes: 95 additions & 0 deletions pkg/controller/pxc/volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package pxc

import (
"context"
"strings"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

api "github.com/percona/percona-xtradb-cluster-operator/pkg/apis/pxc/v1"
"github.com/percona/percona-xtradb-cluster-operator/pkg/pxc/app/statefulset"
)

func (r *ReconcilePerconaXtraDBCluster) reconcilePersistentVolumes(ctx context.Context, cr *api.PerconaXtraDBCluster) error {
log := logf.FromContext(ctx)

pxcSet := statefulset.NewNode(cr)
sts := pxcSet.StatefulSet()

err := r.client.Get(ctx, client.ObjectKeyFromObject(sts), sts)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, "get statefulset/%s", sts.Name)
}

if cr.Spec.PXC.VolumeSpec.PersistentVolumeClaim == nil {
return nil
}

var volumeTemplate corev1.PersistentVolumeClaim
for _, vct := range sts.Spec.VolumeClaimTemplates {
if vct.Name == "datadir" {
volumeTemplate = vct
}
}

requested := cr.Spec.PXC.VolumeSpec.PersistentVolumeClaim.Resources.Requests[corev1.ResourceStorage]
actual := volumeTemplate.Spec.Resources.Requests[corev1.ResourceStorage]

if requested.Cmp(actual) < 0 {
return errors.Wrap(err, "requested storage is less than actual")
}

if requested.Cmp(actual) == 0 {
return nil
}

// Check storage class for allowVolumeExpansion field

labels := map[string]string{
"app.kubernetes.io/component": "pxc",
"app.kubernetes.io/instance": cr.Name,
"app.kubernetes.io/managed-by": "percona-xtradb-cluster-operator",
"app.kubernetes.io/name": "percona-xtradb-cluster",
"app.kubernetes.io/part-of": "percona-xtradb-cluster",
}

pvcList := corev1.PersistentVolumeClaimList{}
if err := r.client.List(ctx, &pvcList, client.InNamespace(cr.Namespace), client.MatchingLabels(labels)); err != nil {
return errors.Wrap(err, "list persistentvolumeclaims")
}

pvcNames := make([]string, 0, len(pvcList.Items))
for _, pvc := range pvcList.Items {
pvcNames = append(pvcNames, pvc.Name)
}

log.Info("Resizing PVCs", "requested", requested, "actual", actual, "pvcList", strings.Join(pvcNames, ","))

log.Info("Deleting statefulset", "name", sts.Name)

if err := r.client.Delete(ctx, sts, client.PropagationPolicy("Orphan")); err != nil {
return errors.Wrapf(err, "delete statefulset/%s", sts.Name)
}

for _, pvc := range pvcList.Items {
if !strings.HasPrefix(pvc.Name, "datadir-"+sts.Name) {
continue
}

log.Info("Resizing PVC", "name", pvc.Name)
pvc.Spec.Resources.Requests[corev1.ResourceStorage] = requested

if err := r.client.Update(ctx, &pvc); err != nil {
return errors.Wrapf(err, "update persistentvolumeclaim/%s", pvc.Name)
}
}

return nil
}
Loading