Skip to content

Commit 0fb322f

Browse files
committed
Make Pod informer registration optional
1 parent 0df1aa7 commit 0fb322f

File tree

3 files changed

+116
-46
lines changed

3 files changed

+116
-46
lines changed

cmd/csi-resizer/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23-
"k8s.io/client-go/util/workqueue"
2423
"os"
2524
"time"
2625

26+
"k8s.io/client-go/util/workqueue"
27+
2728
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
2829
"github.com/kubernetes-csi/external-resizer/pkg/controller"
2930
"github.com/kubernetes-csi/external-resizer/pkg/resizer"
@@ -53,6 +54,8 @@ var (
5354
metricsAddress = flag.String("metrics-address", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.")
5455
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
5556

57+
handleVolumeInUseError = flag.Bool("handle-volume-inuse-error", false, "Flag to turn on capability to handle volume in use error in resizer controller")
58+
5659
version = "unknown"
5760
)
5861

@@ -88,7 +91,7 @@ func main() {
8891
resizerName := csiResizer.Name()
8992
rc := controller.NewResizeController(resizerName, csiResizer, kubeClient, *resyncPeriod, informerFactory,
9093
workqueue.NewItemExponentialFailureRateLimiter(*retryIntervalStart, *retryIntervalMax),
91-
)
94+
*handleVolumeInUseError)
9295
run := func(ctx context.Context) {
9396
informerFactory.Start(wait.NeverStop)
9497
rc.Run(*workers, ctx)

pkg/controller/controller.go

+33-25
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ type resizeController struct {
6262

6363
usedPVCs *inUsePVCStore
6464

65-
podLister corelisters.PodLister
66-
podListerSynced cache.InformerSynced
65+
podLister corelisters.PodLister
66+
podListerSynced cache.InformerSynced
67+
handleVolumeInUseError bool
6768
}
6869

6970
// NewResizeController returns a ResizeController.
@@ -73,13 +74,10 @@ func NewResizeController(
7374
kubeClient kubernetes.Interface,
7475
resyncPeriod time.Duration,
7576
informerFactory informers.SharedInformerFactory,
76-
pvcRateLimiter workqueue.RateLimiter) ResizeController {
77+
pvcRateLimiter workqueue.RateLimiter,
78+
handleVolumeInUseError bool) ResizeController {
7779
pvInformer := informerFactory.Core().V1().PersistentVolumes()
7880
pvcInformer := informerFactory.Core().V1().PersistentVolumeClaims()
79-
80-
// list pods so as we can identify PVC that are in-use
81-
podInformer := informerFactory.Core().V1().Pods()
82-
8381
eventBroadcaster := record.NewBroadcaster()
8482
eventBroadcaster.StartLogging(klog.Infof)
8583
eventBroadcaster.StartRecordingToSink(&corev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events(v1.NamespaceAll)})
@@ -90,18 +88,17 @@ func NewResizeController(
9088
pvcRateLimiter, fmt.Sprintf("%s-pvc", name))
9189

9290
ctrl := &resizeController{
93-
name: name,
94-
resizer: resizer,
95-
kubeClient: kubeClient,
96-
pvLister: pvInformer.Lister(),
97-
pvSynced: pvInformer.Informer().HasSynced,
98-
pvcLister: pvcInformer.Lister(),
99-
pvcSynced: pvcInformer.Informer().HasSynced,
100-
podLister: podInformer.Lister(),
101-
podListerSynced: podInformer.Informer().HasSynced,
102-
claimQueue: claimQueue,
103-
eventRecorder: eventRecorder,
104-
usedPVCs: newUsedPVCStore(),
91+
name: name,
92+
resizer: resizer,
93+
kubeClient: kubeClient,
94+
pvLister: pvInformer.Lister(),
95+
pvSynced: pvInformer.Informer().HasSynced,
96+
pvcLister: pvcInformer.Lister(),
97+
pvcSynced: pvcInformer.Informer().HasSynced,
98+
claimQueue: claimQueue,
99+
eventRecorder: eventRecorder,
100+
usedPVCs: newUsedPVCStore(),
101+
handleVolumeInUseError: handleVolumeInUseError,
105102
}
106103

107104
// Add a resync period as the PVC's request size can be resized again when we handling
@@ -112,11 +109,18 @@ func NewResizeController(
112109
DeleteFunc: ctrl.deletePVC,
113110
}, resyncPeriod)
114111

115-
podInformer.Informer().AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
116-
AddFunc: ctrl.addPod,
117-
DeleteFunc: ctrl.deletePod,
118-
UpdateFunc: ctrl.updatePod,
119-
}, resyncPeriod)
112+
if handleVolumeInUseError {
113+
// list pods so as we can identify PVC that are in-use
114+
klog.Infof("Register Pod informer for resizer %s", ctrl.name)
115+
podInformer := informerFactory.Core().V1().Pods()
116+
ctrl.podLister = podInformer.Lister()
117+
ctrl.podListerSynced = podInformer.Informer().HasSynced
118+
podInformer.Informer().AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
119+
AddFunc: ctrl.addPod,
120+
DeleteFunc: ctrl.deletePod,
121+
UpdateFunc: ctrl.updatePod,
122+
}, resyncPeriod)
123+
}
120124

