Skip to content

Commit d644860

Browse files
committed
check usage when VolumeCondition is not supported
The Usage and VolumeCondition are both optional in the response and kubelet need to consider returning metrics if either one is set.
1 parent de6db3f commit d644860

File tree

3 files changed

+80
-32
lines changed

3 files changed

+80
-32
lines changed

pkg/volume/csi/csi_client.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,7 @@ func (c *csiDriverClient) NodeGetVolumeStats(ctx context.Context, volID string,
612612
if err != nil {
613613
return nil, err
614614
}
615-
usages := resp.GetUsage()
616-
if usages == nil {
617-
return nil, fmt.Errorf("failed to get usage from response. usage is nil")
618-
}
615+
619616
metrics := &volume.Metrics{
620617
Used: resource.NewQuantity(int64(0), resource.BinarySI),
621618
Capacity: resource.NewQuantity(int64(0), resource.BinarySI),
@@ -625,8 +622,9 @@ func (c *csiDriverClient) NodeGetVolumeStats(ctx context.Context, volID string,
625622
InodesFree: resource.NewQuantity(int64(0), resource.BinarySI),
626623
}
627624

625+
var isSupportNodeVolumeCondition bool
628626
if utilfeature.DefaultFeatureGate.Enabled(features.CSIVolumeHealth) {
629-
isSupportNodeVolumeCondition, err := c.nodeSupportsVolumeCondition(ctx)
627+
isSupportNodeVolumeCondition, err = c.nodeSupportsVolumeCondition(ctx)
630628
if err != nil {
631629
return nil, err
632630
}
@@ -637,6 +635,12 @@ func (c *csiDriverClient) NodeGetVolumeStats(ctx context.Context, volID string,
637635
}
638636
}
639637

638+
usages := resp.GetUsage()
639+
// If the driver does not support volume condition and usages is nil, return an error
640+
if !isSupportNodeVolumeCondition && usages == nil {
641+
return nil, fmt.Errorf("failed to get usage from response. usage is nil")
642+
}
643+
640644
for _, usage := range usages {
641645
if usage == nil {
642646
continue

pkg/volume/csi/csi_client_test.go

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func newFakeCsiDriverClientWithVolumeStats(t *testing.T, volumeStatsSet bool) *f
6666
}
6767
}
6868

69-
func newFakeCsiDriverClientWithVolumeStatsAndCondition(t *testing.T, volumeStatsSet, volumeConditionSet bool) *fakeCsiDriverClient {
69+
func newFakeCsiDriverClientWithVolumeStatsAndCondition(t *testing.T, volumeStatsSet, volumeConditionSet, setVolumeStat, setVolumeCondition bool) *fakeCsiDriverClient {
7070
return &fakeCsiDriverClient{
7171
t: t,
72-
nodeClient: fake.NewNodeClientWithVolumeStatsAndCondition(volumeStatsSet, volumeConditionSet),
72+
nodeClient: fake.NewNodeClientWithVolumeStatsAndCondition(volumeStatsSet, volumeConditionSet, setVolumeStat, setVolumeCondition),
7373
}
7474
}
7575

@@ -100,18 +100,23 @@ func (c *fakeCsiDriverClient) NodeGetVolumeStats(ctx context.Context, volID stri
100100
VolumeId: volID,
101101
VolumePath: targetPath,
102102
}
103+
fakeResp := &csipbv1.NodeGetVolumeStatsResponse{}
104+
if c.nodeClient.SetVolumeStats {
105+
fakeResp = getRawVolumeInfo()
106+
}
107+
if c.nodeClient.SetVolumecondition {
108+
fakeResp.VolumeCondition = &csipbv1.VolumeCondition{
109+
Abnormal: true,
110+
Message: "Volume is abnormal",
111+
}
112+
}
113+
c.nodeClient.SetNodeVolumeStatsResp(fakeResp)
103114

104-
c.nodeClient.SetNodeVolumeStatsResp(getRawVolumeInfo())
105115
resp, err := c.nodeClient.NodeGetVolumeStats(ctx, req)
106116
if err != nil {
107117
return nil, err
108118
}
109119

110-
usages := resp.GetUsage()
111-
if usages == nil {
112-
return nil, nil
113-
}
114-
115120
metrics := &volume.Metrics{}
116121

117122
isSupportNodeVolumeCondition, err := c.nodeSupportsVolumeCondition(ctx)
@@ -124,6 +129,10 @@ func (c *fakeCsiDriverClient) NodeGetVolumeStats(ctx context.Context, volID stri
124129
metrics.Abnormal, metrics.Message = &abnormal, &message
125130
}
126131

132+
usages := resp.GetUsage()
133+
if !isSupportNodeVolumeCondition && usages == nil {
134+
return nil, errors.New("volume usage is nil")
135+
}
127136
for _, usage := range usages {
128137
if usage == nil {
129138
continue
@@ -377,8 +386,8 @@ func setupClientWithExpansion(t *testing.T, stageUnstageSet bool, expansionSet b
377386
return newFakeCsiDriverClientWithExpansion(t, stageUnstageSet, expansionSet)
378387
}
379388

380-
func setupClientWithVolumeStatsAndCondition(t *testing.T, volumeStatsSet, volumeConditionSet bool) csiClient {
381-
return newFakeCsiDriverClientWithVolumeStatsAndCondition(t, volumeStatsSet, volumeConditionSet)
389+
func setupClientWithVolumeStatsAndCondition(t *testing.T, volumeStatsSet, volumeConditionSet, setVolumeStat, setVolumecondition bool) csiClient {
390+
return newFakeCsiDriverClientWithVolumeStatsAndCondition(t, volumeStatsSet, volumeConditionSet, setVolumeStat, setVolumecondition)
382391
}
383392

384393
func setupClientWithVolumeStats(t *testing.T, volumeStatsSet bool) csiClient {
@@ -839,13 +848,17 @@ func TestVolumeHealthEnable(t *testing.T) {
839848
tests := []struct {
840849
name string
841850
volumeStatsSet bool
851+
setVolumeStat bool
852+
setVolumecondition bool
842853
volumeConditionSet bool
843854
volumeData VolumeStatsOptions
844855
success bool
845856
}{
846857
{
847-
name: "when nodeVolumeStats=on, VolumeID=on, DeviceMountPath=on, VolumeCondition=on",
858+
name: "when nodeVolumeStats=on, volumeStatsSet=on, setVolumeStat=on, volumeCondition=on, setVolumecondition=on",
848859
volumeStatsSet: true,
860+
setVolumeStat: true,
861+
setVolumecondition: true,
849862
volumeConditionSet: true,
850863
volumeData: VolumeStatsOptions{
851864
VolumeSpec: spec,
@@ -855,8 +868,10 @@ func TestVolumeHealthEnable(t *testing.T) {
855868
success: true,
856869
},
857870
{
858-
name: "when nodeVolumeStats=on, VolumeID=on, DeviceMountPath=on, VolumeCondition=off",
871+
name: "when nodeVolumeStats=on, volumeStatsSet=on, setVolumeStat=on, volumeCondition=off, setVolumecondition=off",
859872
volumeStatsSet: true,
873+
setVolumeStat: true,
874+
setVolumecondition: false,
860875
volumeConditionSet: false,
861876
volumeData: VolumeStatsOptions{
862877
VolumeSpec: spec,
@@ -865,31 +880,55 @@ func TestVolumeHealthEnable(t *testing.T) {
865880
},
866881
success: true,
867882
},
883+
{
884+
name: "when nodeVolumeStats=on, volumeStatsSet=off, setVolumeStat=off, volumeCondition=on, setVolumecondition=on",
885+
volumeStatsSet: false,
886+
setVolumeStat: false,
887+
setVolumecondition: true,
888+
volumeConditionSet: true,
889+
volumeData: VolumeStatsOptions{
890+
VolumeSpec: spec,
891+
VolumeID: "volume1",
892+
DeviceMountPath: "/foo/bar",
893+
},
894+
success: true,
895+
},
896+
{
897+
name: "when nodeVolumeStats=on, volumeStatsSet=off, setVolumeStat=off, volumeCondition=off, setVolumecondition=off",
898+
setVolumeStat: false,
899+
volumeConditionSet: false,
900+
volumeData: VolumeStatsOptions{
901+
VolumeSpec: spec,
902+
VolumeID: "volume1",
903+
DeviceMountPath: "/foo/bar",
904+
},
905+
success: false,
906+
},
868907
}
869908

870909
for _, tc := range tests {
871910
t.Run(tc.name, func(t *testing.T) {
872911
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
873912
defer cancel()
874913
csiSource, _ := getCSISourceFromSpec(tc.volumeData.VolumeSpec)
875-
csClient := setupClientWithVolumeStatsAndCondition(t, tc.volumeStatsSet, tc.volumeConditionSet)
914+
csClient := setupClientWithVolumeStatsAndCondition(t, tc.volumeStatsSet, tc.volumeConditionSet, tc.setVolumeStat, tc.setVolumecondition)
876915
metrics, err := csClient.NodeGetVolumeStats(ctx, csiSource.VolumeHandle, tc.volumeData.DeviceMountPath)
877-
if tc.success {
878-
assert.Nil(t, err)
916+
if err != nil && tc.success {
917+
t.Errorf("For %s : expected %v got %v", tc.name, tc.success, err)
879918
}
880-
881-
if metrics == nil {
882-
t.Errorf("csi.NodeGetVolumeStats returned nil metrics for volume %s", tc.volumeData.VolumeID)
883-
} else {
884-
if tc.volumeConditionSet {
885-
assert.NotNil(t, metrics.Abnormal)
886-
assert.NotNil(t, metrics.Message)
919+
if tc.success {
920+
if metrics == nil {
921+
t.Errorf("csi.NodeGetVolumeStats returned nil metrics for volume %s", tc.volumeData.VolumeID)
887922
} else {
888-
assert.Nil(t, metrics.Abnormal)
889-
assert.Nil(t, metrics.Message)
923+
if tc.volumeConditionSet {
924+
assert.NotNil(t, metrics.Abnormal)
925+
assert.NotNil(t, metrics.Message)
926+
} else {
927+
assert.Nil(t, metrics.Abnormal)
928+
assert.Nil(t, metrics.Message)
929+
}
890930
}
891931
}
892-
893932
})
894933
}
895934
}
@@ -919,7 +958,7 @@ func TestVolumeHealthDisable(t *testing.T) {
919958
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
920959
defer cancel()
921960
csiSource, _ := getCSISourceFromSpec(tc.volumeData.VolumeSpec)
922-
csClient := setupClientWithVolumeStatsAndCondition(t, tc.volumeStatsSet, false)
961+
csClient := setupClientWithVolumeStatsAndCondition(t, tc.volumeStatsSet, false, true, false)
923962
metrics, err := csClient.NodeGetVolumeStats(ctx, csiSource.VolumeHandle, tc.volumeData.DeviceMountPath)
924963
if tc.success {
925964
assert.Nil(t, err)

pkg/volume/csi/fake/fake_client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ type NodeClient struct {
8080
expansionSet bool
8181
volumeStatsSet bool
8282
volumeConditionSet bool
83+
SetVolumeStats bool
84+
SetVolumecondition bool
8385
singleNodeMultiWriterSet bool
8486
volumeMountGroupSet bool
8587
nodeGetInfoResp *csipb.NodeGetInfoResponse
@@ -110,13 +112,16 @@ func NewNodeClientWithExpansion(stageUnstageSet bool, expansionSet bool) *NodeCl
110112
func NewNodeClientWithVolumeStats(volumeStatsSet bool) *NodeClient {
111113
return &NodeClient{
112114
volumeStatsSet: volumeStatsSet,
115+
SetVolumeStats: true,
113116
}
114117
}
115118

116-
func NewNodeClientWithVolumeStatsAndCondition(volumeStatsSet, volumeConditionSet bool) *NodeClient {
119+
func NewNodeClientWithVolumeStatsAndCondition(volumeStatsSet, volumeConditionSet, setVolumeStats, setVolumeCondition bool) *NodeClient {
117120
return &NodeClient{
118121
volumeStatsSet: volumeStatsSet,
119122
volumeConditionSet: volumeConditionSet,
123+
SetVolumeStats: setVolumeStats,
124+
SetVolumecondition: setVolumeCondition,
120125
}
121126
}
122127

0 commit comments

Comments
 (0)