Skip to content

Commit ff2e19e

Browse files
Merge pull request #24 from Rakshith-R/BZ-2048370
Bug 2048370: CSI-Addons controller makes node reclaimspace request even when the PVC is not mounted to any pod
2 parents 0f5af0b + 45faaee commit ff2e19e

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

controllers/reclaimspacejob_controller.go

+28-16
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ type targetDetails struct {
173173
nodeID string
174174
}
175175

176+
// canNodeReclaimSpace returns true if nodeID is not empty,
177+
// indicating volume is mounted to a pod and node reclaimspace
178+
// request can be sent.
179+
func (td *targetDetails) canNodeReclaimSpace() bool {
180+
return td.nodeID != ""
181+
}
182+
176183
// reconcile performs time based validation, fetches required details and makes
177184
// grpc request for controller and node reclaim space operation.
178185
func (r *ReclaimSpaceJobReconciler) reconcile(
@@ -211,15 +218,22 @@ func (r *ReclaimSpaceJobReconciler) reconcile(
211218
return err
212219
}
213220

214-
nodeFound, nodeReclaimedSpace, err := r.nodeReclaimSpace(ctx, logger, target)
215-
if err != nil {
216-
logger.Error(err, "Failed to make node request")
217-
setFailedCondition(
218-
&rsJob.Status.Conditions,
219-
fmt.Sprintf("Failed to make node request: %v", util.GetErrorMessage(err)),
220-
rsJob.Generation)
221-
222-
return err
221+
var (
222+
nodeFound = false
223+
nodeReclaimedSpace *int64
224+
)
225+
if target.canNodeReclaimSpace() {
226+
nodeFound = true
227+
nodeReclaimedSpace, err = r.nodeReclaimSpace(ctx, logger, target)
228+
if err != nil {
229+
logger.Error(err, "Failed to make node request")
230+
setFailedCondition(
231+
&rsJob.Status.Conditions,
232+
fmt.Sprintf("Failed to make node request: %v", util.GetErrorMessage(err)),
233+
rsJob.Generation)
234+
235+
return err
236+
}
223237
}
224238

225239
controllerFound, controllerReclaimedSpace, err := r.controllerReclaimSpace(ctx, logger, target)
@@ -366,23 +380,21 @@ func (r *ReclaimSpaceJobReconciler) controllerReclaimSpace(
366380
return true, calculateReclaimedSpace(resp.PreUsage, resp.PostUsage), nil
367381
}
368382

369-
// controllerReclaimSpace makes node reclaim space request if node client is found
383+
// nodeReclaimSpace makes node reclaim space request if node client is found
370384
// and returns amount of reclaimed space.
371385
// This function returns
372-
// - boolean to indicate client was found or not
373386
// - pointer to int64 indicating amount of reclaimed space, it is nil if not available
374387
// - error
375388
func (r *ReclaimSpaceJobReconciler) nodeReclaimSpace(
376389
ctx context.Context,
377390
logger *logr.Logger,
378-
target *targetDetails) (bool, *int64, error) {
391+
target *targetDetails) (*int64, error) {
379392
clientName, nodeClient := r.getRSClientWithCap(
380393
target.driverName,
381394
target.nodeID,
382395
identity.Capability_ReclaimSpace_ONLINE)
383396
if nodeClient == nil {
384-
logger.Info("Node Client not found")
385-
return false, nil, nil
397+
return nil, errors.New("node Client not found")
386398
}
387399
*logger = logger.WithValues("nodeClient", clientName)
388400

@@ -394,10 +406,10 @@ func (r *ReclaimSpaceJobReconciler) nodeReclaimSpace(
394406
defer cancel()
395407
resp, err := nodeClient.NodeReclaimSpace(newCtx, req)
396408
if err != nil {
397-
return true, nil, err
409+
return nil, err
398410
}
399411

400-
return true, calculateReclaimedSpace(resp.PreUsage, resp.PostUsage), nil
412+
return calculateReclaimedSpace(resp.PreUsage, resp.PostUsage), nil
401413
}
402414

403415
// calculateReclaimedSpace returns amount of reclaimed space.

controllers/reclaimspacejob_controller_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,36 @@ func TestCalculateReclaimedSpace(t *testing.T) {
176176
})
177177
}
178178
}
179+
180+
func TestCanNodeReclaimSpace(t *testing.T) {
181+
tests := []struct {
182+
name string
183+
td targetDetails
184+
want bool
185+
}{
186+
{
187+
name: "empty nodeID",
188+
td: targetDetails{
189+
driverName: "csi.example.com",
190+
pvName: "pvc-a8a5c531-9f88-4fc8-b35d-564585fb42a8",
191+
nodeID: "",
192+
},
193+
want: false,
194+
},
195+
{
196+
name: "non-empty nodeID",
197+
td: targetDetails{
198+
driverName: "csi.example.com",
199+
pvName: "pvc-a8a5c531-9f88-4fc8-b35d-564585fb42a8",
200+
nodeID: "worker-1",
201+
},
202+
want: true,
203+
},
204+
}
205+
for _, tt := range tests {
206+
t.Run(tt.name, func(t *testing.T) {
207+
got := tt.td.canNodeReclaimSpace()
208+
assert.Equal(t, tt.want, got)
209+
})
210+
}
211+
}

0 commit comments

Comments
 (0)