rbd: use NewMetricsBlock to get size

instead of lsblk command use NewMetricsBlock
function from the kubernetes package to get
the size.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2021-10-05 15:20:33 +05:30 committed by mergify[bot]
parent 97525f5e74
commit cda2abca5d

View File

@ -33,6 +33,7 @@ import (
"github.com/container-storage-interface/spec/lib/go/csi" "github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"k8s.io/kubernetes/pkg/volume"
mount "k8s.io/mount-utils" mount "k8s.io/mount-utils"
utilexec "k8s.io/utils/exec" utilexec "k8s.io/utils/exec"
) )
@ -1140,18 +1141,10 @@ func (ns *NodeServer) NodeGetVolumeStats(
// //
// TODO: https://github.com/container-storage-interface/spec/issues/371#issuecomment-756834471 // TODO: https://github.com/container-storage-interface/spec/issues/371#issuecomment-756834471
func blockNodeGetVolumeStats(ctx context.Context, targetPath string) (*csi.NodeGetVolumeStatsResponse, error) { func blockNodeGetVolumeStats(ctx context.Context, targetPath string) (*csi.NodeGetVolumeStatsResponse, error) {
args := []string{"--noheadings", "--bytes", "--output=SIZE", targetPath} mp := volume.NewMetricsBlock(targetPath)
lsblkSize, _, err := util.ExecCommand(ctx, "/bin/lsblk", args...) m, err := mp.GetMetrics()
if err != nil { if err != nil {
err = fmt.Errorf("lsblk %v returned an error: %w", args, err) err = fmt.Errorf("failed to get metrics: %w", err)
log.ErrorLog(ctx, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
size, err := strconv.ParseInt(strings.TrimSpace(lsblkSize), 10, 64)
if err != nil {
err = fmt.Errorf("failed to convert %q to bytes: %w", lsblkSize, err)
log.ErrorLog(ctx, err.Error()) log.ErrorLog(ctx, err.Error())
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
@ -1160,7 +1153,7 @@ func blockNodeGetVolumeStats(ctx context.Context, targetPath string) (*csi.NodeG
return &csi.NodeGetVolumeStatsResponse{ return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{ Usage: []*csi.VolumeUsage{
{ {
Total: size, Total: m.Capacity.Value(),
Unit: csi.VolumeUsage_BYTES, Unit: csi.VolumeUsage_BYTES,
}, },
}, },