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

feat(stats): adding volume stats #27

Merged
merged 2 commits into from
Dec 30, 2019
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
46 changes: 39 additions & 7 deletions pkg/driver/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/core/v1alpha1"
"github.com/openebs/zfs-localpv/pkg/builder"
"github.com/openebs/zfs-localpv/pkg/mgmt"
zfs "github.com/openebs/zfs-localpv/pkg/zfs"
"github.com/openebs/zfs-localpv/pkg/zfs"
"golang.org/x/net/context"
"golang.org/x/sys/unix"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -201,7 +202,7 @@ func (ns *node) NodeGetCapabilities(
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_UNKNOWN,
Type: csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
},
},
},
Expand Down Expand Up @@ -252,19 +253,50 @@ func (ns *node) NodeExpandVolume(
req *csi.NodeExpandVolumeRequest,
) (*csi.NodeExpandVolumeResponse, error) {

return nil, nil
return nil, status.Error(codes.Unimplemented, "")
}

// NodeGetVolumeStats returns statistics for the
// given volume
//
// This implements csi.NodeServer
func (ns *node) NodeGetVolumeStats(
ctx context.Context,
in *csi.NodeGetVolumeStatsRequest,
req *csi.NodeGetVolumeStatsRequest,
) (*csi.NodeGetVolumeStatsResponse, error) {

return nil, status.Error(codes.Unimplemented, "")
volID := req.GetVolumeId()
path := req.GetVolumePath()

if len(volID) == 0 {
return nil, status.Error(codes.InvalidArgument, "volume id is not provided")
}
if len(path) == 0 {
return nil, status.Error(codes.InvalidArgument, "path is not provided")
}

if zfs.IsMountPath(path) == false {
return nil, status.Error(codes.InvalidArgument, "path is not a mount path")
}

var sfs unix.Statfs_t
if err := unix.Statfs(path, &sfs); err != nil {
return nil, status.Errorf(codes.Internal, "statfs on %s failed: %v", path, err)
}

var usage []*csi.VolumeUsage
usage = append(usage, &csi.VolumeUsage{
Unit: csi.VolumeUsage_BYTES,
Total: int64(sfs.Blocks) * sfs.Bsize,
Used: int64(sfs.Blocks-sfs.Bfree) * sfs.Bsize,
Available: int64(sfs.Bavail) * sfs.Bsize,
})
usage = append(usage, &csi.VolumeUsage{
Unit: csi.VolumeUsage_INODES,
Total: int64(sfs.Files),
Used: int64(sfs.Files - sfs.Ffree),
Available: int64(sfs.Ffree),
})

return &csi.NodeGetVolumeStatsResponse{Usage: usage}, nil
}

func (ns *node) validateNodePublishReq(
Expand Down
21 changes: 21 additions & 0 deletions pkg/zfs/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ func GetMounts(dev string) ([]string, error) {
return currentMounts, nil
}

// IsMountPath returns true if path is a mount path
func IsMountPath(path string) bool {

var (
err error
mountList []mount.MountPoint
)

mounter := mount.New("")
// Get list of mounted paths present with the node
if mountList, err = mounter.List(); err != nil {
return false
}
for _, mntInfo := range mountList {
if mntInfo.Path == path {
return true
}
}
return false
}

func verifyMountRequest(vol *apis.ZFSVolume, mountpath string) error {
if len(mountpath) == 0 {
return status.Error(codes.InvalidArgument, "mount path missing in request")
Expand Down