Skip to content

Commit

Permalink
add volume stats metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyXiangLi committed Jan 4, 2021
1 parent ddaa68b commit 29e2b72
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/onsi/ginkgo v1.10.2
github.com/onsi/gomega v1.7.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6 // indirect
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
google.golang.org/grpc v1.26.0
k8s.io/api v0.17.3
k8s.io/apimachinery v0.17.3
Expand Down
77 changes: 76 additions & 1 deletion pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

csi "github.com/container-storage-interface/spec/lib/go/csi"
Expand Down Expand Up @@ -65,13 +66,15 @@ var (
nodeCaps = []csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
csi.NodeServiceCapability_RPC_EXPAND_VOLUME,
csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
}
)

// nodeService represents the node service of CSI driver
type nodeService struct {
metadata cloud.MetadataService
mounter Mounter
statter Statter
inFlight *internal.InFlight
driverOptions *DriverOptions
}
Expand All @@ -87,6 +90,7 @@ func newNodeService(driverOptions *DriverOptions) nodeService {
return nodeService{
metadata: metadata,
mounter: newNodeMounter(),
statter: NewStatter(),
inFlight: internal.NewInFlight(),
driverOptions: driverOptions,
}
Expand Down Expand Up @@ -344,7 +348,64 @@ func (d *nodeService) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
}

func (d *nodeService) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Error(codes.Unimplemented, "NodeGetVolumeStats is not implemented yet")
klog.V(4).Infof("NodeGetVolumeStats: called with args %+v", *req)
if len(req.VolumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume ID was empty")
}
if len(req.VolumePath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume path was empty")
}

exists, err := d.mounter.ExistsPath(req.VolumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "unknown error when stat on %s: %v", req.VolumePath, err)
}
if !exists {
return nil, status.Errorf(codes.NotFound, "path %s does not exist", req.VolumePath)
}

isBlock, err := d.statter.IsBlockDevice(req.VolumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to determine whether %s is block device: %v", req.VolumePath, err)
}
if isBlock {
bcap, err := d.getBlockSizeBytes(req.VolumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get block capacity on path %s: %v", req.VolumePath, err)
}
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Unit: csi.VolumeUsage_BYTES,
Total: bcap,
},
},
}, nil
}

available, capacity, used, inodesFree, inodes, inodesUsed, err := d.statter.StatFS(req.VolumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get fs info on path %s: %v", req.VolumePath, err)
}

return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Available: available,
Total: capacity,
Used: used,
Unit: csi.VolumeUsage_BYTES,
},

{
Available: inodesFree,
Total: inodes,
Used: inodesUsed,
Unit: csi.VolumeUsage_INODES,
},
},
}, nil

}

func (d *nodeService) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
Expand Down Expand Up @@ -550,3 +611,17 @@ func hasMountOption(options []string, opt string) bool {
}
return false
}

func (d *nodeService) getBlockSizeBytes(devicePath string) (int64, error) {
cmd := d.mounter.Command("blockdev", "--getsize64", devicePath)
output, err := cmd.Output()
if err != nil {
return -1, fmt.Errorf("error when getting size of block volume at path %s: output: %s, err: %v", devicePath, string(output), err)
}
strOut := strings.TrimSpace(string(output))
gotSizeBytes, err := strconv.ParseInt(strOut, 10, 64)
if err != nil {
return -1, fmt.Errorf("failed to parse size %s into int a size", strOut)
}
return gotSizeBytes, nil
}
28 changes: 16 additions & 12 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,26 +1175,23 @@ func TestNodeGetVolumeStats(t *testing.T) {

mockMetadata := mocks.NewMockMetadataService(mockCtl)
mockMounter := mocks.NewMockMounter(mockCtl)
VolumePath := "/test"

mockMounter.EXPECT().ExistsPath(VolumePath).Return(true, nil)
awsDriver := nodeService{
metadata: mockMetadata,
mounter: mockMounter,
statter: NewFakeStatter(),
inFlight: internal.NewInFlight(),
}

expErrCode := codes.Unimplemented

req := &csi.NodeGetVolumeStatsRequest{}
_, err := awsDriver.NodeGetVolumeStats(context.TODO(), req)
if err == nil {
t.Fatalf("Expected error code %d, got nil", expErrCode)
}
srvErr, ok := status.FromError(err)
if !ok {
t.Fatalf("Could not get error status code from error: %v", srvErr)
req := &csi.NodeGetVolumeStatsRequest{
VolumeId: "vol-test",
VolumePath: VolumePath,
}
if srvErr.Code() != expErrCode {
t.Fatalf("Expected error code %d, got %d message %s", expErrCode, srvErr.Code(), srvErr.Message())
_, err := awsDriver.NodeGetVolumeStats(context.TODO(), req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
}

Expand Down Expand Up @@ -1226,6 +1223,13 @@ func TestNodeGetCapabilities(t *testing.T) {
},
},
},
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
},
},
},
}
expResp := &csi.NodeGetCapabilitiesResponse{Capabilities: caps}

Expand Down
80 changes: 80 additions & 0 deletions pkg/driver/statter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package driver

import (
"fmt"

"golang.org/x/sys/unix"
)

type Statter interface {
StatFS(path string) (int64, int64, int64, int64, int64, int64, error)
IsBlockDevice(string) (bool, error)
}

var _ Statter = realStatter{}

type realStatter struct {
}

func NewStatter() realStatter {
return realStatter{}
}

// IsBlock checks if the given path is a block device
func (realStatter) IsBlockDevice(fullPath string) (bool, error) {
var st unix.Stat_t
err := unix.Stat(fullPath, &st)
if err != nil {
return false, err
}

return (st.Mode & unix.S_IFMT) == unix.S_IFBLK, nil
}

func (realStatter) StatFS(path string) (available, capacity, used, inodesFree, inodes, inodesUsed int64, err error) {
statfs := &unix.Statfs_t{}
err = unix.Statfs(path, statfs)
if err != nil {
err = fmt.Errorf("failed to get fs info on path %s: %v", path, err)
return
}

// Available is blocks available * fragment size
available = int64(statfs.Bavail) * int64(statfs.Bsize)
// Capacity is total block count * fragment size
capacity = int64(statfs.Blocks) * int64(statfs.Bsize)
// Usage is block being used * fragment size (aka block size).
used = (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize)
inodes = int64(statfs.Files)
inodesFree = int64(statfs.Ffree)
inodesUsed = inodes - inodesFree
return
}

type fakeStatter struct{}

func NewFakeStatter() fakeStatter {
return fakeStatter{}
}

func (fakeStatter) StatFS(path string) (available, capacity, used, inodesFree, inodes, inodesUsed int64, err error) {
// Assume the file exists and give some dummy values back
return 1, 1, 1, 1, 1, 1, nil
}

func (fakeStatter) IsBlockDevice(fullPath string) (bool, error) {
return false, nil
}

0 comments on commit 29e2b72

Please sign in to comment.