codeflare_sdk.common.utils package

Submodules

codeflare_sdk.common.utils.constants module

codeflare_sdk.common.utils.constants.RAY_VERSION = '2.47.1'

The below are used to define the default runtime image for the Ray Cluster. * For python 3.11:ray:2.47.1-py311-cu121 * For python 3.12:ray:2.47.1-py312-cu128

codeflare_sdk.common.utils.demos module

codeflare_sdk.common.utils.demos.copy_demo_nbs(dir: str = './demo-notebooks', overwrite: bool = False)[source]

Copy the demo notebooks from the package to the current working directory

overwrite=True will overwrite any files that exactly match files written by copy_demo_nbs in the target directory. Any files that exist in the directory that don’t match these values will remain untouched.

Args:
dir (str):

The directory to copy the demo notebooks to. Defaults to “./demo-notebooks”.

overwrite (bool):

Whether to overwrite files in the directory if it already exists. Defaults to False.

Raises:
FileExistsError:

If the directory already exists.

codeflare_sdk.common.utils.generate_cert module

codeflare_sdk.common.utils.generate_cert.export_env(cluster_name, namespace)[source]

Sets environment variables to configure TLS for a Ray cluster.

Args:
cluster_name (str):

The name of the Ray cluster.

namespace (str):

The Kubernetes namespace where the Ray cluster is located.

Environment Variables Set:
  • RAY_USE_TLS: Enables TLS for Ray.

  • RAY_TLS_SERVER_CERT: Path to the TLS server certificate.

  • RAY_TLS_SERVER_KEY: Path to the TLS server private key.

  • RAY_TLS_CA_CERT: Path to the CA certificate.

codeflare_sdk.common.utils.generate_cert.generate_ca_cert(days: int = 30)[source]

Generates a self-signed CA certificate and private key, encoded in base64 format.

Similar to:

openssl req -x509 -nodes -newkey rsa:2048 -keyout ca.key -days 1826 -out ca.crt -subj ‘/CN=root-ca’

Args:
days (int):

The number of days for which the CA certificate will be valid. Default is 30.

Returns:
Tuple[str, str]:

A tuple containing the base64-encoded private key and CA certificate.

codeflare_sdk.common.utils.generate_cert.generate_tls_cert(cluster_name, namespace, days=30)[source]

Generates a TLS certificate and key for a Ray cluster, saving them locally along with the CA certificate.

Args:
cluster_name (str):

The name of the Ray cluster.

namespace (str):

The Kubernetes namespace where the Ray cluster is located.

days (int):

The number of days for which the TLS certificate will be valid. Default is 30.

Files Created:
  • ca.crt: The CA certificate.

  • tls.crt: The TLS certificate signed by the CA.

  • tls.key: The private key for the TLS certificate.

Raises:
Exception:

If an error occurs while retrieving the CA secret.

codeflare_sdk.common.utils.generate_cert.get_secret_name(cluster_name, namespace, api_instance)[source]

Retrieves the name of the Kubernetes secret containing the CA certificate for the given Ray cluster.

Args:
cluster_name (str):

The name of the Ray cluster.

namespace (str):

The Kubernetes namespace where the Ray cluster is located.

api_instance (client.CoreV1Api):

An instance of the Kubernetes CoreV1Api.

Returns:
str:

The name of the Kubernetes secret containing the CA certificate.

Raises:
KeyError:

If no secret matching the cluster name is found.

codeflare_sdk.common.utils.k8s_utils module

Kubernetes utility functions for the CodeFlare SDK.

codeflare_sdk.common.utils.k8s_utils.get_current_namespace()[source]

Retrieves the current Kubernetes namespace.

Returns:
str:

The current namespace or None if not found.

codeflare_sdk.common.utils.utils module

codeflare_sdk.common.utils.utils.get_ray_image_for_python_version(python_version=None, warn_on_unsupported=True)[source]

Get the appropriate Ray image for a given Python version. If no version is provided, uses the current runtime Python version. This prevents us needing to hard code image versions for tests.

Args:

python_version: Python version string (e.g. “3.11”). If None, detects current version. warn_on_unsupported: If True, warns and returns None for unsupported versions.

If False, silently falls back to Python 3.12 image.

codeflare_sdk.common.utils.utils.update_image(image) str[source]

The update_image() function automatically sets the image config parameter to a preset image based on Python version if not specified. This now points to the centralized function in utils.py.

codeflare_sdk.common.utils.validation module

Validation utilities for the CodeFlare SDK.

This module contains validation functions used across the SDK for ensuring configuration compatibility and correctness.

codeflare_sdk.common.utils.validation.extract_ray_version_from_image(image_name: str) str | None[source]

Extract Ray version from a container image name.

Supports various image naming patterns: - quay.io/modh/ray:2.47.1-py311-cu121 - ray:2.47.1 - some-registry/ray:2.47.1-py311 - quay.io/modh/ray@sha256:… (falls back to None)

Args:

image_name: The container image name/tag

Returns:

The extracted Ray version, or None if not found

codeflare_sdk.common.utils.validation.validate_ray_version_compatibility(image_name: str, sdk_ray_version: str = '2.47.1') Tuple[bool, bool, str][source]

Validate that the Ray version in the runtime image matches the SDK’s Ray version.

Args:

image_name: The container image name/tag sdk_ray_version: The Ray version used by the CodeFlare SDK

Returns:
tuple: (is_compatible, is_warning, message)
  • is_compatible: True if versions match or cannot be determined, False if mismatch

  • is_warning: True if this is a warning (non-fatal), False otherwise

  • message: Descriptive message about the validation result

Module contents

Common utilities for the CodeFlare SDK.

codeflare_sdk.common.utils.get_current_namespace()[source]

Retrieves the current Kubernetes namespace.

Returns:
str:

The current namespace or None if not found.