From 61351b921951f6d89074d52366aa828193fef77f Mon Sep 17 00:00:00 2001 From: Bobbins228 Date: Wed, 2 Aug 2023 13:28:45 +0100 Subject: [PATCH 1/5] Added in cluster configuration support --- src/codeflare_sdk/cluster/auth.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index 85db3d61d..2d6a42ddd 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -162,7 +162,14 @@ def config_check() -> str: global config_path global api_client if config_path == None and api_client == None: - config.load_kube_config() + try: + config.load_kube_config() + except config.config_exception.ConfigException: + try: + config.load_incluster_config() + except: + print("Unable to load config file or in cluster configuration") + if config_path != None and api_client == None: return config_path From d8fb7993aacf868e4ac12f7b18c4fb5a1be25be6 Mon Sep 17 00:00:00 2001 From: Bobbins228 Date: Thu, 3 Aug 2023 10:17:22 +0100 Subject: [PATCH 2/5] Updated config_check() function --- src/codeflare_sdk/cluster/auth.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index 2d6a42ddd..d2cfb3c33 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -21,6 +21,7 @@ import abc from kubernetes import client, config +import os global api_client api_client = None @@ -161,14 +162,16 @@ def config_check() -> str: """ global config_path global api_client + home_directory = os.path.expanduser("~") if config_path == None and api_client == None: - try: + if os.path.isfile("%s/.kube/config" % home_directory): config.load_kube_config() - except config.config_exception.ConfigException: - try: - config.load_incluster_config() - except: - print("Unable to load config file or in cluster configuration") + elif "KUBERNETES_PORT" in os.environ: + config.load_incluster_config() + else: + print( + "Unable to load config file or in cluster configuration, try specifying a config file path with load_kube_config()" + ) if config_path != None and api_client == None: return config_path From f0f8a61e24c884babcfaca7d360ec3567477febb Mon Sep 17 00:00:00 2001 From: Bobbins228 Date: Thu, 17 Aug 2023 18:01:59 +0100 Subject: [PATCH 3/5] Added exception handling to config methods --- src/codeflare_sdk/cluster/auth.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index d2cfb3c33..7311a3166 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -165,12 +165,18 @@ def config_check() -> str: home_directory = os.path.expanduser("~") if config_path == None and api_client == None: if os.path.isfile("%s/.kube/config" % home_directory): - config.load_kube_config() + try: + config.load_kube_config() + except Exception as e: + print(e) elif "KUBERNETES_PORT" in os.environ: - config.load_incluster_config() + try: + config.load_incluster_config() + except Exception as e: + print(e) else: - print( - "Unable to load config file or in cluster configuration, try specifying a config file path with load_kube_config()" + raise PermissionError( + "Action not permitted, have you put in correct/up-to-date auth credentials?" ) if config_path != None and api_client == None: From 7b1398b9605f0d438cc8a52d786f75ad5d1575d9 Mon Sep 17 00:00:00 2001 From: Bobbins228 Date: Mon, 21 Aug 2023 17:01:39 +0100 Subject: [PATCH 4/5] Added unit tests for auth methods --- src/codeflare_sdk/cluster/auth.py | 4 +-- tests/unit_test.py | 46 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index 7311a3166..45a3d98c0 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -167,12 +167,12 @@ def config_check() -> str: if os.path.isfile("%s/.kube/config" % home_directory): try: config.load_kube_config() - except Exception as e: + except Exception as e: # pragma: no cover print(e) elif "KUBERNETES_PORT" in os.environ: try: config.load_incluster_config() - except Exception as e: + except Exception as e: # pragma: no cover print(e) else: raise PermissionError( diff --git a/tests/unit_test.py b/tests/unit_test.py index ac126016f..58f55bfa1 100644 --- a/tests/unit_test.py +++ b/tests/unit_test.py @@ -37,6 +37,8 @@ TokenAuthentication, Authentication, KubeConfigFileAuthentication, + config_check, + api_config_handler, ) from codeflare_sdk.utils.pretty_print import ( print_no_resources_found, @@ -150,6 +152,46 @@ def test_token_auth_login_tls(mocker): assert token_auth.login() == ("Logged into testserver:6443") +def test_config_check_no_config_file(mocker): + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") + mocker.patch("os.path.isfile", return_value=False) + mocker.patch("codeflare_sdk.cluster.auth.config_path", None) + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + + with pytest.raises(PermissionError) as e: + config_check() + + +def test_config_check_with_incluster_config(mocker): + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") + mocker.patch("os.path.isfile", return_value=False) + mocker.patch.dict(os.environ, {"KUBERNETES_PORT": "number"}) + mocker.patch("kubernetes.config.load_incluster_config", side_effect=None) + mocker.patch("codeflare_sdk.cluster.auth.config_path", None) + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + + result = config_check() + assert result == None + + +def test_config_check_with_existing_config_file(mocker): + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") + mocker.patch("os.path.isfile", return_value=True) + mocker.patch("kubernetes.config.load_kube_config", side_effect=None) + mocker.patch("codeflare_sdk.cluster.auth.config_path", None) + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + + result = config_check() + assert result == None + + +def test_config_check_with_config_path_and_no_api_client(mocker): + mocker.patch("codeflare_sdk.cluster.auth.config_path", "/mock/config/path") + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + result = config_check() + assert result == "/mock/config/path" + + def test_load_kube_config(mocker): mocker.patch.object(config, "load_kube_config") kube_config_auth = KubeConfigFileAuthentication( @@ -162,6 +204,10 @@ def test_load_kube_config(mocker): == "Loaded user config file at path %s" % kube_config_auth.kube_config_path ) + kube_config_auth = KubeConfigFileAuthentication(kube_config_path=None) + response = kube_config_auth.load_kube_config() + assert response == "Please specify a config file path" + def test_auth_coverage(): abstract = Authentication() From e395c6fd7dd9d9f1da2e95426c380c54577b60b4 Mon Sep 17 00:00:00 2001 From: Bobbins228 Date: Mon, 21 Aug 2023 18:16:52 +0100 Subject: [PATCH 5/5] Updated error handling --- src/codeflare_sdk/cluster/auth.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index 45a3d98c0..92fa1549d 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -22,6 +22,7 @@ import abc from kubernetes import client, config import os +from ..utils.kube_api_helpers import _kube_api_error_handling global api_client api_client = None @@ -168,12 +169,12 @@ def config_check() -> str: try: config.load_kube_config() except Exception as e: # pragma: no cover - print(e) + _kube_api_error_handling(e) elif "KUBERNETES_PORT" in os.environ: try: config.load_incluster_config() except Exception as e: # pragma: no cover - print(e) + _kube_api_error_handling(e) else: raise PermissionError( "Action not permitted, have you put in correct/up-to-date auth credentials?"