diff --git a/vast_csi/server.py b/vast_csi/server.py index 2f12cb49..97553f97 100644 --- a/vast_csi/server.py +++ b/vast_csi/server.py @@ -34,7 +34,13 @@ from easypy.bunch import Bunch from .logging import logger, init_logging -from .utils import patch_traceback_format, get_mount, normalize_mount_options, parse_load_balancing_strategy +from .utils import ( + patch_traceback_format, + get_mount, + normalize_mount_options, + parse_load_balancing_strategy, + string_to_proto_timestamp +) from . import csi_pb2_grpc from .csi_pb2_grpc import ControllerServicer, NodeServicer, IdentityServicer from . import csi_types as types @@ -573,14 +579,11 @@ def CreateSnapshot(self, source_volume_id, name, parameters=None): if not handled: raise Abort(INVALID_ARGUMENT, str(exc)) - creation_time = types.Timestamp() - creation_time.FromJsonString(snap.created) - snp = types.Snapshot( size_bytes=0, # indicates 'unspecified' snapshot_id=str(snap.id), source_volume_id=volume_id, - creation_time=creation_time, + creation_time=string_to_proto_timestamp(snap.created), ready_to_use=True, ) @@ -650,7 +653,6 @@ def to_snapshot(dentry): ) else: page_size = max_entries or 250 - ts = types.Timestamp() if starting_token: ret = self.vms_session.get_by_token(starting_token) @@ -663,7 +665,7 @@ def to_snapshot(dentry): size_bytes=0, # indicates 'unspecified' snapshot_id=str(snap.id), source_volume_id=self._to_volume_id(snap.path) or "n/a", - creation_time=ts.FromJsonString(snap.created), + creation_time=string_to_proto_timestamp(snap.created), ready_to_use=True, ))]) @@ -672,7 +674,7 @@ def to_snapshot(dentry): size_bytes=0, # indicates 'unspecified' snapshot_id=str(snap.id), source_volume_id=self._to_volume_id(snap.path) or "n/a", - creation_time=ts.FromJsonString(snap.created), + creation_time=string_to_proto_timestamp(snap.created), ready_to_use=True, )) for snap in ret.results]) diff --git a/vast_csi/utils.py b/vast_csi/utils.py index 02582f24..277151f0 100644 --- a/vast_csi/utils.py +++ b/vast_csi/utils.py @@ -1,4 +1,5 @@ import re +from datetime import datetime from requests.exceptions import HTTPError # noqa from plumbum import local @@ -9,6 +10,8 @@ ROUNDROBIN, RANDOM, ) +from . import csi_types as types + LOAD_BALANCING_STRATEGIES = {ROUNDROBIN, RANDOM} @@ -119,3 +122,9 @@ def normalize_mount_options(mount_options: str): s = mount_options.strip() mount_options = list({p for p in s.split(",") if p}) return mount_options + + +def string_to_proto_timestamp(str_ts: str): + """Convert string to protobuf.Timestamp""" + t = datetime.fromisoformat(str_ts.rstrip("Z")).timestamp() + return types.Timestamp(seconds=int(t), nanos=int(t % 1 * 1e9))