Skip to content

Commit

Permalink
fix: sfs support get volume stats (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang authored Nov 3, 2022
1 parent f016dfc commit 89df4d3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/sfs-csi-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"flag"
"fmt"
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/utils/mounts"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,6 +70,8 @@ func main() {
// Make this configurable when ther are more options.
defaultShareProto := "NFS"
d := sfs.NewDriver(nodeID, endpoint, defaultShareProto, *cloud)
mount := mounts.GetMountProvider()
d.SetupDriver(mount)
d.Run()
},
}
Expand Down
26 changes: 24 additions & 2 deletions pkg/sfs/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ limitations under the License.
package sfs

import (
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/utils/mounts"
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/config"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"

"github.com/huaweicloud/huaweicloud-csi-driver/pkg/config"
)

const (
Expand Down Expand Up @@ -74,6 +74,9 @@ func NewDriver(nodeID, endpoint, shareProto string, cloud config.CloudCredential
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY,
})
d.AddNodeServiceCapabilities([]csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
})

d.ids = &identityServer{Driver: d}
d.cs = &controllerServer{Driver: d}
Expand Down Expand Up @@ -109,6 +112,21 @@ func (d *SfsDriver) AddVolumeCapabilityAccessModes(vc []csi.VolumeCapability_Acc
return vca
}

func (d *SfsDriver) AddNodeServiceCapabilities(nl []csi.NodeServiceCapability_RPC_Type) {
var nsc []*csi.NodeServiceCapability
for _, n := range nl {
klog.Infof("Enabling node service capability: %v", n.String())
nsc = append(nsc, &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: n,
},
},
})
}
d.nscap = nsc
}

func (d *SfsDriver) ValidateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
return nil
Expand All @@ -126,6 +144,10 @@ func (d *SfsDriver) GetVolumeCapabilityAccessModes() []*csi.VolumeCapability_Acc
return d.vcap
}

func (d *SfsDriver) SetupDriver(mount mounts.IMount) {
d.ns.Mount = mount
}

func (d *SfsDriver) Run() {
s := NewNonBlockingGRPCServer()
s.Start(d.endpoint, d.ids, d.cs, d.ns)
Expand Down
56 changes: 55 additions & 1 deletion pkg/sfs/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ package sfs

import (
"fmt"
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/utils/mounts"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
log "k8s.io/klog/v2"
utilpath "k8s.io/utils/path"
"os"

"github.com/chnsz/golangsdk"
Expand All @@ -30,6 +34,7 @@ import (

type nodeServer struct {
Driver *SfsDriver
Mount mounts.IMount
}

func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
Expand Down Expand Up @@ -146,7 +151,56 @@ func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetC
}

func (ns *nodeServer) NodeGetVolumeStats(_ context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
log.Infof("NodeGetVolumeStats: called with args %v", protosanitizer.StripSecrets(*req))

volumeID := req.GetVolumeId()
volumePath := req.GetVolumePath()
if err := nodeGetStatsValidation(volumeID, volumePath); err != nil {
return nil, err
}

stats, err := ns.Mount.GetDeviceStats(volumePath)
if err != nil {
return nil, status.Errorf(codes.Unknown, "Failed to get stats by path: %s", err)
}
log.Infof("NodeGetVolumeStats: stats info :%s", protosanitizer.StripSecrets(*stats))

return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Total: stats.TotalBytes,
Available: stats.AvailableBytes,
Used: stats.UsedBytes,
Unit: csi.VolumeUsage_BYTES,
},
{
Total: stats.TotalInodes,
Available: stats.AvailableInodes,
Used: stats.UsedInodes,
Unit: csi.VolumeUsage_INODES,
},
},
}, nil

}

func nodeGetStatsValidation(volumeID, volumePath string) error {
if len(volumeID) == 0 {
return status.Error(codes.InvalidArgument, "Validation failed, VolumeID cannot be empty")
}
if len(volumePath) == 0 {
return status.Error(codes.InvalidArgument, "Validation failed, VolumePath cannot be empty")
}

exists, err := utilpath.Exists(utilpath.CheckFollowSymlink, volumePath)
if err != nil {
return status.Errorf(codes.Unknown,
"Failed to check whether VolumePath %s exists: %s", volumePath, err)
}
if !exists {
return status.Errorf(codes.Unknown, "Error, the volume path %s not found", volumePath)
}
return nil
}

// NodeExpandVolume node expand volume
Expand Down

0 comments on commit 89df4d3

Please sign in to comment.