Skip to content

Commit

Permalink
Load client lazily (#41)
Browse files Browse the repository at this point in the history
* Load client lazily
  • Loading branch information
t-persson authored Nov 21, 2024
1 parent 08cbd15 commit 0d0d1eb
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/etos_lib/kubernetes/etos.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def exists(self, name: str) -> bool:
class Kubernetes:
"""Kubernetes is a client for fetching ETOS custom resources from Kubernetes."""

__client = None
__providers = None
__requests = None
__testruns = None
Expand All @@ -87,7 +88,13 @@ def __init__(self, version="v1alpha1", config_path: Union[None, str, Path] = Non
"""Initialize a dynamic client with version."""
self.load_kubernetes_config(config_path)
self.version = f"etos.eiffel-community.github.io/{version}"
self.__client = DynamicClient(api_client.ApiClient())

@property
def _client(self) -> DynamicClient:
"""Client to use when communicating with Kubernetes."""
if self.__client is None:
self.__client = DynamicClient(api_client.ApiClient())
return self.__client

def load_kubernetes_config(self, path: Union[None, str, Path]):
"""Load a Kubernetes config if possible, will log an error if not possible.
Expand Down Expand Up @@ -129,16 +136,14 @@ def namespace(self) -> str:
def providers(self) -> DynamicResource:
"""Providers request returns a client for Provider resources."""
if self.__providers is None:
self.__providers = self.__client.resources.get(
api_version=self.version, kind="Provider"
)
self.__providers = self._client.resources.get(api_version=self.version, kind="Provider")
return self.__providers

@property
def environment_requests(self) -> DynamicResource:
"""Environment requests returns a client for EnvironmentRequest resources."""
if self.__requests is None:
self.__requests = self.__client.resources.get(
self.__requests = self._client.resources.get(
api_version=self.version, kind="EnvironmentRequest"
)
return self.__requests
Expand All @@ -147,7 +152,7 @@ def environment_requests(self) -> DynamicResource:
def environments(self) -> DynamicResource:
"""Environments returns a client for Environment resources."""
if self.__environments is None:
self.__environments = self.__client.resources.get(
self.__environments = self._client.resources.get(
api_version=self.version, kind="Environment"
)
return self.__environments
Expand All @@ -156,5 +161,5 @@ def environments(self) -> DynamicResource:
def testruns(self) -> DynamicResource:
"""Testruns returns a client for TestRun resources."""
if self.__testruns is None:
self.__testruns = self.__client.resources.get(api_version=self.version, kind="TestRun")
self.__testruns = self._client.resources.get(api_version=self.version, kind="TestRun")
return self.__testruns

0 comments on commit 0d0d1eb

Please sign in to comment.