Skip to content

Commit 730b0ed

Browse files
[Part-1] Refactor k8s related code into a separate module
This commit is first of the chain in refactoring the cluster/ package.
1 parent 932b925 commit 730b0ed

18 files changed

+331
-294
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ node_modules
1212
.DS_Store
1313
ui-tests/playwright-report
1414
ui-tests/test-results
15+
/src/codeflare_sdk.egg-info/

poetry.lock

+252-243
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codeflare_sdk.egg-info/SOURCES.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ src/codeflare_sdk.egg-info/SOURCES.txt
77
src/codeflare_sdk.egg-info/dependency_links.txt
88
src/codeflare_sdk.egg-info/top_level.txt
99
src/codeflare_sdk/cluster/__init__.py
10-
src/codeflare_sdk/cluster/auth.py
1110
src/codeflare_sdk/cluster/awload.py
1211
src/codeflare_sdk/cluster/cluster.py
1312
src/codeflare_sdk/cluster/config.py
1413
src/codeflare_sdk/cluster/model.py
1514
src/codeflare_sdk/cluster/widgets.py
15+
src/codeflare_sdk/common/__init__.py
16+
src/codeflare_sdk/common/kubernetes_cluster/__init__.py
17+
src/codeflare_sdk/common/kubernetes_cluster/auth.py
18+
src/codeflare_sdk/common/kubernetes_cluster/kube_api_helpers.py
1619
src/codeflare_sdk/job/__init__.py
1720
src/codeflare_sdk/job/ray_jobs.py
1821
src/codeflare_sdk/utils/__init__.py
1922
src/codeflare_sdk/utils/demos.py
2023
src/codeflare_sdk/utils/generate_cert.py
2124
src/codeflare_sdk/utils/generate_yaml.py
22-
src/codeflare_sdk/utils/kube_api_helpers.py
2325
src/codeflare_sdk/utils/pretty_print.py

src/codeflare_sdk/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
from .cluster import (
2-
Authentication,
3-
KubeConfiguration,
4-
TokenAuthentication,
5-
KubeConfigFileAuthentication,
62
AWManager,
73
Cluster,
84
ClusterConfiguration,
@@ -17,6 +13,13 @@
1713
view_clusters,
1814
)
1915

16+
from .common import (
17+
Authentication,
18+
KubeConfiguration,
19+
TokenAuthentication,
20+
KubeConfigFileAuthentication,
21+
)
22+
2023
from .job import RayJobClient
2124

2225
from .utils import generate_cert

