From 1cea127d6a5811e3293ee77c3eb1cb97b44fdb4e Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Tue, 27 Jun 2023 15:26:36 +0200 Subject: [PATCH] Use storage class requested by statefulset instead of the old PVC Until now we reused the storage class of the to be resized PVC. However that can be an issue if the requested or default storage class changed and the old storage class is not available anymore. With this change we create new PVCs using the storage class (or the lack of it, i.e. default) of the statefulset. --- controllers/controller_test.go | 5 ++++- controllers/controller_util_test.go | 3 ++- controllers/pvc.go | 2 +- go.mod | 1 + pvc/pvc.go | 25 ++++++++++++++----------- tools.go | 1 + 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/controllers/controller_test.go b/controllers/controller_test.go index 8c59924..02fb2e6 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -1,4 +1,5 @@ -//+build integration +//go:build integration +// +build integration package controllers @@ -20,6 +21,7 @@ import ( rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/pointer" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -94,6 +96,7 @@ func TestController(t *testing.T) { pvc := newSource(ns, "data-test-0", "1G", func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim { pvc.Labels = sts.Spec.Selector.MatchLabels + pvc.Spec.StorageClassName = pointer.String("default-foobar") // Default storage class. Make sure it is not copied return pvc }) require.NoError(c.Create(ctx, pvc)) diff --git a/controllers/controller_util_test.go b/controllers/controller_util_test.go index d005004..a4f82c8 100644 --- a/controllers/controller_util_test.go +++ b/controllers/controller_util_test.go @@ -1,4 +1,5 @@ -//+build integration +//go:build integration +// +build integration package controllers diff --git a/controllers/pvc.go b/controllers/pvc.go index 6a6e928..7d67414 100644 --- a/controllers/pvc.go +++ b/controllers/pvc.go @@ -40,7 +40,7 @@ func filterResizablePVCs(ctx context.Context, sts appsv1.StatefulSet, pvcs []cor } for _, tpl := range sts.Spec.VolumeClaimTemplates { if isPVCTooSmall(ctx, p, tpl, sts.Name) { - res = append(res, pvc.NewEntity(p, tpl.Spec.Resources.Requests[corev1.ResourceStorage])) + res = append(res, pvc.NewEntity(p, tpl.Spec.Resources.Requests[corev1.ResourceStorage], tpl.Spec.StorageClassName)) break } } diff --git a/go.mod b/go.mod index 97556f1..954ffc2 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( k8s.io/api v0.21.3 k8s.io/apimachinery v0.21.3 k8s.io/client-go v0.21.3 + k8s.io/utils v0.0.0-20210722164352-7f3ee0f31471 sigs.k8s.io/controller-runtime v0.9.5 sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20210713022429-8b55f85c90c3 sigs.k8s.io/controller-tools v0.6.2 diff --git a/pvc/pvc.go b/pvc/pvc.go index 61cde52..98fbef1 100644 --- a/pvc/pvc.go +++ b/pvc/pvc.go @@ -14,13 +14,15 @@ import ( const ManagedLabel = "sts-resize.vshn.net/managed" // NewEntity returns a new pvc Info -func NewEntity(pvc corev1.PersistentVolumeClaim, growTo resource.Quantity) Entity { +func NewEntity(pvc corev1.PersistentVolumeClaim, growTo resource.Quantity, storageClassName *string) Entity { + pvc.Spec.StorageClassName = storageClassName return Entity{ - SourceName: pvc.Name, - Namespace: pvc.Namespace, - Labels: pvc.Labels, - TargetSize: growTo, - Spec: pvc.Spec, + SourceName: pvc.Name, + Namespace: pvc.Namespace, + Labels: pvc.Labels, + TargetSize: growTo, + TargetStorageClass: storageClassName, + Spec: pvc.Spec, } } @@ -29,9 +31,10 @@ type Entity struct { Namespace string SourceName string - Labels map[string]string - Spec corev1.PersistentVolumeClaimSpec - TargetSize resource.Quantity + Labels map[string]string + Spec corev1.PersistentVolumeClaimSpec + TargetSize resource.Quantity + TargetStorageClass *string BackedUp bool Restored bool @@ -60,7 +63,7 @@ func (pi Entity) GetBackup() *corev1.PersistentVolumeClaim { Spec: corev1.PersistentVolumeClaimSpec{ AccessModes: pi.Spec.AccessModes, Resources: pi.Spec.Resources, - StorageClassName: pi.Spec.StorageClassName, + StorageClassName: pi.TargetStorageClass, // use target storage class. Old one might not be usable anymore VolumeMode: pi.Spec.VolumeMode, }, } @@ -81,7 +84,7 @@ func (pi Entity) GetResizedSource() *corev1.PersistentVolumeClaim { corev1.ResourceStorage: pi.TargetSize, }, }, - StorageClassName: pi.Spec.StorageClassName, + StorageClassName: pi.TargetStorageClass, VolumeMode: pi.Spec.VolumeMode, }, } diff --git a/tools.go b/tools.go index b5a3872..1c2b5f1 100644 --- a/tools.go +++ b/tools.go @@ -1,3 +1,4 @@ +//go:build tools // +build tools package main