Skip to content

Commit 1cea127

Browse files
committed
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.
1 parent f8966cc commit 1cea127

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

controllers/controller_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//+build integration
1+
//go:build integration
2+
// +build integration
23

34
package controllers
45

@@ -20,6 +21,7 @@ import (
2021
rbacv1 "k8s.io/api/rbac/v1"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/utils/pointer"
2325
ctrl "sigs.k8s.io/controller-runtime"
2426
"sigs.k8s.io/controller-runtime/pkg/client"
2527
)
@@ -94,6 +96,7 @@ func TestController(t *testing.T) {
9496
pvc := newSource(ns, "data-test-0", "1G",
9597
func(pvc *corev1.PersistentVolumeClaim) *corev1.PersistentVolumeClaim {
9698
pvc.Labels = sts.Spec.Selector.MatchLabels
99+
pvc.Spec.StorageClassName = pointer.String("default-foobar") // Default storage class. Make sure it is not copied
97100
return pvc
98101
})
99102
require.NoError(c.Create(ctx, pvc))

controllers/controller_util_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//+build integration
1+
//go:build integration
2+
// +build integration
23

34
package controllers
45

controllers/pvc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func filterResizablePVCs(ctx context.Context, sts appsv1.StatefulSet, pvcs []cor
4040
}
4141
for _, tpl := range sts.Spec.VolumeClaimTemplates {
4242
if isPVCTooSmall(ctx, p, tpl, sts.Name) {
43-
res = append(res, pvc.NewEntity(p, tpl.Spec.Resources.Requests[corev1.ResourceStorage]))
43+
res = append(res, pvc.NewEntity(p, tpl.Spec.Resources.Requests[corev1.ResourceStorage], tpl.Spec.StorageClassName))
4444
break
4545
}
4646
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
k8s.io/api v0.21.3
88
k8s.io/apimachinery v0.21.3
99
k8s.io/client-go v0.21.3
10+
k8s.io/utils v0.0.0-20210722164352-7f3ee0f31471
1011
sigs.k8s.io/controller-runtime v0.9.5
1112
sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20210713022429-8b55f85c90c3
1213
sigs.k8s.io/controller-tools v0.6.2

pvc/pvc.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import (
1414
const ManagedLabel = "sts-resize.vshn.net/managed"
1515

1616
// NewEntity returns a new pvc Info
17-
func NewEntity(pvc corev1.PersistentVolumeClaim, growTo resource.Quantity) Entity {
17+
func NewEntity(pvc corev1.PersistentVolumeClaim, growTo resource.Quantity, storageClassName *string) Entity {
18+
pvc.Spec.StorageClassName = storageClassName
1819
return Entity{
19-
SourceName: pvc.Name,
20-
Namespace: pvc.Namespace,
21-
Labels: pvc.Labels,
22-
TargetSize: growTo,
23-
Spec: pvc.Spec,
20+
SourceName: pvc.Name,
21+
Namespace: pvc.Namespace,
22+
Labels: pvc.Labels,
23+
TargetSize: growTo,
24+
TargetStorageClass: storageClassName,
25+
Spec: pvc.Spec,
2426
}
2527
}
2628

@@ -29,9 +31,10 @@ type Entity struct {
2931
Namespace string
3032
SourceName string
3133

32-
Labels map[string]string
33-
Spec corev1.PersistentVolumeClaimSpec
34-
TargetSize resource.Quantity
34+
Labels map[string]string
35+
Spec corev1.PersistentVolumeClaimSpec
36+
TargetSize resource.Quantity
37+
TargetStorageClass *string
3538

3639
BackedUp bool
3740
Restored bool
@@ -60,7 +63,7 @@ func (pi Entity) GetBackup() *corev1.PersistentVolumeClaim {
6063
Spec: corev1.PersistentVolumeClaimSpec{
6164
AccessModes: pi.Spec.AccessModes,
6265
Resources: pi.Spec.Resources,
63-
StorageClassName: pi.Spec.StorageClassName,
66+
StorageClassName: pi.TargetStorageClass, // use target storage class. Old one might not be usable anymore
6467
VolumeMode: pi.Spec.VolumeMode,
6568
},
6669
}
@@ -81,7 +84,7 @@ func (pi Entity) GetResizedSource() *corev1.PersistentVolumeClaim {
8184
corev1.ResourceStorage: pi.TargetSize,
8285
},
8386
},
84-
StorageClassName: pi.Spec.StorageClassName,
87+
StorageClassName: pi.TargetStorageClass,
8588
VolumeMode: pi.Spec.VolumeMode,
8689
},
8790
}

tools.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build tools
12
// +build tools
23

34
package main

0 commit comments

Comments
 (0)