Skip to content

Commit 8540143

Browse files
committed
docs: enhance common module code documentation
1 parent 13658b8 commit 8540143

File tree

4 files changed

+152
-19
lines changed

4 files changed

+152
-19
lines changed

src/codeflare_sdk/common/kubernetes_cluster/auth.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,18 @@ 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+
173+
Returns:
174+
str:
175+
The loaded config path if successful.
176+
177+
Raises:
178+
PermissionError:
179+
If no valid credentials or config file is found.
170180
"""
171181
global config_path
172182
global api_client
@@ -215,7 +225,16 @@ def _gen_ca_cert_path(ca_cert_path: Optional[str]):
215225

216226

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

src/codeflare_sdk/common/kueue/kueue.py

+55-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,21 @@
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+
Args:
30+
namespace (str):
31+
The Kubernetes namespace where the local queues are located.
32+
33+
Returns:
34+
Optional[str]:
35+
The name of the default queue if it exists, otherwise None.
36+
"""
2437
try:
2538
config_check()
2639
api_instance = client.CustomObjectsApi(get_api_client())
@@ -58,12 +71,14 @@ def list_local_queues(
5871
Depending on the version of the local queue API, the available flavors may not be present in the response.
5972
6073
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.
74+
namespace (str, optional):
75+
The namespace to list local queues from. Defaults to None.
76+
flavors (List[str], optional):
77+
The flavors to filter local queues by. Defaults to None.
6378
Returns:
64-
List[dict]: A list of dictionaries containing the name of the local queue and the available flavors
79+
List[dict]:
80+
A list of dictionaries containing the name of the local queue and the available flavors
6581
"""
66-
6782
from ...ray.cluster.cluster import get_current_namespace
6883

6984
if namespace is None: # pragma: no cover
@@ -92,8 +107,22 @@ def list_local_queues(
92107
return to_return
93108

94109

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

114143

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

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". overwrite (bool):
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():

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)