From ffa40426576b2606361c494f5ae24cd1c5245ea5 Mon Sep 17 00:00:00 2001 From: Bartlomiej Gmerek <42570669+Gmerold@users.noreply.github.com> Date: Fri, 10 Jan 2025 11:02:39 +0100 Subject: [PATCH] fix: Fixes Unauthorized error when listing K8s nodes (#458) --- src/charm.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/charm.py b/src/charm.py index 5883822..ebefff2 100755 --- a/src/charm.py +++ b/src/charm.py @@ -28,6 +28,7 @@ from charms.sdcore_upf_k8s.v0.fiveg_n4 import N4Provides from jinja2 import Environment, FileSystemLoader from lightkube.core.client import Client +from lightkube.core.exceptions import ApiError from lightkube.models.meta_v1 import ObjectMeta from lightkube.resources.core_v1 import Node, Pod from ops import ( @@ -1112,10 +1113,17 @@ def _hugepages_are_available(self) -> bool: Returns: bool: Whether HugePages are available in the K8S nodes """ - client = Client() - nodes = client.list(Node) if not self._hugepages_is_enabled(): return True + client = Client() + try: + nodes = client.list(Node) + except ApiError as e: + if e.status.reason == "Unauthorized": + logger.debug("kube-apiserver not ready yet") + return False + else: + raise e if not nodes: return False return all(node.status.allocatable.get("hugepages-1Gi", "0") >= "2Gi" for node in nodes) # type: ignore