Skip to content

Commit

Permalink
Use storage class requested by statefulset instead of the old PVC
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glrf committed Jun 27, 2023
1 parent f8966cc commit 1cea127
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
5 changes: 4 additions & 1 deletion controllers/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build integration
//go:build integration
// +build integration

package controllers

Expand All @@ -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"
)
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion controllers/controller_util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build integration
//go:build integration
// +build integration

package controllers

Expand Down
2 changes: 1 addition & 1 deletion controllers/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions pvc/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down
1 change: 1 addition & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build tools
// +build tools

package main
Expand Down

0 comments on commit 1cea127

Please sign in to comment.