Skip to content

Commit ac1a1dc

Browse files
Ygnasopenshift-merge-bot[bot]
authored andcommitted
docs: enhance common module code documentation
1 parent 1f026b8 commit ac1a1dc

File tree

4 files changed

+156
-19
lines changed

4 files changed

+156
-19
lines changed

Diff for: src/codeflare_sdk/common/kubernetes_cluster/auth.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,20 @@ def load_kube_config(self):
165165

166166
def config_check() -> str:
167167
"""
168-
Function for loading the config file at the default config location ~/.kube/config if the user has not
169-
specified their own config file or has logged in with their token and server.
168+
Check and load the Kubernetes config from the default location.
169+
170+
This function checks if a Kubernetes config file exists at the default path
171+
(`~/.kube/config`). If none is provided, it tries to load in-cluster config.
172+
If the `config_path` global variable is set by an external module (e.g., `auth.py`),
173+
this path will be used directly.
174+
175+
Returns:
176+
str:
177+
The loaded config path if successful.
178+
179+
Raises:
180+
PermissionError:
181+
If no valid credentials or config file is found.
170182
"""
171183
global config_path
172184
global api_client
@@ -215,7 +227,16 @@ def _gen_ca_cert_path(ca_cert_path: Optional[str]):
215227

216228

217229
def get_api_client() -> client.ApiClient:
218-
"This function should load the api client with defaults"
230+
"""
231+
Retrieve the Kubernetes API client with the default configuration.
232+
233+
This function returns the current API client instance if already loaded,
234+
or creates a new API client with the default configuration.
235+
236+
Returns:
237+
client.ApiClient:
238+
The Kubernetes API client object.
239+
"""
219240
if api_client != None:
220241
return api_client
221242
to_return = client.ApiClient()

Diff for: src/codeflare_sdk/common/kueue/kueue.py

+57-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,23 @@
1919
from kubernetes.client.exceptions import ApiException
2020

2121

