Skip to content

Commit

Permalink
Remove strict
Browse files Browse the repository at this point in the history
  • Loading branch information
t-persson committed Oct 9, 2024
1 parent f478b2d commit a7a7d62
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 88 deletions.
15 changes: 4 additions & 11 deletions src/etos_lib/kubernetes/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ class Environment(Resource):

logger = logging.getLogger(__name__)

def __init__(self, client: Kubernetes, strict: bool = False):
"""Set up Kubernetes client.
:param strict: If True, the client will raise exceptions when Kubernetes could not
be reached as expected such as the ETOS namespace not being able to be determined.
The default (False) will just ignore any problems.
"""
self.strict = strict
with self._catch_errors_if_not_strict():
self.client = client.environments
self.namespace = client.namespace
def __init__(self, client: Kubernetes):
"""Set up Kubernetes client."""
self.client = client.environments
self.namespace = client.namespace
15 changes: 4 additions & 11 deletions src/etos_lib/kubernetes/environment_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ class EnvironmentRequest(Resource):

logger = logging.getLogger(__name__)

def __init__(self, client: Kubernetes, strict: bool = False):
"""Set up Kubernetes client.
:param strict: If True, the client will raise exceptions when Kubernetes could not
be reached as expected such as the ETOS namespace not being able to be determined.
The default (False) will just ignore any problems.
"""
self.strict = strict
with self._catch_errors_if_not_strict():
self.client = client.environment_requests
self.namespace = client.namespace
def __init__(self, client: Kubernetes):
"""Set up Kubernetes client."""
self.client = client.environment_requests
self.namespace = client.namespace
52 changes: 8 additions & 44 deletions src/etos_lib/kubernetes/etos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@
import os
import logging
from pathlib import Path
from contextlib import contextmanager
from typing import Optional
from pydantic import BaseModel
from kubernetes import config
from kubernetes.client import api_client
from kubernetes.client.exceptions import ApiException
from kubernetes.dynamic import DynamicClient
from kubernetes.dynamic.resource import Resource as DynamicResource, ResourceInstance
from kubernetes.dynamic.exceptions import (
ResourceNotFoundError,
ResourceNotUniqueError,
NotFoundError,
)
from kubernetes.dynamic.exceptions import NotFoundError

config.load_config()
NAMESPACE_FILE = Path("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
Expand All @@ -44,56 +38,26 @@ class Resource:

client: DynamicResource
namespace: str = "default"
strict: bool = False

@contextmanager
def _catch_errors_if_not_strict(self):
"""Catch errors if the strict flag is False, else raise."""
try:
yield
# Internal exceptions
except NoNamespace:
if self.strict:
raise
# Client exceptions
except ApiException:
if self.strict:
raise
# Dynamic client exceptions
except (ResourceNotFoundError, ResourceNotUniqueError):
if self.strict:
raise
# Built-in exceptions
except AttributeError:
# AttributeError happens if ResourceNotFoundError was raised when setting up
# clients.
if self.strict:
raise

def get(self, name: str) -> Optional[ResourceInstance]:
"""Get a resource from Kubernetes by name."""
try:
with self._catch_errors_if_not_strict():
resource = self.client.get(name=name, namespace=self.namespace) # type: ignore
resource = self.client.get(name=name, namespace=self.namespace) # type: ignore
except NotFoundError:
resource = None
return resource

def delete(self, name: str) -> bool:
"""Delete a resource by name."""
with self._catch_errors_if_not_strict():
if self.client.delete(name=name, namespace=self.namespace): # type: ignore
return True
return False
if self.client.delete(name=name, namespace=self.namespace): # type: ignore
return True
return False

def create(self, model: BaseModel) -> bool:
"""Create a resource from a pydantic model."""
with self._catch_errors_if_not_strict():
if self.client.create(
body=model.model_dump(), namespace=self.namespace
): # type: ignore
return True
return False
if self.client.create(body=model.model_dump(), namespace=self.namespace): # type: ignore
return True
return False

def exists(self, name: str) -> bool:
"""Test if a resource with name exists."""
Expand Down
15 changes: 4 additions & 11 deletions src/etos_lib/kubernetes/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@
class Provider(Resource):
"""Provider handles the Provider custom Kubernetes resources."""

def __init__(self, client: Kubernetes, strict: bool = False):
"""Set up Kubernetes client.
:param strict: If True, the client will raise exceptions when Kubernetes could not
be reached as expected such as the ETOS namespace not being able to be determined.
The default (False) will just ignore any problems.
"""
self.strict = strict
with self._catch_errors_if_not_strict():
self.client = client.providers
self.namespace = client.namespace
def __init__(self, client: Kubernetes):
"""Set up Kubernetes client."""
self.client = client.providers
self.namespace = client.namespace
15 changes: 4 additions & 11 deletions src/etos_lib/kubernetes/testrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@
class TestRun(Resource):
"""TestRun handles the TestRun custom Kubernetes resources."""

def __init__(self, client: Kubernetes, strict: bool = False):
"""Set up Kubernetes client.
:param strict: If True, the client will raise exceptions when Kubernetes could not
be reached as expected such as the ETOS namespace not being able to be determined.
The default (False) will just ignore any problems.
"""
self.strict = strict
with self._catch_errors_if_not_strict():
self.client = client.testruns
self.namespace = client.namespace
def __init__(self, client: Kubernetes):
"""Set up Kubernetes client."""
self.client = client.testruns
self.namespace = client.namespace

0 comments on commit a7a7d62

Please sign in to comment.