Skip to content

Commit 0d676ed

Browse files
authored
Merge pull request #1 from Maxusmusti/tedtest
Updates for error checking
2 parents 942459d + 39b8fab commit 0d676ed

File tree

4 files changed

+62
-35
lines changed

4 files changed

+62
-35
lines changed

src/codeflare_sdk/cluster/awload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
from os.path import isfile
2121
import errno
2222
import os
23-
import openshift as oc
2423
import yaml
2524

2625
from kubernetes import client, config
27-
from .cluster import _kube_api_error_handling
26+
from ..utils.kube_api_helpers import _kube_api_error_handling
2827

2928

3029
class AWManager:

src/codeflare_sdk/cluster/cluster.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
cluster setup queue, a list of all existing clusters, and the user's working namespace.
1919
"""
2020

21-
from os import stat
2221
from time import sleep
2322
from typing import List, Optional, Tuple, Dict
2423

2524
from ray.job_submission import JobSubmissionClient
2625

2726
from ..utils import pretty_print
2827
from ..utils.generate_yaml import generate_appwrapper
28+
from ..utils.kube_api_helpers import _kube_api_error_handling
2929
from .config import ClusterConfiguration
3030
from .model import (
3131
AppWrapper,
@@ -409,36 +409,17 @@ def get_cluster(cluster_name: str, namespace: str = "default"):
409409

410410
# private methods
411411
def _get_ingress_domain():
412-
config.load_kube_config()
413-
api_client = client.CustomObjectsApi()
414-
ingress = api_client.get_cluster_custom_object(
415-
"config.openshift.io", "v1", "ingresses", "cluster"
416-
)
412+
try:
413+
config.load_kube_config()
414+
api_client = client.CustomObjectsApi()
415+
ingress = api_client.get_cluster_custom_object(
416+
"config.openshift.io", "v1", "ingresses", "cluster"
417+
)
418+
except Exception as e: # pragma: no cover
419+
return _kube_api_error_handling(e)
417420
return ingress["spec"]["domain"]
418421

419422

420-
def _kube_api_error_handling(e: Exception): # pragma: no cover
421-
perm_msg = (
422-
"Action not permitted, have you put in correct/up-to-date auth credentials?"
423-
)
424-
nf_msg = "No instances found, nothing to be done."
425-
exists_msg = "Resource with this name already exists."
426-
if type(e) == config.ConfigException:
427-
raise PermissionError(perm_msg)
428-
if type(e) == executing.executing.NotOneValueFound:
429-
print(nf_msg)
430-
return
431-
if type(e) == client.ApiException:
432-
if e.reason == "Not Found":
433-
print(nf_msg)
434-
return
435-
elif e.reason == "Unauthorized" or e.reason == "Forbidden":
436-
raise PermissionError(perm_msg)
437-
elif e.reason == "Conflict":
438-
raise FileExistsError(exists_msg)
439-
raise e
440-
441-
442423
def _app_wrapper_status(name, namespace="default") -> Optional[AppWrapper]:
443424
try:
444425
config.load_kube_config()

src/codeflare_sdk/utils/generate_yaml.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import argparse
2323
import uuid
2424
from kubernetes import client, config
25+
from .kube_api_helpers import _kube_api_error_handling
2526

2627

2728
def read_template(template):
@@ -239,11 +240,14 @@ def enable_local_interactive(resources, cluster_name, namespace):
239240
][0].get("command")[2]
240241

241242
command = command.replace("deployment-name", cluster_name)
242-
config.load_kube_config()
243-
api_client = client.CustomObjectsApi()
244-
ingress = api_client.get_cluster_custom_object(
245-
"config.openshift.io", "v1", "ingresses", "cluster"
246-
)
243+
try:
244+
config.load_kube_config()
245+
api_client = client.CustomObjectsApi()
246+
ingress = api_client.get_cluster_custom_object(
247+
"config.openshift.io", "v1", "ingresses", "cluster"
248+
)
249+
except Exception as e: # pragma: no cover
250+
return _kube_api_error_handling(e)
247251
domain = ingress["spec"]["domain"]
248252
command = command.replace("server-name", domain)
249253

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2022 IBM, Red Hat
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
This sub-module exists primarily to be used internally for any Kubernetes
17+
API error handling or wrapping.
18+
"""
19+
20+
import executing
21+
from kubernetes import client, config
22+
23+
# private methods
24+
def _kube_api_error_handling(e: Exception): # pragma: no cover
25+
perm_msg = (
26+
"Action not permitted, have you put in correct/up-to-date auth credentials?"
27+
)
28+
nf_msg = "No instances found, nothing to be done."
29+
exists_msg = "Resource with this name already exists."
30+
if type(e) == config.ConfigException:
31+
raise PermissionError(perm_msg)
32+
if type(e) == executing.executing.NotOneValueFound:
33+
print(nf_msg)
34+
return
35+
if type(e) == client.ApiException:
36+
if e.reason == "Not Found":
37+
print(nf_msg)
38+
return
39+
elif e.reason == "Unauthorized" or e.reason == "Forbidden":
40+
raise PermissionError(perm_msg)
41+
elif e.reason == "Conflict":
42+
raise FileExistsError(exists_msg)
43+
raise e

0 commit comments

Comments
 (0)