Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in violating Kubernetes naming rules #244

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/cloudai/_core/json_gen_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from abc import abstractmethod
from typing import Any, Dict

Expand All @@ -28,6 +29,27 @@ class JsonGenStrategy(TestTemplateStrategy):
It specifies how to generate JSON job specifications based on system and test parameters.
"""

def sanitize_k8s_job_name(self, job_name: str) -> str:
"""
Sanitize the job name to ensure it follows Kubernetes naming rules.

- Must be lowercase.
- Can only contain alphanumeric characters, hyphens, and periods.
- Must start and end with an alphanumeric character.
- Must be at most 253 characters long.

Args:
job_name (str): The original job name to be sanitized.

Returns:
str: The sanitized job name that complies with Kubernetes naming rules.
"""
sanitized_name = job_name.lower()
sanitized_name = re.sub(r"[^a-z0-9.-]", "-", sanitized_name)
sanitized_name = re.sub(r"^[^a-z0-9]+", "", sanitized_name)
sanitized_name = re.sub(r"[^a-z0-9]+$", "", sanitized_name)
return sanitized_name[:253]

@abstractmethod
def gen_json(self, tr: TestRun) -> Dict[Any, Any]:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def gen_json(self, tr: TestRun) -> Dict[Any, Any]:
final_env_vars = self._override_env_vars(self.system.global_env_vars, tr.test.extra_env_vars)
final_cmd_args = self._override_cmd_args(self.default_cmd_args, tr.test.cmd_args)
final_num_nodes = self._determine_num_nodes(tr.num_nodes, tr.nodes)
sanitized_job_name = self.sanitize_k8s_job_name("nccl-test")
job_spec = self._create_job_spec(
tr.name, final_num_nodes, tr.nodes, final_env_vars, final_cmd_args, tr.test.extra_cmd_args
TaekyungHeo marked this conversation as resolved.
Show resolved Hide resolved
sanitized_job_name, final_num_nodes, tr.nodes, final_env_vars, final_cmd_args, tr.test.extra_cmd_args
)

return job_spec
Expand Down