Skip to content

Commit b745d7d

Browse files
torredilConnorJC3
authored andcommitted
Timeout if external-resizer lease is not updated within resyncPeriod
Signed-off-by: torredil <torredil@amazon.com>
1 parent 8054137 commit b745d7d

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
## volume-modifier-for-k8s
22

3-
volume-modifier-for-k8s is a sidecar deployed alongside CSI drivers to enable volume modification through annotations on the PVC.
3+
`volume-modifier-for-k8s` is a sidecar deployed alongside CSI drivers to enable volume modification through annotations on the PVC.
44

5+
## Requirements
6+
7+
Leader election must be enabled in the [external-resizer](https://github.com/kubernetes-csi/external-resizer). This is required in order to efficiently coordinate calls to the EC2 modify-volume API.
58

69
## Security
710

cmd/main.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func main() {
6363
os.Exit(0)
6464
}
6565
klog.Infof("Version : %s", version)
66+
klog.InfoS("Leader election must be enabled in the external-resizer CSI sidecar")
6667

6768
podName := os.Getenv("POD_NAME")
6869
if podName == "" {
@@ -167,26 +168,45 @@ func main() {
167168
func leaseHandler(podName string, mc controller.ModifyController, leaseChannel chan *v1.Lease) {
168169
var cancel context.CancelFunc = nil
169170

170-
for lease := range leaseChannel {
171-
currentLeader := *lease.Spec.HolderIdentity
171+
klog.InfoS("leaseHandler: Looking for external-resizer lease holder")
172172

173-
klog.V(6).InfoS("leaseHandler: Lease updated", "currentLeader", currentLeader, "podName", podName)
173+
timer := time.NewTimer(*resyncPeriod)
174+
defer timer.Stop()
174175

175-
if currentLeader == podName && cancel == nil {
176-
var ctx context.Context
177-
ctx, cancel = context.WithCancel(context.Background())
178-
klog.InfoS("leaseHandler: Starting ModifyController", "podName", podName, "currentLeader", currentLeader)
179-
go mc.Run(*workers, ctx)
180-
} else if currentLeader != podName && cancel != nil {
181-
klog.InfoS("leaseHandler: Stopping ModifyController", "podName", podName, "currentLeader", currentLeader)
182-
cancel()
183-
cancel = nil
184-
}
185-
}
176+
for {
177+
select {
178+
case lease, ok := <-leaseChannel:
179+
if !ok {
180+
if cancel != nil {
181+
cancel()
182+
}
183+
return
184+
}
185+
currentLeader := *lease.Spec.HolderIdentity
186+
klog.V(6).InfoS("leaseHandler: Lease updated", "currentLeader", currentLeader, "podName", podName)
187+
188+
if currentLeader == podName && cancel == nil {
189+
var ctx context.Context
190+
ctx, cancel = context.WithCancel(context.Background())
191+
klog.InfoS("leaseHandler: Starting ModifyController", "podName", podName, "currentLeader", currentLeader)
192+
go mc.Run(*workers, ctx)
193+
} else if currentLeader != podName && cancel != nil {
194+
klog.InfoS("leaseHandler: Stopping ModifyController", "podName", podName, "currentLeader", currentLeader)
195+
cancel()
196+
cancel = nil
197+
}
198+
199+
if !timer.Stop() {
200+
<-timer.C
201+
}
202+
timer.Reset(*resyncPeriod)
186203

187-
// Ensure cancel is called if it's not nil when we exit the function
188-
if cancel != nil {
189-
cancel()
204+
case <-timer.C:
205+
if cancel != nil {
206+
cancel()
207+
}
208+
klog.Fatalf("leaseHandler: No external-resizer lease update received within timeout period", "timeout", *resyncPeriod)
209+
}
190210
}
191211
}
192212

0 commit comments

Comments
 (0)