Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller/reclaimspacejob: skip making node req if nodeID is empty #104

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions controllers/reclaimspacejob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ type targetDetails struct {
nodeID string
}

// canNodeReclaimSpace returns true if nodeID is not empty,
// indicating volume is mounted to a pod and node reclaimspace
// request can be sent.
func (td *targetDetails) canNodeReclaimSpace() bool {
return td.nodeID != ""
}

// reconcile performs time based validation, fetches required details and makes
// grpc request for controller and node reclaim space operation.
func (r *ReclaimSpaceJobReconciler) reconcile(
Expand Down Expand Up @@ -211,15 +218,22 @@ func (r *ReclaimSpaceJobReconciler) reconcile(
return err
}

nodeFound, nodeReclaimedSpace, err := r.nodeReclaimSpace(ctx, logger, target)
if err != nil {
logger.Error(err, "Failed to make node request")
setFailedCondition(
&rsJob.Status.Conditions,
fmt.Sprintf("Failed to make node request: %v", util.GetErrorMessage(err)),
rsJob.Generation)

return err
var (
nodeFound = false
nodeReclaimedSpace *int64
)
if target.canNodeReclaimSpace() {
nodeFound = true
nodeReclaimedSpace, err = r.nodeReclaimSpace(ctx, logger, target)
if err != nil {
logger.Error(err, "Failed to make node request")
setFailedCondition(
&rsJob.Status.Conditions,
fmt.Sprintf("Failed to make node request: %v", util.GetErrorMessage(err)),
rsJob.Generation)

return err
}
}

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

// controllerReclaimSpace makes node reclaim space request if node client is found
// nodeReclaimSpace makes node reclaim space request if node client is found
// and returns amount of reclaimed space.
// This function returns
// - boolean to indicate client was found or not
// - pointer to int64 indicating amount of reclaimed space, it is nil if not available
// - error
func (r *ReclaimSpaceJobReconciler) nodeReclaimSpace(
ctx context.Context,
logger *logr.Logger,
target *targetDetails) (bool, *int64, error) {
target *targetDetails) (*int64, error) {
clientName, nodeClient := r.getRSClientWithCap(
target.driverName,
target.nodeID,
identity.Capability_ReclaimSpace_ONLINE)
if nodeClient == nil {
logger.Info("Node Client not found")
return false, nil, nil
return nil, errors.New("node Client not found")
}
*logger = logger.WithValues("nodeClient", clientName)

Expand All @@ -394,10 +406,10 @@ func (r *ReclaimSpaceJobReconciler) nodeReclaimSpace(
defer cancel()
resp, err := nodeClient.NodeReclaimSpace(newCtx, req)
if err != nil {
return true, nil, err
return nil, err
}

return true, calculateReclaimedSpace(resp.PreUsage, resp.PostUsage), nil
return calculateReclaimedSpace(resp.PreUsage, resp.PostUsage), nil
}

// calculateReclaimedSpace returns amount of reclaimed space.
Expand Down
33 changes: 33 additions & 0 deletions controllers/reclaimspacejob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,36 @@ func TestCalculateReclaimedSpace(t *testing.T) {
})
}
}

func TestCanNodeReclaimSpace(t *testing.T) {
tests := []struct {
name string
td targetDetails
want bool
}{
{
name: "empty nodeID",
td: targetDetails{
driverName: "csi.example.com",
pvName: "pvc-a8a5c531-9f88-4fc8-b35d-564585fb42a8",
nodeID: "",
},
want: false,
},
{
name: "non-empty nodeID",
td: targetDetails{
driverName: "csi.example.com",
pvName: "pvc-a8a5c531-9f88-4fc8-b35d-564585fb42a8",
nodeID: "worker-1",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.td.canNodeReclaimSpace()
assert.Equal(t, tt.want, got)
})
}
}