1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1212# or implied. See the License for the specific language governing
1313# permissions and limitations under the License.
14- from pathlib import Path
15- from typing import Dict , Optional
1614import re
15+ from pathlib import Path
16+ from typing import Dict , Optional , cast
17+
1718import yaml
1819from kubernetes import client , config
1920from kubernetes .client .rest import ApiException
2021from zenml import get_step_context , step
2122from zenml .client import Client
23+ from zenml .integrations .bentoml .services .bentoml_local_deployment import (
24+ BentoMLLocalDeploymentConfig ,
25+ BentoMLLocalDeploymentService ,
26+ )
2227from zenml .logger import get_logger
28+ from zenml .orchestrators .utils import get_config_environment_vars
2329
2430logger = get_logger (__name__ )
2531
@@ -93,7 +99,7 @@ def apply_kubernetes_configuration(k8s_configs: list) -> None:
9399 logger .error (f"Error applying { kind } { name } : { e } " )
94100 raise e
95101
96- @step
102+ @step ( enable_cache = False )
97103def k8s_deployment (
98104 docker_image_tag : str ,
99105 namespace : str = "default"
@@ -103,6 +109,17 @@ def k8s_deployment(
103109 # Sanitize the model name
104110 model_name = sanitize_name (raw_model_name )
105111
112+ # Get environment variables
113+ environment_vars = get_config_environment_vars ()
114+
115+ # Get current deployment
116+ zenml_client = Client ()
117+ model_deployer = zenml_client .active_stack .model_deployer
118+ services = model_deployer .find_model_server (
119+ model_name = model_name ,
120+ model_version = "production" ,
121+ )
122+
106123 # Read the K8s template
107124 template_path = Path (__file__ ).parent / "k8s_template.yaml"
108125 with open (template_path , "r" ) as f :
@@ -120,6 +137,23 @@ def k8s_deployment(
120137 if config ["kind" ] == "Service" :
121138 # Update service selector
122139 config ["spec" ]["selector" ]["app" ] = model_name
140+
141+ # Update metadata annotations with SSL certificate ARN
142+ config ["metadata" ]["annotations" ] = {
143+ "service.beta.kubernetes.io/aws-load-balancer-ssl-cert" : "arn:aws:acm:eu-central-1:339712793861:certificate/0426ace8-5fa3-40dd-bd81-b0fb1064bd85" ,
144+ "service.beta.kubernetes.io/aws-load-balancer-backend-protocol" : "http" ,
145+ "service.beta.kubernetes.io/aws-load-balancer-ssl-ports" : "443" ,
146+ "service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout" : "3600"
147+ }
148+
149+ # Update ports
150+ config ["spec" ]["ports" ] = [
151+ {
152+ "name" : "https" ,
153+ "port" : 443 ,
154+ "targetPort" : 3000
155+ }
156+ ]
123157
124158 elif config ["kind" ] == "Deployment" :
125159 # Update deployment selector and template
@@ -131,6 +165,12 @@ def k8s_deployment(
131165 for container in containers :
132166 container ["name" ] = model_name
133167 container ["image" ] = docker_image_tag
168+
169+ # Add environment variables to the container
170+ env_vars = []
171+ for key , value in environment_vars .items ():
172+ env_vars .append ({"name" : key , "value" : value })
173+ container ["env" ] = env_vars
134174
135175 # Apply the configurations
136176 try :
@@ -149,9 +189,22 @@ def k8s_deployment(
149189 "namespace" : namespace ,
150190 "status" : deployment_status ,
151191 "service_port" : 3000 ,
152- "configurations" : k8s_configs
192+ "configurations" : k8s_configs ,
193+ "url" : "chat-rag.staging.cloudinfra.zenml.io"
153194 }
154195
196+ if services :
197+ bentoml_deployment = cast (BentoMLLocalDeploymentService , services [0 ])
198+ zenml_client .update_service (
199+ id = bentoml_deployment .uuid ,
200+ prediction_url = "https://chat-rag.staging.cloudinfra.zenml.io" ,
201+ health_check_url = "https://chat-rag.staging.cloudinfra.zenml.io/healthz" ,
202+ labels = {
203+ "docker_image" : docker_image_tag ,
204+ "namespace" : namespace ,
205+ }
206+ )
207+
155208 return deployment_info
156209
157210
0 commit comments