1313# permissions and limitations under the License.
1414from pathlib import Path
1515from typing import Dict , Optional
16-
16+ import re
1717import yaml
1818from kubernetes import client , config
1919from kubernetes .client .rest import ApiException
@@ -98,26 +98,17 @@ def deploy_model_to_k8s(
9898 docker_image_tag : str ,
9999 namespace : str = "default"
100100) -> Dict :
101- """Deploy a service to Kubernetes with the specified docker image and tag.
102-
103- Args:
104- docker_image: The full docker image name (e.g. "organization/image-name")
105- docker_image_tag: The tag to use for the docker image
106- namespace: Kubernetes namespace to deploy to (default: "default")
107-
108- Returns:
109- dict: Dictionary containing deployment information
110- """
111- # Get model name from context
112- model_name = get_step_context ().model .name
101+ # Get the raw model name
102+ raw_model_name = get_step_context ().model .name
103+ # Sanitize the model name
104+ model_name = sanitize_name (raw_model_name )
113105
114106 # Read the K8s template
115107 template_path = Path (__file__ ).parent / "k8s_template.yaml"
116108 with open (template_path , "r" ) as f :
117- # Load all documents in the YAML file
118109 k8s_configs = list (yaml .safe_load_all (f ))
119110
120- # Update both Service and Deployment configurations
111+ # Update configurations with sanitized names
121112 for config in k8s_configs :
122113 # Add namespace
123114 config ["metadata" ]["namespace" ] = namespace
@@ -161,4 +152,15 @@ def deploy_model_to_k8s(
161152 "configurations" : k8s_configs
162153 }
163154
164- return deployment_info
155+ return deployment_info
156+
157+
158+
159+ def sanitize_name (name : str ) -> str :
160+ # Convert to lowercase and replace invalid characters with '-'
161+ sanitized = re .sub (r"[^a-z0-9-]" , "-" , name .lower ())
162+ # Trim to a maximum length of 63 characters and strip leading/trailing '-'
163+ sanitized = sanitized [:63 ].strip ("-" )
164+ # Ensure the name doesn't start or end with '-'
165+ sanitized = sanitized .strip ("-" )
166+ return sanitized
0 commit comments