22-
def get_default_kueue_name(namespace: str):
23-
# If the local queue is set, use it. Otherwise, try to use the default queue.
22+
def get_default_kueue_name(namespace: str) -> Optional[str]:
23+
"""
24+
Retrieves the default Kueue name from the provided namespace.
25+
26+
This function attempts to fetch the local queues in the given namespace and checks if any of them is annotated
27+
as the default queue. If found, the name of the default queue is returned.
28+
29+
The default queue is marked with the annotation "kueue.x-k8s.io/default-queue" set to "true."
30+
31+
Args:
32+
namespace (str):
33+
The Kubernetes namespace where the local queues are located.
34+
35+
Returns:
36+
Optional[str]:
37+
The name of the default queue if it exists, otherwise None.
38+
"""
2439
try:
2540
config_check()
2641
api_instance = client.CustomObjectsApi(get_api_client())
@@ -58,12 +73,14 @@ def list_local_queues(
5873
Depending on the version of the local queue API, the available flavors may not be present in the response.
5974
6075
Args:
61-
namespace (str, optional): The namespace to list local queues from. Defaults to None.
62-
flavors (List[str], optional): The flavors to filter local queues by. Defaults to None.
76+
namespace (str, optional):
77+
The namespace to list local queues from. Defaults to None.
78+
flavors (List[str], optional):
79+
The flavors to filter local queues by. Defaults to None.
6380
Returns:
64-
List[dict]: A list of dictionaries containing the name of the local queue and the available flavors
81+
List[dict]:
82+
A list of dictionaries containing the name of the local queue and the available flavors
6583
"""
66-
6784
from ...ray.cluster.cluster import get_current_namespace
6885

6986
if namespace is None: # pragma: no cover
@@ -92,8 +109,22 @@ def list_local_queues(
92109
return to_return
93110

94111

95-
def local_queue_exists(namespace: str, local_queue_name: str):
96-
# get all local queues in the namespace
112+
def local_queue_exists(namespace: str, local_queue_name: str) -> bool:
113+
"""
114+
Checks if a local queue with the provided name exists in the given namespace.
115+
116+
This function queries the local queues in the specified namespace and verifies if any queue matches the given name.
117+
118+
Args:
119+
namespace (str):
120+
The namespace where the local queues are located.
121+
local_queue_name (str):
122+
The name of the local queue to check for existence.
123+
124+
Returns:
125+
bool:
126+
True if the local queue exists, False otherwise.
127+
"""
97128
try:
98129
config_check()
99130
api_instance = client.CustomObjectsApi(get_api_client())
@@ -113,6 +144,24 @@ def local_queue_exists(namespace: str, local_queue_name: str):
113144

114145

115146
def add_queue_label(item: dict, namespace: str, local_queue: Optional[str]):
147+
"""
148+
Adds a local queue name label to the provided item.
149+
150+
If the local queue is not provided, the default local queue for the namespace is used. The function validates if the
151+
local queue exists, and if it does, the local queue name label is added to the resource metadata.
152+
153+
Args:
154+
item (dict):
155+
The resource where the label will be added.
156+
namespace (str):
157+
The namespace of the local queue.
158+
local_queue (str, optional):
159+
The name of the local queue to use. Defaults to None.
160+
161+
Raises:
162+
ValueError:
163+
If the provided or default local queue does not exist in the namespace.
164+
"""
116165
lq_name = local_queue or get_default_kueue_name(namespace)
117166
if lq_name == None:
118167
return

Diff for: src/codeflare_sdk/common/utils/demos.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ def copy_demo_nbs(dir: str = "./demo-notebooks", overwrite: bool = False):
1313
Any files that exist in the directory that don't match these values will remain untouched.
1414
1515
Args:
16-
dir (str): The directory to copy the demo notebooks to. Defaults to "./demo-notebooks". overwrite (bool):
17-
overwrite (bool): Whether to overwrite files in the directory if it already exists. Defaults to False.
16+
dir (str):
17+
The directory to copy the demo notebooks to. Defaults to "./demo-notebooks".
18+
overwrite (bool):
19+
Whether to overwrite files in the directory if it already exists. Defaults to False.
20+
1821
Raises:
19-
FileExistsError: If the directory already exists.
22+
FileExistsError:
23+
If the directory already exists.
2024
"""
2125
# does dir exist already?
2226
if overwrite is False and pathlib.Path(dir).exists():

Diff for: src/codeflare_sdk/common/utils/generate_cert.py

+68-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@
2828

2929

3030
def generate_ca_cert(days: int = 30):
31-
# Generate base64 encoded ca.key and ca.cert
32-
# Similar to:
33-
# openssl req -x509 -nodes -newkey rsa:2048 -keyout ca.key -days 1826 -out ca.crt -subj '/CN=root-ca'
34-
# base64 -i ca.crt -i ca.key
31+
"""
32+
Generates a self-signed CA certificate and private key, encoded in base64 format.
33+
34+
Similar to:
35+
openssl req -x509 -nodes -newkey rsa:2048 -keyout ca.key -days 1826 -out ca.crt -subj '/CN=root-ca'
36+
37+
Args:
38+
days (int):
39+
The number of days for which the CA certificate will be valid. Default is 30.
40+
41+
Returns:
42+
Tuple[str, str]:
43+
A tuple containing the base64-encoded private key and CA certificate.
44+
"""
3545

3646
private_key = rsa.generate_private_key(
3747
public_exponent=65537,
@@ -79,6 +89,25 @@ def generate_ca_cert(days: int = 30):
7989

8090

8191
def get_secret_name(cluster_name, namespace, api_instance):
92+
"""
93+
Retrieves the name of the Kubernetes secret containing the CA certificate for the given Ray cluster.
94+
95+
Args:
96+
cluster_name (str):
97+
The name of the Ray cluster.
98+
namespace (str):
99+
The Kubernetes namespace where the Ray cluster is located.
100+
api_instance (client.CoreV1Api):
101+
An instance of the Kubernetes CoreV1Api.
102+
103+
Returns:
104+
str:
105+
The name of the Kubernetes secret containing the CA certificate.
106+
107+
Raises:
108+
KeyError:
109+
If no secret matching the cluster name is found.
110+
"""
82111
label_selector = f"ray.openshift.ai/cluster-name={cluster_name}"
83112
try:
84113
secrets = api_instance.list_namespaced_secret(
@@ -97,7 +126,26 @@ def get_secret_name(cluster_name, namespace, api_instance):
97126

98127

99128
def generate_tls_cert(cluster_name, namespace, days=30):
100-
# Create a folder tls-<cluster>-<namespace> and store three files: ca.crt, tls.crt, and tls.key
129+
"""
130+
Generates a TLS certificate and key for a Ray cluster, saving them locally along with the CA certificate.
131+
132+
Args:
133+
cluster_name (str):
134+
The name of the Ray cluster.
135+
namespace (str):
136+
The Kubernetes namespace where the Ray cluster is located.
137+
days (int):
138+
The number of days for which the TLS certificate will be valid. Default is 30.
139+
140+
Files Created:
141+
- ca.crt: The CA certificate.
142+
- tls.crt: The TLS certificate signed by the CA.
143+
- tls.key: The private key for the TLS certificate.
144+
145+
Raises:
146+
Exception:
147+
If an error occurs while retrieving the CA secret.
148+
"""
101149
tls_dir = os.path.join(os.getcwd(), f"tls-{cluster_name}-{namespace}")
102150
if not os.path.exists(tls_dir):
103151
os.makedirs(tls_dir)
@@ -181,6 +229,21 @@ def generate_tls_cert(cluster_name, namespace, days=30):
181229

182230

183231
def export_env(cluster_name, namespace):
232+
"""
233+
Sets environment variables to configure TLS for a Ray cluster.
234+
235+
Args:
236+
cluster_name (str):
237+
The name of the Ray cluster.
238+
namespace (str):
239+
The Kubernetes namespace where the Ray cluster is located.
240+
241+
Environment Variables Set:
242+
- RAY_USE_TLS: Enables TLS for Ray.
243+
- RAY_TLS_SERVER_CERT: Path to the TLS server certificate.
244+
- RAY_TLS_SERVER_KEY: Path to the TLS server private key.
245+
- RAY_TLS_CA_CERT: Path to the CA certificate.
246+
"""
184247
tls_dir = os.path.join(os.getcwd(), f"tls-{cluster_name}-{namespace}")
185248
os.environ["RAY_USE_TLS"] = "1"
186249
os.environ["RAY_TLS_SERVER_CERT"] = os.path.join(tls_dir, "tls.crt")

0 commit comments

Comments
 (0)