From 39b8fab6737171ab6ad6b56659d0b6599d806245 Mon Sep 17 00:00:00 2001 From: Mustafa Eyceoz Date: Wed, 12 Jul 2023 16:18:15 -0400 Subject: [PATCH] Updates for error checking --- src/codeflare_sdk/cluster/awload.py | 3 +- src/codeflare_sdk/cluster/cluster.py | 37 +++++------------- src/codeflare_sdk/utils/generate_yaml.py | 14 ++++--- src/codeflare_sdk/utils/kube_api_helpers.py | 43 +++++++++++++++++++++ 4 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 src/codeflare_sdk/utils/kube_api_helpers.py diff --git a/src/codeflare_sdk/cluster/awload.py b/src/codeflare_sdk/cluster/awload.py index 25f614232..ecf432133 100644 --- a/src/codeflare_sdk/cluster/awload.py +++ b/src/codeflare_sdk/cluster/awload.py @@ -20,11 +20,10 @@ from os.path import isfile import errno import os -import openshift as oc import yaml from kubernetes import client, config -from .cluster import _kube_api_error_handling +from ..utils.kube_api_helpers import _kube_api_error_handling class AWManager: diff --git a/src/codeflare_sdk/cluster/cluster.py b/src/codeflare_sdk/cluster/cluster.py index f89ecac71..8381ba2c3 100644 --- a/src/codeflare_sdk/cluster/cluster.py +++ b/src/codeflare_sdk/cluster/cluster.py @@ -18,7 +18,6 @@ cluster setup queue, a list of all existing clusters, and the user's working namespace. """ -from os import stat from time import sleep from typing import List, Optional, Tuple, Dict @@ -26,6 +25,7 @@ from ..utils import pretty_print from ..utils.generate_yaml import generate_appwrapper +from ..utils.kube_api_helpers import _kube_api_error_handling from .config import ClusterConfiguration from .model import ( AppWrapper, @@ -409,36 +409,17 @@ def get_cluster(cluster_name: str, namespace: str = "default"): # private methods def _get_ingress_domain(): - config.load_kube_config() - api_client = client.CustomObjectsApi() - ingress = api_client.get_cluster_custom_object( - "config.openshift.io", "v1", "ingresses", "cluster" - ) + try: + config.load_kube_config() + api_client = client.CustomObjectsApi() + ingress = api_client.get_cluster_custom_object( + "config.openshift.io", "v1", "ingresses", "cluster" + ) + except Exception as e: # pragma: no cover + return _kube_api_error_handling(e) return ingress["spec"]["domain"] -def _kube_api_error_handling(e: Exception): # pragma: no cover - perm_msg = ( - "Action not permitted, have you put in correct/up-to-date auth credentials?" - ) - nf_msg = "No instances found, nothing to be done." - exists_msg = "Resource with this name already exists." - if type(e) == config.ConfigException: - raise PermissionError(perm_msg) - if type(e) == executing.executing.NotOneValueFound: - print(nf_msg) - return - if type(e) == client.ApiException: - if e.reason == "Not Found": - print(nf_msg) - return - elif e.reason == "Unauthorized" or e.reason == "Forbidden": - raise PermissionError(perm_msg) - elif e.reason == "Conflict": - raise FileExistsError(exists_msg) - raise e - - def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]: try: config.load_kube_config() diff --git a/src/codeflare_sdk/utils/generate_yaml.py b/src/codeflare_sdk/utils/generate_yaml.py index 426203b4e..f71603d3a 100755 --- a/src/codeflare_sdk/utils/generate_yaml.py +++ b/src/codeflare_sdk/utils/generate_yaml.py @@ -22,6 +22,7 @@ import argparse import uuid from kubernetes import client, config +from .kube_api_helpers import _kube_api_error_handling def read_template(template): @@ -239,11 +240,14 @@ def enable_local_interactive(resources, cluster_name, namespace): ][0].get("command")[2] command = command.replace("deployment-name", cluster_name) - config.load_kube_config() - api_client = client.CustomObjectsApi() - ingress = api_client.get_cluster_custom_object( - "config.openshift.io", "v1", "ingresses", "cluster" - ) + try: + config.load_kube_config() + api_client = client.CustomObjectsApi() + ingress = api_client.get_cluster_custom_object( + "config.openshift.io", "v1", "ingresses", "cluster" + ) + except Exception as e: # pragma: no cover + return _kube_api_error_handling(e) domain = ingress["spec"]["domain"] command = command.replace("server-name", domain) diff --git a/src/codeflare_sdk/utils/kube_api_helpers.py b/src/codeflare_sdk/utils/kube_api_helpers.py new file mode 100644 index 000000000..492fc0c52 --- /dev/null +++ b/src/codeflare_sdk/utils/kube_api_helpers.py @@ -0,0 +1,43 @@ +# Copyright 2022 IBM, Red Hat +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +This sub-module exists primarily to be used internally for any Kubernetes +API error handling or wrapping. +""" + +import executing +from kubernetes import client, config + +# private methods +def _kube_api_error_handling(e: Exception): # pragma: no cover + perm_msg = ( + "Action not permitted, have you put in correct/up-to-date auth credentials?" + ) + nf_msg = "No instances found, nothing to be done." + exists_msg = "Resource with this name already exists." + if type(e) == config.ConfigException: + raise PermissionError(perm_msg) + if type(e) == executing.executing.NotOneValueFound: + print(nf_msg) + return + if type(e) == client.ApiException: + if e.reason == "Not Found": + print(nf_msg) + return + elif e.reason == "Unauthorized" or e.reason == "Forbidden": + raise PermissionError(perm_msg) + elif e.reason == "Conflict": + raise FileExistsError(exists_msg) + raise e