Skip to content

Updates for error checking #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/codeflare_sdk/cluster/awload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 9 additions & 28 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
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

from ray.job_submission import JobSubmissionClient

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,
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 9 additions & 5 deletions src/codeflare_sdk/utils/generate_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
43 changes: 43 additions & 0 deletions src/codeflare_sdk/utils/kube_api_helpers.py
Original file line number Diff line number Diff line change
@@ -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