From 0c127f524c05b3d08c15385fc0e5295c5efa4e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Po=C5=BAniak?= Date: Thu, 27 Apr 2023 12:05:22 +0200 Subject: [PATCH 1/2] Check if runtime path exists before creating RuntimeServiceWrapper object --- dev-requirements.txt | 2 +- granulate_utils/containers/cri.py | 16 ++++++++++------ requirements.txt | 3 +-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index c11a9b7d..4466989d 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -5,7 +5,7 @@ isort==5.12.0 types-requests==0.1.9 types-setuptools==57.4.3 types-psutil==5.8.0 -grpcio-tools==1.43.0 +grpcio-tools==1.53.0 types-dataclasses==0.6.6 backoff-stubs~=1.10 pytest~=7.0.1 diff --git a/granulate_utils/containers/cri.py b/granulate_utils/containers/cri.py index 1aa9b1a1..7904f251 100644 --- a/granulate_utils/containers/cri.py +++ b/granulate_utils/containers/cri.py @@ -3,6 +3,7 @@ # Licensed under the AGPL3 License. See LICENSE.md in the project root for license information. # import json +import os from typing import List, Optional, Union import grpc # type: ignore # no types-grpc sadly @@ -47,12 +48,15 @@ def __init__(self) -> None: @staticmethod def _is_cri_available(path: str) -> bool: - with RuntimeServiceWrapper(path) as stub: - try: - stub.Version(api_pb2.VersionRequest()) - return True - except grpc._channel._InactiveRpcError: - return False + if os.path.exists(path): + with RuntimeServiceWrapper(path) as stub: + try: + stub.Version(api_pb2.VersionRequest()) + return True + except grpc._channel._InactiveRpcError: + return False + else: + return False @staticmethod def _reconstruct_name(container: Union[api_pb2.Container, api_pb2.ContainerStatus]) -> str: diff --git a/requirements.txt b/requirements.txt index ba4b80fc..e2f14bcf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,8 @@ psutil~=5.8.0 requests~=2.27.1 -grpcio~=1.43.0 +grpcio~=1.53.0 protobuf~=3.19.4 docker~=5.0.0 -dataclasses~=0.8; python_version < '3.7' typing-extensions>=4.1.0 pyelftools~=0.28 packaging~=23.1 From 2742cdf0e9bca6a690e1bb7a91ed4993cf25e77a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Po=C5=BAniak?= Date: Thu, 4 May 2023 15:30:41 +0200 Subject: [PATCH 2/2] Check if path exists before calling _is_cri_available --- granulate_utils/containers/cri.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/granulate_utils/containers/cri.py b/granulate_utils/containers/cri.py index 7904f251..5d1dcf1b 100644 --- a/granulate_utils/containers/cri.py +++ b/granulate_utils/containers/cri.py @@ -39,24 +39,22 @@ class CriClient(ContainersClientInterface): def __init__(self) -> None: self._runtimes = {} for rt, path in RUNTIMES: - path = "unix://" + ns.resolve_host_root_links(path) - if self._is_cri_available(path): - self._runtimes[rt] = path + if os.path.exists(path): + path = "unix://" + ns.resolve_host_root_links(path) + if self._is_cri_available(path): + self._runtimes[rt] = path if not self._runtimes: raise CriNotAvailableError(f"CRI is not available at any of {RUNTIMES}") @staticmethod def _is_cri_available(path: str) -> bool: - if os.path.exists(path): - with RuntimeServiceWrapper(path) as stub: - try: - stub.Version(api_pb2.VersionRequest()) - return True - except grpc._channel._InactiveRpcError: - return False - else: - return False + with RuntimeServiceWrapper(path) as stub: + try: + stub.Version(api_pb2.VersionRequest()) + return True + except grpc._channel._InactiveRpcError: + return False @staticmethod def _reconstruct_name(container: Union[api_pb2.Container, api_pb2.ContainerStatus]) -> str: