Skip to content

Commit

Permalink
Prevent crash when account is created with non-existent namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
theoctober19th committed Jan 9, 2024
1 parent 590a8bd commit 8de38e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion spark8t/cli/service_account_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
AccountNotFound,
PrimaryAccountNotFound,
ResourceAlreadyExists,
K8sResourceNotFound
)
from spark8t.services import K8sServiceAccountRegistry, parse_conf_overrides
from spark8t.utils import setup_logging
Expand Down Expand Up @@ -199,7 +200,7 @@ def main(args: Namespace, logger: Logger):
try:
main(args, logger)
exit(0)
except (AccountNotFound, PrimaryAccountNotFound, ResourceAlreadyExists) as e:
except (AccountNotFound, PrimaryAccountNotFound, ResourceAlreadyExists, K8sResourceNotFound) as e:
print(str(e))
exit(1)
except Exception as e:
Expand Down
11 changes: 11 additions & 0 deletions spark8t/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from spark8t.domain import KubernetesResourceType


class K8sClusterNotReachable(Exception):
"""Kubernetes cluster cannot be reached successfully by the client."""

Expand Down Expand Up @@ -52,3 +55,11 @@ class FormatError(SyntaxError):

class ResourceAlreadyExists(FileExistsError):
pass


class NamespaceNotFound(K8sResourceNotFound):
def __init__(self, resource_name: str):
super().__init__(resource_name, resource_type=KubernetesResourceType.NAMESPACE)

def __str__(self) -> str:
return f"Namespace '{self.resource_name}' could not be found."
7 changes: 7 additions & 0 deletions spark8t/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
AccountNotFound,
FormatError,
K8sResourceNotFound,
NamespaceNotFound,
ResourceAlreadyExists,
)
from spark8t.literals import MANAGED_BY_LABELNAME, PRIMARY_LABELNAME, SPARK8S_LABEL
Expand Down Expand Up @@ -1180,6 +1181,12 @@ def create(self, service_account: ServiceAccount) -> str:
rolename = username + "-role"
rolebindingname = username + "-role-binding"

if not self.kube_interface.exists(
KubernetesResourceType.NAMESPACE,
service_account.namespace,
):
raise NamespaceNotFound(service_account.namespace)

# Check if the resources to be created already exist in K8s cluster
if self.kube_interface.exists(
KubernetesResourceType.SERVICEACCOUNT,
Expand Down

0 comments on commit 8de38e6

Please sign in to comment.