Implement GET_VOLUME_STATS capability

This commit is contained in:
Mehran Kholdi 2020-07-14 15:55:00 +04:30
parent 83e184c58a
commit c895312131
2 changed files with 55 additions and 11 deletions

View File

@ -10,6 +10,27 @@ import rawfile_util
from rawfile_util import attached_loops from rawfile_util import attached_loops
def volume_stats(volume_id: str) -> dict:
img_file = rawfile_util.img_file(volume_id)
dev_stat = img_file.stat()
stats = {
"dev_size": dev_stat.st_size,
"dev_free": dev_stat.st_size - dev_stat.st_blocks * 512,
}
mountpoint = volume_to_mountpoint(img_file)
if mountpoint is not None:
fs_stat = os.statvfs(mountpoint)
stats.update(
{
"fs_size": fs_stat.f_frsize * fs_stat.f_blocks,
"fs_free": fs_stat.f_frsize * fs_stat.f_bfree,
"fs_files": fs_stat.f_files,
"fs_files_free": fs_stat.f_ffree,
}
)
return stats
class VolumeStatsCollector(object): class VolumeStatsCollector(object):
def collect(self): def collect(self):
VOLUME_ID = "volume_id" VOLUME_ID = "volume_id"
@ -43,18 +64,18 @@ class VolumeStatsCollector(object):
) )
for volume_id in rawfile_util.list_all_volumes(): for volume_id in rawfile_util.list_all_volumes():
img_file = rawfile_util.img_file(volume_id)
labels = [volume_id] labels = [volume_id]
dev_stat = img_file.stat() key_to_gauge = {
dev_size.add_metric(labels, dev_stat.st_size) "dev_size": dev_size,
dev_free.add_metric(labels, dev_stat.st_size - dev_stat.st_blocks * 512) "dev_free": dev_free,
mountpoint = volume_to_mountpoint(img_file) "fs_size": fs_size,
if mountpoint is not None: "fs_free": fs_free,
fs_stat = os.statvfs(mountpoint) "fs_files": fs_files,
fs_size.add_metric(labels, fs_stat.f_frsize * fs_stat.f_blocks) "fs_files_free": fs_files_free,
fs_free.add_metric(labels, fs_stat.f_frsize * fs_stat.f_bfree) }
fs_files.add_metric(labels, fs_stat.f_files) stats = volume_stats(volume_id)
fs_files_free.add_metric(labels, fs_stat.f_ffree) for key in stats.keys():
key_to_gauge[key].add_metric(labels, stats[key])
return [fs_size, fs_free, fs_files, fs_files_free, dev_size, dev_free] return [fs_size, fs_free, fs_files, fs_files_free, dev_size, dev_free]

View File

@ -6,6 +6,7 @@ from google.protobuf.wrappers_pb2 import BoolValue
import rawfile_util import rawfile_util
from csi import csi_pb2, csi_pb2_grpc from csi import csi_pb2, csi_pb2_grpc
from declarative import be_mounted, be_unmounted, be_symlink, be_absent from declarative import be_mounted, be_unmounted, be_symlink, be_absent
from metrics import volume_stats
from orchestrator.k8s import volume_to_node, run_on_node from orchestrator.k8s import volume_to_node, run_on_node
from rawfile_util import attach_loop, detach_loops from rawfile_util import attach_loop, detach_loops
from remote import init_rawfile, scrub, expand_rawfile from remote import init_rawfile, scrub, expand_rawfile
@ -55,6 +56,7 @@ class RawFileNodeServicer(csi_pb2_grpc.NodeServicer):
return csi_pb2.NodeGetCapabilitiesResponse( return csi_pb2.NodeGetCapabilitiesResponse(
capabilities=[ capabilities=[
Cap(rpc=Cap.RPC(type=Cap.RPC.STAGE_UNSTAGE_VOLUME)), Cap(rpc=Cap.RPC(type=Cap.RPC.STAGE_UNSTAGE_VOLUME)),
Cap(rpc=Cap.RPC(type=Cap.RPC.GET_VOLUME_STATS)),
Cap(rpc=Cap.RPC(type=Cap.RPC.EXPAND_VOLUME)), Cap(rpc=Cap.RPC(type=Cap.RPC.EXPAND_VOLUME)),
] ]
) )
@ -105,6 +107,27 @@ class RawFileNodeServicer(csi_pb2_grpc.NodeServicer):
detach_loops(img_file) detach_loops(img_file)
return csi_pb2.NodeUnstageVolumeResponse() return csi_pb2.NodeUnstageVolumeResponse()
@log_grpc_request
def NodeGetVolumeStats(self, request, context):
volume_id = request.volume_id
stats = volume_stats(volume_id)
return csi_pb2.NodeGetVolumeStatsResponse(
usage=[
csi_pb2.VolumeUsage(
available=stats["fs_free"],
total=stats["fs_size"],
used=stats["fs_size"] - stats["fs_free"],
unit=csi_pb2.VolumeUsage.Unit.BYTES,
),
csi_pb2.VolumeUsage(
available=stats["fs_files_free"],
total=stats["fs_files"],
used=stats["fs_files"] - stats["fs_files_free"],
unit=csi_pb2.VolumeUsage.Unit.INODES,
),
]
)
@log_grpc_request @log_grpc_request
def NodeExpandVolume(self, request, context): def NodeExpandVolume(self, request, context):
volume_id = request.volume_id volume_id = request.volume_id