src/codeflare_sdk/cluster/__init__.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
from .auth import (
2-
Authentication,
3-
KubeConfiguration,
4-
TokenAuthentication,
5-
KubeConfigFileAuthentication,
6-
)
7-
81
from .model import (
92
RayClusterStatus,
103
AppWrapperStatus,

src/codeflare_sdk/cluster/awload.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import os
2323
import yaml
2424

25-
from kubernetes import client, config
26-
from ..utils.kube_api_helpers import _kube_api_error_handling
27-
from .auth import config_check, get_api_client
25+
from kubernetes import client
26+
from ..common import _kube_api_error_handling
27+
from ..common.kubernetes_cluster.auth import (
28+
config_check,
29+
get_api_client,
30+
)
2831

2932

3033
class AWManager:

src/codeflare_sdk/cluster/cluster.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
cluster setup queue, a list of all existing clusters, and the user's working namespace.
1919
"""
2020

21-
import re
22-
import subprocess
2321
from time import sleep
2422
from typing import List, Optional, Tuple, Dict
2523

26-
from kubernetes import config
2724
from ray.job_submission import JobSubmissionClient
2825

29-
from .auth import config_check, get_api_client
26+
from ..common.kubernetes_cluster.auth import (
27+
config_check,
28+
get_api_client,
29+
)
3030
from ..utils import pretty_print
3131
from ..utils.generate_yaml import (
3232
generate_appwrapper,
3333
head_worker_gpu_count_from_cluster,
3434
)
35-
from ..utils.kube_api_helpers import _kube_api_error_handling
35+
from ..common import _kube_api_error_handling
3636
from ..utils.generate_yaml import is_openshift_cluster
3737

3838
from .config import ClusterConfiguration
@@ -47,8 +47,7 @@
4747
cluster_up_down_buttons,
4848
is_notebook,
4949
)
50-
from kubernetes import client, config
51-
from kubernetes.utils import parse_quantity
50+
from kubernetes import client
5251
import yaml
5352
import os
5453
import requests

src/codeflare_sdk/cluster/widgets.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
import pandas as pd
2929
from .config import ClusterConfiguration
3030
from .model import RayClusterStatus
31-
from ..utils.kube_api_helpers import _kube_api_error_handling
32-
from .auth import config_check, get_api_client
31+
from ..common import _kube_api_error_handling
32+
from ..common.kubernetes_cluster.auth import (
33+
config_check,
34+
get_api_client,
35+
)
3336

3437

3538
def cluster_up_down_buttons(cluster: "codeflare_sdk.cluster.Cluster") -> widgets.Button:

src/codeflare_sdk/common/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Importing everything from the kubernetes_cluster module
2+
from .kubernetes_cluster import (
3+
Authentication,
4+
KubeConfiguration,
5+
TokenAuthentication,
6+
KubeConfigFileAuthentication,
7+
_kube_api_error_handling,
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .auth import (
2+
Authentication,
3+
KubeConfiguration,
4+
TokenAuthentication,
5+
KubeConfigFileAuthentication,
6+
config_check,
7+
get_api_client,
8+
)
9+
10+
from .kube_api_helpers import _kube_api_error_handling

src/codeflare_sdk/cluster/auth.py renamed to src/codeflare_sdk/common/kubernetes_cluster/auth.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from kubernetes import client, config
2424
import os
2525
import urllib3
26-
from ..utils.kube_api_helpers import _kube_api_error_handling
26+
from .kube_api_helpers import _kube_api_error_handling
2727

2828
from typing import Optional
2929

src/codeflare_sdk/utils/generate_cert.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
from cryptography import x509
2020
from cryptography.x509.oid import NameOID
2121
import datetime
22-
from ..cluster.auth import config_check, get_api_client
23-
from kubernetes import client, config
24-
from .kube_api_helpers import _kube_api_error_handling
22+
from ..common.kubernetes_cluster.auth import (
23+
config_check,
24+
get_api_client,
25+
)
26+
from kubernetes import client
27+
from ..common import _kube_api_error_handling
2528

2629

2730
def generate_ca_cert(days: int = 30):

src/codeflare_sdk/utils/generate_yaml.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121
from typing import Optional
2222
import typing
2323
import yaml
24-
import sys
2524
import os
26-
import argparse
2725
import uuid
28-
from kubernetes import client, config
29-
from .kube_api_helpers import _kube_api_error_handling
30-
from ..cluster.auth import get_api_client, config_check
31-
from os import urandom
32-
from base64 import b64encode
33-
from urllib3.util import parse_url
26+
from kubernetes import client
27+
from ..common import _kube_api_error_handling
28+
from ..common.kubernetes_cluster.auth import (
29+
get_api_client,
30+
config_check,
31+
)
3432
from kubernetes.client.exceptions import ApiException
3533
import codeflare_sdk
3634

tests/e2e/support.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import subprocess
55
from kubernetes import client, config
66
import kubernetes.client
7-
from codeflare_sdk.utils.kube_api_helpers import _kube_api_error_handling
7+
from codeflare_sdk.common.kubernetes_cluster.kube_api_helpers import (
8+
_kube_api_error_handling,
9+
)
810

911

1012
def get_ray_image():

tests/unit_test.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
_app_wrapper_status,
4040
_ray_cluster_status,
4141
)
42-
from codeflare_sdk.cluster.auth import (
42+
from codeflare_sdk.common.kubernetes_cluster import (
4343
TokenAuthentication,
4444
Authentication,
4545
KubeConfigFileAuthentication,
@@ -71,7 +71,7 @@
7171
get_package_and_version,
7272
)
7373

74-
import codeflare_sdk.utils.kube_api_helpers
74+
import codeflare_sdk.common.kubernetes_cluster.kube_api_helpers
7575
from codeflare_sdk.utils.generate_yaml import (
7676
gen_names,
7777
is_openshift_cluster,
@@ -198,8 +198,8 @@ def test_token_auth_login_tls(mocker):
198198
def test_config_check_no_config_file(mocker):
199199
mocker.patch("os.path.expanduser", return_value="/mock/home/directory")
200200
mocker.patch("os.path.isfile", return_value=False)
201-
mocker.patch("codeflare_sdk.cluster.auth.config_path", None)
202-
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
201+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.config_path", None)
202+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None)
203203

204204
with pytest.raises(PermissionError) as e:
205205
config_check()
@@ -210,8 +210,8 @@ def test_config_check_with_incluster_config(mocker):
210210
mocker.patch("os.path.isfile", return_value=False)
211211
mocker.patch.dict(os.environ, {"KUBERNETES_PORT": "number"})
212212
mocker.patch("kubernetes.config.load_incluster_config", side_effect=None)
213-
mocker.patch("codeflare_sdk.cluster.auth.config_path", None)
214-
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
213+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.config_path", None)
214+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None)
215215

216216
result = config_check()
217217
assert result == None
@@ -221,16 +221,18 @@ def test_config_check_with_existing_config_file(mocker):
221221
mocker.patch("os.path.expanduser", return_value="/mock/home/directory")
222222
mocker.patch("os.path.isfile", return_value=True)
223223
mocker.patch("kubernetes.config.load_kube_config", side_effect=None)
224-
mocker.patch("codeflare_sdk.cluster.auth.config_path", None)
225-
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
224+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.config_path", None)
225+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None)
226226

227227
result = config_check()
228228
assert result == None
229229

230230

231231
def test_config_check_with_config_path_and_no_api_client(mocker):
232-
mocker.patch("codeflare_sdk.cluster.auth.config_path", "/mock/config/path")
233-
mocker.patch("codeflare_sdk.cluster.auth.api_client", None)
232+
mocker.patch(
233+
"codeflare_sdk.common.kubernetes_cluster.auth.config_path", "/mock/config/path"
234+
)
235+
mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None)
234236
result = config_check()
235237
assert result == "/mock/config/path"
236238

@@ -2170,7 +2172,8 @@ def test_map_to_ray_cluster(mocker):
21702172

21712173
mock_api_client = mocker.MagicMock(spec=client.ApiClient)
21722174
mocker.patch(
2173-
"codeflare_sdk.cluster.auth.get_api_client", return_value=mock_api_client
2175+
"codeflare_sdk.common.kubernetes_cluster.auth.get_api_client",
2176+
return_value=mock_api_client,
21742177
)
21752178

21762179
mock_routes = {

tests/upgrade/raycluster_sdk_upgrade_sleep_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from tests.e2e.support import *
1313

1414

15-
from codeflare_sdk.utils.kube_api_helpers import _kube_api_error_handling
15+
from codeflare_sdk.common import _kube_api_error_handling
1616

1717
namespace = "test-ns-rayupgrade-sleep"
1818
# Global variables for kueue resources

tests/upgrade/raycluster_sdk_upgrade_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from tests.e2e.support import *
88
from codeflare_sdk.cluster.cluster import get_cluster
99

10-
from codeflare_sdk.utils.kube_api_helpers import _kube_api_error_handling
10+
from codeflare_sdk.common import _kube_api_error_handling
1111

1212
namespace = "test-ns-rayupgrade"
1313
# Global variables for kueue resources

0 commit comments

Comments
 (0)