Skip to content

Commit 38e5ebf

Browse files
committed
Fix code to handle pod updates
1 parent f7cf14d commit 38e5ebf

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

pkg/controller/controller.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func NewResizeController(
115115
podInformer.Informer().AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
116116
AddFunc: ctrl.addPod,
117117
DeleteFunc: ctrl.deletePod,
118+
UpdateFunc: ctrl.updatePod,
118119
}, resyncPeriod)
119120

120121
return ctrl
@@ -130,20 +131,29 @@ func (ctrl *resizeController) addPVC(obj interface{}) {
130131

131132
func (ctrl *resizeController) addPod(obj interface{}) {
132133
pod := parsePod(obj)
133-
if pod != nil {
134+
if pod == nil {
134135
return
135136
}
136137
ctrl.usedPVCs.addPod(pod)
137138
}
138139

139140
func (ctrl *resizeController) deletePod(obj interface{}) {
140141
pod := parsePod(obj)
141-
if pod != nil {
142+
if pod == nil {
142143
return
143144
}
144145
ctrl.usedPVCs.removePod(pod)
145146
}
146147

148+
func (ctrl *resizeController) updatePod(oldObj, newObj interface{}) {
149+
pod := parsePod(newObj)
150+
if pod == nil {
151+
return
152+
}
153+
154+
ctrl.usedPVCs.addPod(pod)
155+
}
156+
147157
func (ctrl *resizeController) updatePVC(oldObj, newObj interface{}) {
148158
oldPVC, ok := oldObj.(*v1.PersistentVolumeClaim)
149159
if !ok || oldPVC == nil {
@@ -361,9 +371,9 @@ func (ctrl *resizeController) resizePVC(pvc *v1.PersistentVolumeClaim, pv *v1.Pe
361371
// we must not try expansion here
362372
if ctrl.usedPVCs.hasInUseErrors(pvc) && ctrl.usedPVCs.checkForUse(pvc) {
363373
// Record an event to indicate that resizer is not expanding the pvc
364-
ctrl.eventRecorder.Event(pvc, v1.EventTypeWarning, util.VolumeResizeFailed,
365-
fmt.Sprintf("CSI resizer is not expanding %s because it is in-use", pv.Name))
366-
return fmt.Errorf("csi resizer is not expanding %s because it is in-use", pv.Name)
374+
msg := fmt.Sprintf("Unable to expand %s because CSI driver %s only supports offline expansion and volume is currently in-use", util.PVCKey(pvc), ctrl.resizer.Name())
375+
ctrl.eventRecorder.Event(pvc, v1.EventTypeWarning, util.VolumeResizeFailed, msg)
376+
return fmt.Errorf(msg)
367377
}
368378

369379
// Record an event to indicate that external resizer is resizing this volume.
@@ -485,14 +495,14 @@ func parsePod(obj interface{}) *v1.Pod {
485495
}
486496
pod, ok := obj.(*v1.Pod)
487497
if !ok {
488-
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
498+
staleObj, ok := obj.(cache.DeletedFinalStateUnknown)
489499
if !ok {
490-
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %#v", obj))
500+
utilruntime.HandleError(fmt.Errorf("couldn't get stale object %#v", obj))
491501
return nil
492502
}
493-
pod, ok = tombstone.Obj.(*v1.Pod)
503+
pod, ok = staleObj.Obj.(*v1.Pod)
494504
if !ok {
495-
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a Pod %#v", obj))
505+
utilruntime.HandleError(fmt.Errorf("stale object is not a Pod %#v", obj))
496506
return nil
497507
}
498508
}
@@ -507,6 +517,7 @@ func inUseError(err error) bool {
507517
}
508518
// if this is a failed precondition error then that means driver does not support expansion
509519
// of in-use volumes
520+
// More info - https://github.com/container-storage-interface/spec/blob/master/spec.md#controllerexpandvolume-errors
510521
if st.Code() == codes.FailedPrecondition {
511522
return true
512523
}

pkg/controller/controller_test.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controller
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76
"time"
87

@@ -34,7 +33,6 @@ func TestController(t *testing.T) {
3433
NodeResize bool
3534
CallCSIExpand bool
3635
expectBlockVolume bool
37-
expectError bool
3836

3937
// is PVC being expanded in-use
4038
pvcInUse bool
@@ -113,7 +111,6 @@ func TestController(t *testing.T) {
113111
CallCSIExpand: false,
114112
pvcHasInUseErrors: true,
115113
pvcInUse: true,
116-
expectError: true,
117114
},
118115
{
119116
Name: "Resize PVC, no FS resize, pvc-inuse but no failedprecondition error",
@@ -148,20 +145,15 @@ func TestController(t *testing.T) {
148145
}
149146
}
150147

148+
if test.pvcInUse {
149+
pod := withPVC(test.PVC.Name, pod())
150+
initialObjects = append(initialObjects, pod)
151+
}
152+
151153
kubeClient, informerFactory := fakeK8s(initialObjects)
152154
pvInformer := informerFactory.Core().V1().PersistentVolumes()
153155
pvcInformer := informerFactory.Core().V1().PersistentVolumeClaims()
154-
155-
for _, obj := range initialObjects {
156-
switch obj.(type) {
157-
case *v1.PersistentVolume:
158-
pvInformer.Informer().GetStore().Add(obj)
159-
case *v1.PersistentVolumeClaim:
160-
pvcInformer.Informer().GetStore().Add(obj)
161-
default:
162-
t.Fatalf("Test %s: Unknown initalObject type: %+v", test.Name, obj)
163-
}
164-
}
156+
podInformer := informerFactory.Core().V1().Pods()
165157

166158
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
167159
metricsAddress := ""
@@ -182,23 +174,28 @@ func TestController(t *testing.T) {
182174
}
183175
}
184176

185-
if test.pvcInUse {
186-
pod := withPVC(test.PVC.Name, pod())
187-
ctrlInstance.usedPVCs.addPod(pod)
188-
if !ctrlInstance.usedPVCs.checkForUse(test.PVC) {
189-
t.Fatalf("pvc %s is not in use", test.PVC.Name)
190-
}
191-
}
177+
stopCh := make(chan struct{})
178+
informerFactory.Start(stopCh)
192179

193-
err = ctrlInstance.syncPVC(fmt.Sprintf("%s/%s", test.PVC.Namespace, test.PVC.Name))
194-
if err != nil && !test.expectError {
195-
t.Fatalf("Test %s: Unexpected error: %v", test.Name, err)
196-
}
180+
ctx := context.TODO()
181+
defer ctx.Done()
182+
go controller.Run(1, ctx)
197183

198-
if test.expectError && err == nil {
199-
t.Fatalf("Test %s: expected error got no none", test.Name)
184+
for _, obj := range initialObjects {
185+
switch obj.(type) {
186+
case *v1.PersistentVolume:
187+
pvInformer.Informer().GetStore().Add(obj)
188+
case *v1.PersistentVolumeClaim:
189+
pvcInformer.Informer().GetStore().Add(obj)
190+
case *v1.Pod:
191+
podInformer.Informer().GetStore().Add(obj)
192+
default:
193+
t.Fatalf("Test %s: Unknown initalObject type: %+v", test.Name, obj)
194+
}
200195
}
201196

197+
time.Sleep(time.Second * 2)
198+
202199
expandCallCount := client.GetExpandCount()
203200
if test.CallCSIExpand && expandCallCount == 0 {
204201
t.Fatalf("for %s: expected csi expand call, no csi expand call was made", test.Name)

0 commit comments

Comments
 (0)