From 5f05e54ac0c403bc985b681a177e36eb3e0e55d2 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 7 Aug 2023 15:35:22 +0200 Subject: [PATCH 1/3] initial RPC_GET_VOLUME_STAT support --- go.mod | 2 +- pkg/linode-bs/driver.go | 1 + pkg/linode-bs/nodeserver.go | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 45276304..5ddb8efa 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/kubernetes-csi/csi-test/v3 v3.1.1 github.com/linode/linodego v0.21.0 golang.org/x/net v0.7.0 + golang.org/x/sys v0.5.0 google.golang.org/grpc v1.31.1 k8s.io/apimachinery v0.19.2 k8s.io/utils v0.0.0-20201005171033-6301aaf42dc7 @@ -20,7 +21,6 @@ require ( github.com/onsi/ginkgo v1.11.0 // indirect github.com/onsi/gomega v1.7.1 // indirect golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 // indirect - golang.org/x/sys v0.5.0 // indirect golang.org/x/text v0.7.0 // indirect google.golang.org/appengine v1.6.5 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect diff --git a/pkg/linode-bs/driver.go b/pkg/linode-bs/driver.go index 673cc0d2..3a221d78 100644 --- a/pkg/linode-bs/driver.go +++ b/pkg/linode-bs/driver.go @@ -94,6 +94,7 @@ func (linodeDriver *LinodeDriver) SetupLinodeDriver(linodeClient linodeclient.Li ns := []csi.NodeServiceCapability_RPC_Type{ csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME, csi.NodeServiceCapability_RPC_EXPAND_VOLUME, + csi.NodeServiceCapability_RPC_GET_VOLUME_STATS, } if err := linodeDriver.AddNodeServiceCapabilities(ns); err != nil { return err diff --git a/pkg/linode-bs/nodeserver.go b/pkg/linode-bs/nodeserver.go index 431bf64f..6990fc53 100644 --- a/pkg/linode-bs/nodeserver.go +++ b/pkg/linode-bs/nodeserver.go @@ -27,6 +27,7 @@ import ( "github.com/linode/linode-blockstorage-csi-driver/pkg/metadata" mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" "golang.org/x/net/context" + "golang.org/x/sys/unix" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "k8s.io/utils/mount" @@ -304,5 +305,27 @@ func (ns *LinodeNodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInf } func (ns *LinodeNodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) { - return nil, status.Error(codes.Unimplemented, "NodeGetVolumeStats is not yet implemented") + var statfs unix.Statfs_t + // See http://man7.org/linux/man-pages/man2/statfs.2.html for details. + err := unix.Statfs(req.VolumePath, &statfs) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get stats: %v", err.Error()) + } + + return &csi.NodeGetVolumeStatsResponse{ + Usage: []*csi.VolumeUsage{ + { + Available: int64(statfs.Bavail) * int64(statfs.Bsize), + Total: int64(statfs.Blocks) * int64(statfs.Bsize), + Used: (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize), + Unit: csi.VolumeUsage_BYTES, + }, + { + Available: int64(statfs.Ffree), + Total: int64(statfs.Files), + Used: int64(statfs.Files) - int64(statfs.Ffree), + Unit: csi.VolumeUsage_INODES, + }, + }, + }, nil } From 5a7409fdc4513a013e05b0cea42af8b3c29c725d Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 7 Aug 2023 16:24:27 +0200 Subject: [PATCH 2/3] correctly return when request parameters are empty --- pkg/linode-bs/nodeserver.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/linode-bs/nodeserver.go b/pkg/linode-bs/nodeserver.go index 6990fc53..193ee975 100644 --- a/pkg/linode-bs/nodeserver.go +++ b/pkg/linode-bs/nodeserver.go @@ -305,6 +305,10 @@ func (ns *LinodeNodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInf } func (ns *LinodeNodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) { + if req.VolumeId == "" || req.VolumePath == "" { + return nil, status.Error(codes.InvalidArgument, "volume ID or path empty") + } + var statfs unix.Statfs_t // See http://man7.org/linux/man-pages/man2/statfs.2.html for details. err := unix.Statfs(req.VolumePath, &statfs) From 368c00954d73fee352da6dbee960a0682d5d550e Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 7 Aug 2023 16:26:23 +0200 Subject: [PATCH 3/3] handle not found error correctly to CSI spec --- pkg/linode-bs/nodeserver.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/linode-bs/nodeserver.go b/pkg/linode-bs/nodeserver.go index 193ee975..b0e0104b 100644 --- a/pkg/linode-bs/nodeserver.go +++ b/pkg/linode-bs/nodeserver.go @@ -15,6 +15,7 @@ limitations under the License. */ import ( + "errors" "fmt" "os" "strconv" @@ -313,6 +314,9 @@ func (ns *LinodeNodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.Nod // See http://man7.org/linux/man-pages/man2/statfs.2.html for details. err := unix.Statfs(req.VolumePath, &statfs) if err != nil { + if errors.Is(err, unix.ENOENT) { + return nil, status.Errorf(codes.NotFound, "volume path not found: %v", err.Error()) + } return nil, status.Errorf(codes.Internal, "failed to get stats: %v", err.Error()) }