Skip to content
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

Added in cluster configuration support #271

Merged
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
19 changes: 18 additions & 1 deletion src/codeflare_sdk/cluster/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

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
Expand Down Expand Up @@ -161,8 +163,23 @@ def config_check() -> str:
"""
global config_path
global api_client
home_directory = os.path.expanduser("~")
if config_path == None and api_client == None:
config.load_kube_config()
if os.path.isfile("%s/.kube/config" % home_directory):
try:
config.load_kube_config()
except Exception as e: # pragma: no cover
_kube_api_error_handling(e)
elif "KUBERNETES_PORT" in os.environ:
try:
config.load_incluster_config()
except Exception as e: # pragma: no cover
_kube_api_error_handling(e)
else:
raise PermissionError(
"Action not permitted, have you put in correct/up-to-date auth credentials?"
)

if config_path != None and api_client == None:
return config_path

Expand Down
46 changes: 46 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
TokenAuthentication,
Authentication,
KubeConfigFileAuthentication,
config_check,
api_config_handler,
)
from codeflare_sdk.utils.pretty_print import (
print_no_resources_found,
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand Down
Loading