Skip to content

Commit 150ffdd

Browse files
committed
RHOAIENG-27175: add RayCluster name validation
rh-pre-commit.version: 2.3.2 rh-pre-commit.check-secrets: ENABLED
1 parent b74b01b commit 150ffdd

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/codeflare_sdk/ray/cluster/config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
import pathlib
22+
import re
2223
import warnings
2324
from dataclasses import dataclass, field, fields
2425
from typing import Dict, List, Optional, Union, get_args, get_origin
@@ -38,6 +39,20 @@
3839
"huawei.com/Ascend310": "NPU",
3940
}
4041

42+
# Kubernetes metadata.name must be a lowercase RFC 1123 subdomain
43+
_RFC1123_SUBDOMAIN = re.compile(
44+
r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$"
45+
)
46+
47+
48+
def _validate_cluster_name(name: str):
49+
"""Raise ValueError if name is not a valid Kubernetes metadata.name (RFC 1123 subdomain)."""
50+
if not name or not _RFC1123_SUBDOMAIN.match(name):
51+
raise ValueError(
52+
"Cluster name must be a valid RFC 1123 subdomain "
53+
"(lowercase, numbers, hyphens/dots; start and end with letter or number)."
54+
)
55+
4156

4257
@dataclass
4358
class ClusterConfiguration:
@@ -171,6 +186,7 @@ def __post_init__(self):
171186
self._validate_extended_resource_requests(
172187
self.worker_extended_resource_requests
173188
)
189+
_validate_cluster_name(self.name)
174190

175191
def _combine_extended_resource_mapping(self):
176192
if overwritten := set(self.extended_resource_mapping.keys()).intersection(

src/codeflare_sdk/ray/cluster/test_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ def test_ray_usage_stats_enabled(mocker):
201201
assert env_vars["RAY_USAGE_STATS_ENABLED"] == "1"
202202

203203

204+
def test_cluster_name_validation():
205+
with pytest.raises(ValueError):
206+
ClusterConfiguration(name="TestCluster", namespace="ns")
207+
with pytest.raises(ValueError):
208+
ClusterConfiguration(name="testcluster-", namespace="ns")
209+
with pytest.raises(ValueError):
210+
ClusterConfiguration(name="-testcluster", namespace="ns")
211+
212+
204213
# Make sure to always keep this function last
205214
def test_cleanup():
206215
os.remove(f"{cluster_dir}test-all-params.yaml")

src/codeflare_sdk/ray/rayjobs/test/test_rayjob.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from ray.runtime_env import RuntimeEnv
1919

2020
from codeflare_sdk.ray.rayjobs.rayjob import RayJob
21-
from codeflare_sdk.ray.cluster.config import ClusterConfiguration
2221
from codeflare_sdk.ray.rayjobs.config import ManagedClusterConfig
2322
from kubernetes.client import V1Volume, V1VolumeMount, V1Toleration
2423

@@ -80,7 +79,7 @@ def test_rayjob_init_validation_both_provided(auto_mock_setup):
8079
"""
8180
Test that providing both cluster_name and cluster_config raises error.
8281
"""
83-
cluster_config = ClusterConfiguration(name="test-cluster", namespace="test")
82+
cluster_config = ManagedClusterConfig()
8483

8584
with pytest.raises(
8685
ValueError,
@@ -145,9 +144,7 @@ def test_rayjob_init_with_cluster_config(auto_mock_setup):
145144
"""
146145
Test RayJob initialization with cluster configuration for auto-creation.
147146
"""
148-
cluster_config = ClusterConfiguration(
149-
name="auto-cluster", namespace="test-namespace", num_workers=2
150-
)
147+
cluster_config = ManagedClusterConfig(num_workers=2)
151148

152149
rayjob = RayJob(
153150
job_name="test-job",
@@ -166,9 +163,7 @@ def test_rayjob_cluster_name_generation(auto_mock_setup):
166163
"""
167164
Test that cluster names are generated when config has empty name.
168165
"""
169-
cluster_config = ClusterConfiguration(
170-
name="", # Empty name should trigger generation
171-
namespace="test-namespace",
166+
cluster_config = ManagedClusterConfig(
172167
num_workers=1,
173168
)
174169

@@ -186,9 +181,7 @@ def test_rayjob_cluster_config_namespace_none(auto_mock_setup):
186181
"""
187182
Test that cluster config namespace is set when None.
188183
"""
189-
cluster_config = ClusterConfiguration(
190-
name="test-cluster",
191-
namespace=None, # This should be set to job namespace
184+
cluster_config = ManagedClusterConfig(
192185
num_workers=1,
193186
)
194187

0 commit comments

Comments
 (0)