Skip to content

Commit 4be6c90

Browse files
committed
Use correct context
- Set timeout for each CSI call separately. - Pass ctx to csi-lib-utils function as needed.
1 parent 5a5c9f6 commit 4be6c90

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

cmd/csi-snapshotter/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ func main() {
165165

166166
// Connect to CSI.
167167
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
168+
ctx := context.Background()
168169
csiConn, err := connection.Connect(
170+
ctx,
169171
*csiAddress,
170172
metricsManager,
171173
connection.OnConnectionLoss(connection.ExitOnConnectionLoss()))
@@ -175,11 +177,11 @@ func main() {
175177
}
176178

177179
// Pass a context with a timeout
178-
ctx, cancel := context.WithTimeout(context.Background(), *csiTimeout)
180+
tctx, cancel := context.WithTimeout(ctx, *csiTimeout)
179181
defer cancel()
180182

181183
// Find driver name
182-
driverName, err := csirpc.GetDriverName(ctx, csiConn)
184+
driverName, err := csirpc.GetDriverName(tctx, csiConn)
183185
if err != nil {
184186
klog.Errorf("error getting CSI driver name: %v", err)
185187
os.Exit(1)
@@ -202,13 +204,15 @@ func main() {
202204
}
203205

204206
// Check it's ready
205-
if err = csirpc.ProbeForever(csiConn, *csiTimeout); err != nil {
207+
if err = csirpc.ProbeForever(ctx, csiConn, *csiTimeout); err != nil {
206208
klog.Errorf("error waiting for CSI driver to be ready: %v", err)
207209
os.Exit(1)
208210
}
209211

210212
// Find out if the driver supports create/delete snapshot.
211-
supportsCreateSnapshot, err := supportsControllerCreateSnapshot(ctx, csiConn)
213+
tctx, cancel = context.WithTimeout(ctx, *csiTimeout)
214+
defer cancel()
215+
supportsCreateSnapshot, err := supportsControllerCreateSnapshot(tctx, csiConn)
212216
if err != nil {
213217
klog.Errorf("error determining if driver supports create/delete snapshot operations: %v", err)
214218
os.Exit(1)
@@ -228,7 +232,9 @@ func main() {
228232
snapShotter := snapshotter.NewSnapshotter(csiConn)
229233
var groupSnapshotter group_snapshotter.GroupSnapshotter
230234
if *enableVolumeGroupSnapshots {
231-
supportsCreateVolumeGroupSnapshot, err := supportsGroupControllerCreateVolumeGroupSnapshot(ctx, csiConn)
235+
tctx, cancel = context.WithTimeout(ctx, *csiTimeout)
236+
defer cancel()
237+
supportsCreateVolumeGroupSnapshot, err := supportsGroupControllerCreateVolumeGroupSnapshot(tctx, csiConn)
232238
if err != nil {
233239
klog.Errorf("error determining if driver supports create/delete group snapshot operations: %v", err)
234240
} else if !supportsCreateVolumeGroupSnapshot {

cmd/csi-snapshotter/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func createMockServer(t *testing.T) (*gomock.Controller, *driver.MockCSIDriver,
154154

155155
// Create a client connection to it
156156
addr := drv.Address()
157-
csiConn, err := connection.Connect(addr, metricsManager)
157+
csiConn, err := connection.Connect(context.Background(), addr, metricsManager)
158158
if err != nil {
159159
return nil, nil, nil, nil, nil, err
160160
}

pkg/snapshotter/snapshotter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func createMockServer(t *testing.T) (*gomock.Controller, *driver.MockCSIDriver,
5757

5858
// Create a client connection to it
5959
addr := drv.Address()
60-
csiConn, err := connection.Connect(addr, metricsManager)
60+
csiConn, err := connection.Connect(context.Background(), addr, metricsManager)
6161
if err != nil {
6262
return nil, nil, nil, nil, nil, err
6363
}

0 commit comments

Comments
 (0)