121125
return ctrl
122126
}
@@ -235,8 +239,12 @@ func (ctrl *resizeController) Run(
235239
defer klog.Infof("Shutting down external resizer %s", ctrl.name)
236240

237241
stopCh := ctx.Done()
242+
informersSyncd := []cache.InformerSynced{ctrl.pvSynced, ctrl.pvcSynced}
243+
if ctrl.handleVolumeInUseError {
244+
informersSyncd = append(informersSyncd, ctrl.podListerSynced)
245+
}
238246

239-
if !cache.WaitForCacheSync(stopCh, ctrl.pvSynced, ctrl.pvcSynced, ctrl.podListerSynced) {
247+
if !cache.WaitForCacheSync(stopCh, informersSyncd...) {
240248
klog.Errorf("Cannot sync pod, pv or pvc caches")
241249
return
242250
}

pkg/controller/controller_test.go

+78-19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
func TestController(t *testing.T) {
2525
blockVolumeMode := v1.PersistentVolumeBlock
2626
fsVolumeMode := v1.PersistentVolumeFilesystem
27+
2728
for _, test := range []struct {
2829
Name string
2930
PVC *v1.PersistentVolumeClaim
@@ -37,7 +38,8 @@ func TestController(t *testing.T) {
3738
// is PVC being expanded in-use
3839
pvcInUse bool
3940
// does PVC being expanded has Failed Precondition errors
40-
pvcHasInUseErrors bool
41+
pvcHasInUseErrors bool
42+
handleVolumeInUseErrorHandler bool
4143
}{
4244
{
4345
Name: "Invalid key",
@@ -87,33 +89,66 @@ func TestController(t *testing.T) {
8789
CallCSIExpand: true,
8890
},
8991
{
90-
Name: "Resize PVC with FS resize",
91-
PVC: createPVC(2, 1),
92-
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
93-
CreateObjects: true,
94-
NodeResize: true,
95-
CallCSIExpand: true,
92+
Name: "Resize PVC with FS resize",
93+
PVC: createPVC(2, 1),
94+
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
95+
CreateObjects: true,
96+
NodeResize: true,
97+
CallCSIExpand: true,
98+
handleVolumeInUseErrorHandler: true,
9699
},
97100
{
98-
Name: "Block Resize PVC with FS resize",
99-
PVC: createPVC(2, 1),
100-
PV: createPV(1, "testPVC", defaultNS, "foobar", &blockVolumeMode),
101-
CreateObjects: true,
102-
NodeResize: true,
103-
CallCSIExpand: true,
104-
expectBlockVolume: true,
101+
Name: "Block Resize PVC with FS resize",
102+
PVC: createPVC(2, 1),
103+
PV: createPV(1, "testPVC", defaultNS, "foobar", &blockVolumeMode),
104+
CreateObjects: true,
105+
NodeResize: true,
106+
CallCSIExpand: true,
107+
expectBlockVolume: true,
108+
handleVolumeInUseErrorHandler: true,
109+
},
110+
{
111+
Name: "Resize PVC, no FS resize, pvc-inuse with failedprecondition",
112+
PVC: createPVC(2, 1),
113+
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
114+
CreateObjects: true,
115+
CallCSIExpand: false,
116+
pvcHasInUseErrors: true,
117+
pvcInUse: true,
118+
handleVolumeInUseErrorHandler: true,
119+
},
120+
{
121+
Name: "Resize PVC, no FS resize, pvc-inuse but no failedprecondition error",
122+
PVC: createPVC(2, 1),
123+
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
124+
CreateObjects: true,
125+
CallCSIExpand: true,
126+
pvcHasInUseErrors: false,
127+
pvcInUse: true,
128+
handleVolumeInUseErrorHandler: true,
129+
},
130+
{
131+
Name: "Resize PVC, no FS resize, pvc not in-use but has failedprecondition error",
132+
PVC: createPVC(2, 1),
133+
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
134+
CreateObjects: true,
135+
CallCSIExpand: true,
136+
pvcHasInUseErrors: true,
137+
pvcInUse: false,
138+
handleVolumeInUseErrorHandler: true,
105139
},
140+
// test cases with volume in use error handling disabled.
106141
{
107-
Name: "Resize PVC, no FS resize, pvc-inuse with failedprecondition",
142+
Name: "Feat disabled, Resize PVC, no FS resize, pvc-inuse with failedprecondition",
108143
PVC: createPVC(2, 1),
109144
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
110145
CreateObjects: true,
111-
CallCSIExpand: false,
146+
CallCSIExpand: true,
112147
pvcHasInUseErrors: true,
113148
pvcInUse: true,
114149
},
115150
{
116-
Name: "Resize PVC, no FS resize, pvc-inuse but no failedprecondition error",
151+
Name: "Feat disabled, Resize PVC, no FS resize, pvc-inuse but no failedprecondition error",
117152
PVC: createPVC(2, 1),
118153
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
119154
CreateObjects: true,
@@ -122,14 +157,38 @@ func TestController(t *testing.T) {
122157
pvcInUse: true,
123158
},
124159
{
125-
Name: "Resize PVC, no FS resize, pvc not in-use but has failedprecondition error",
160+
Name: "Feat disabled, Resize PVC, no FS resize, pvc not in-use but has failedprecondition error",
126161
PVC: createPVC(2, 1),
127162
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
128163
CreateObjects: true,
129164
CallCSIExpand: true,
130165
pvcHasInUseErrors: true,
131166
pvcInUse: false,
132167
},
168+
{
169+
Name: "Feat disabled, Block Resize PVC with FS resize",
170+
PVC: createPVC(2, 1),
171+
PV: createPV(1, "testPVC", defaultNS, "foobar", &blockVolumeMode),
172+
CreateObjects: true,
173+
NodeResize: true,
174+
CallCSIExpand: true,
175+
expectBlockVolume: true,
176+
},
177+
{
178+
Name: "Feat disabled, Resize PVC with FS resize",
179+
PVC: createPVC(2, 1),
180+
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
181+
CreateObjects: true,
182+
NodeResize: true,
183+
CallCSIExpand: true,
184+
},
185+
{
186+
Name: "Feat disabled, Resize PVC, no FS resize",
187+
PVC: createPVC(2, 1),
188+
PV: createPV(1, "testPVC", defaultNS, "foobar", &fsVolumeMode),
189+
CreateObjects: true,
190+
CallCSIExpand: true,
191+
},
133192
} {
134193
client := csi.NewMockClient("mock", test.NodeResize, true, true)
135194
driverName, _ := client.GetDriverName(context.TODO())
@@ -163,7 +222,7 @@ func TestController(t *testing.T) {
163222
t.Fatalf("Test %s: Unable to create resizer: %v", test.Name, err)
164223
}
165224

166-
controller := NewResizeController(driverName, csiResizer, kubeClient, time.Second, informerFactory, workqueue.DefaultControllerRateLimiter())
225+
controller := NewResizeController(driverName, csiResizer, kubeClient, time.Second, informerFactory, workqueue.DefaultControllerRateLimiter(), test.handleVolumeInUseErrorHandler)
167226

168227
ctrlInstance, _ := controller.(*resizeController)
169228

0 commit comments

Comments
 (0)