Skip to content

Commit 26a69fc

Browse files
authored
Merge pull request #496 from gnufied/disable-node-expansion-if-not-required
Disable node expansion if not required
2 parents 4d678b4 + c6de4ce commit 26a69fc

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

pkg/controller/expand_and_recover.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/kubernetes-csi/external-resizer/pkg/util"
2626
v1 "k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/api/resource"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/klog/v2"
2930
)
3031

@@ -199,6 +200,27 @@ func (ctrl *resizeController) expandAndRecover(pvc *v1.PersistentVolumeClaim, pv
199200
return pvc, pv, nil, true
200201
}
201202

203+
func (ctrl *resizeController) removeNodeExpansionNotRequiredAnnotation(pvc *v1.PersistentVolumeClaim) *v1.PersistentVolumeClaim {
204+
if !metav1.HasAnnotation(pvc.ObjectMeta, util.NodeExpansionNotRequired) {
205+
return pvc
206+
}
207+
208+
delete(pvc.Annotations, util.NodeExpansionNotRequired)
209+
return pvc
210+
}
211+
212+
func (ctrl *resizeController) addNodeExpansionNotRequiredAnnotation(pvc *v1.PersistentVolumeClaim) *v1.PersistentVolumeClaim {
213+
if metav1.HasAnnotation(pvc.ObjectMeta, util.NodeExpansionNotRequired) {
214+
return pvc
215+
}
216+
217+
if pvc.Annotations == nil {
218+
pvc.Annotations = make(map[string]string)
219+
}
220+
pvc.Annotations[util.NodeExpansionNotRequired] = "true"
221+
return pvc
222+
}
223+
202224
func (ctrl *resizeController) markForSlowRetry(pvcKey string, resizeStatus v1.ClaimResourceStatus) {
203225
if resizeStatus == v1.PersistentVolumeClaimControllerResizeInfeasible {
204226
ctrl.slowSet.Add(pvcKey, slowset.ObjectData{

pkg/controller/expand_and_recover_test.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"github.com/kubernetes-csi/external-resizer/pkg/features"
1010
"github.com/kubernetes-csi/external-resizer/pkg/resizer"
1111
"github.com/kubernetes-csi/external-resizer/pkg/testutil"
12+
"github.com/kubernetes-csi/external-resizer/pkg/util"
1213
"google.golang.org/grpc/codes"
1314
"google.golang.org/grpc/status"
1415
v1 "k8s.io/api/core/v1"
1516
"k8s.io/apimachinery/pkg/api/resource"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1618
"k8s.io/apimachinery/pkg/runtime"
1719
"k8s.io/apimachinery/pkg/util/sets"
1820
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -29,21 +31,24 @@ func TestExpandAndRecover(t *testing.T) {
2931
pv *v1.PersistentVolume
3032
disableNodeExpansion bool
3133
disableControllerExpansion bool
34+
3235
// expectations of test
33-
expectedResizeStatus v1.ClaimResourceStatus
34-
expectedAllocatedSize resource.Quantity
35-
pvcWithFinalErrors sets.Set[string]
36-
expansionError error
37-
expectResizeCall bool
38-
expectedConditions []v1.PersistentVolumeClaimConditionType
36+
expectedResizeStatus v1.ClaimResourceStatus
37+
expectedAllocatedSize resource.Quantity
38+
expectNodeExpansionNotRequiredAnnotation bool
39+
pvcWithFinalErrors sets.Set[string]
40+
expansionError error
41+
expectResizeCall bool
42+
expectedConditions []v1.PersistentVolumeClaimConditionType
3943
}{
4044
{
41-
name: "pvc.spec.size > pv.spec.size, resize_status=node_expansion_inprogress",
42-
pvc: testutil.GetTestPVC("test-vol0", "2G", "1G", "", ""),
43-
pv: createPV(1, "claim01", defaultNS, "test-uid", &fsVolumeMode),
44-
expectedResizeStatus: v1.PersistentVolumeClaimNodeResizePending,
45-
expectedAllocatedSize: resource.MustParse("2G"),
46-
expectResizeCall: true,
45+
name: "pvc.spec.size > pv.spec.size, resize_status=node_expansion_inprogress",
46+
pvc: testutil.GetTestPVC("test-vol0", "2G", "1G", "", ""),
47+
pv: createPV(1, "claim01", defaultNS, "test-uid", &fsVolumeMode),
48+
expectedResizeStatus: v1.PersistentVolumeClaimNodeResizePending,
49+
expectNodeExpansionNotRequiredAnnotation: false,
50+
expectedAllocatedSize: resource.MustParse("2G"),
51+
expectResizeCall: true,
4752
},
4853
{
4954
name: "pvc.spec.size = pv.spec.size, resize_status=no_expansion_inprogress",
@@ -116,13 +121,14 @@ func TestExpandAndRecover(t *testing.T) {
116121
expectResizeCall: false,
117122
},
118123
{
119-
name: "pvc.spec.size > pv.spec.size, disable_node_expansion=true, resize_status=no_expansion_inprogress",
120-
pvc: testutil.GetTestPVC("test-vol0", "2G", "1G", "", ""),
121-
pv: createPV(1, "claim01", defaultNS, "test-uid", &fsVolumeMode),
122-
disableNodeExpansion: true,
123-
expectedResizeStatus: "",
124-
expectedAllocatedSize: resource.MustParse("2G"),
125-
expectResizeCall: true,
124+
name: "pvc.spec.size > pv.spec.size, disable_node_expansion=true, resize_status=no_expansion_inprogress",
125+
pvc: testutil.GetTestPVC("test-vol0", "2G", "1G", "", ""),
126+
pv: createPV(1, "claim01", defaultNS, "test-uid", &fsVolumeMode),
127+
disableNodeExpansion: true,
128+
expectedResizeStatus: "",
129+
expectNodeExpansionNotRequiredAnnotation: true,
130+
expectedAllocatedSize: resource.MustParse("2G"),
131+
expectResizeCall: true,
126132
},
127133
{
128134
name: "pv.spec.size >= pvc.spec.size, resize_status=node_expansion_failed",
@@ -193,6 +199,10 @@ func TestExpandAndRecover(t *testing.T) {
193199
t.Fatalf("expected resize status to be %s, got %s", test.expectedResizeStatus, actualResizeStatus)
194200
}
195201

202+
if test.expectNodeExpansionNotRequiredAnnotation != metav1.HasAnnotation(pvc.ObjectMeta, util.NodeExpansionNotRequired) {
203+
t.Fatalf("expected node expansion not required annotation to be %t, got %t", test.expectNodeExpansionNotRequiredAnnotation, metav1.HasAnnotation(pvc.ObjectMeta, util.NodeExpansionNotRequired))
204+
}
205+
196206
actualAllocatedSize := pvc.Status.AllocatedResources.Storage()
197207

198208
if test.expectedAllocatedSize.Cmp(*actualAllocatedSize) != 0 {

pkg/controller/resize_status.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func (ctrl *resizeController) markControllerResizeInProgress(
4747
newPVC.Status.Conditions = util.MergeResizeConditionsOfPVC(newPVC.Status.Conditions, conditions, true /*keepOldResizeConditions*/)
4848
}
4949

50+
newPVC = ctrl.removeNodeExpansionNotRequiredAnnotation(newPVC)
51+
5052
if updateStatus {
5153
newPVC = mergeStorageResourceStatus(newPVC, v1.PersistentVolumeClaimControllerResizeInProgress)
5254
}
@@ -78,6 +80,9 @@ func (ctrl *resizeController) markForPendingNodeExpansion(pvc *v1.PersistentVolu
7880
newPVC.Status.Conditions = util.MergeResizeConditionsOfPVC(newPVC.Status.Conditions,
7981
[]v1.PersistentVolumeClaimCondition{pvcCondition}, true /*keepOldResizeConditions*/)
8082

83+
// make sure if any annotation was previously added is removed here
84+
newPVC = ctrl.removeNodeExpansionNotRequiredAnnotation(newPVC)
85+
8186
newPVC = mergeStorageResourceStatus(newPVC, v1.PersistentVolumeClaimNodeResizePending)
8287
updatedPVC, err := util.PatchClaim(ctrl.kubeClient, pvc, newPVC, true /* addResourceVersionCheck */)
8388

@@ -173,6 +178,9 @@ func (ctrl *resizeController) markOverallExpansionAsFinished(
173178
newPVC.Status.AllocatedResourceStatuses = resourceStatusMap
174179
}
175180

181+
// this will ensure that kubelet does not try to resize volume again
182+
newPVC = ctrl.addNodeExpansionNotRequiredAnnotation(newPVC)
183+
176184
updatedPVC, err := util.PatchClaim(ctrl.kubeClient, pvc, newPVC, true /* addResourceVersionCheck */)
177185
if err != nil {
178186
return pvc, fmt.Errorf("mark PVC %q as resize finished failed: %v", klog.KObj(pvc), err)

pkg/util/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ var (
5353
// Its value will be set by the external-resizer when it deems that filesystem resize is required after resizing volume.
5454
// Its value will be used by pv_controller to determine pvc's status capacity when binding pvc and pv.
5555
AnnPreResizeCapacity = "volume.alpha.kubernetes.io/pre-resize-capacity"
56+
57+
NodeExpansionNotRequired = "volume.kubernetes.io/node-expansion-not-required"
5658
)
5759

5860
// MergeResizeConditionsOfPVC updates pvc with requested resize conditions

0 commit comments

Comments
 (0)