@@ -213,90 +213,98 @@ def create_deployment(
213
213
214
214
yatai_service = get_yatai_service ()
215
215
216
- try :
217
- # Make sure there is no active deployment with the same deployment name
218
- get_deployment_pb = yatai_service .GetDeployment (
219
- GetDeploymentRequest (deployment_name = deployment_name , namespace = namespace )
216
+ # Make sure there is no active deployment with the same deployment name
217
+ get_deployment_pb = yatai_service .GetDeployment (
218
+ GetDeploymentRequest (deployment_name = deployment_name , namespace = namespace )
219
+ )
220
+ if get_deployment_pb .status .status_code == status_pb2 .Status .OK :
221
+ raise BentoMLDeploymentException (
222
+ 'Deployment "{name}" already existed, use Update or Apply for updating '
223
+ 'existing deployment, delete the deployment, or use a different deployment '
224
+ 'name' .format (name = deployment_name )
220
225
)
221
- if get_deployment_pb .status .status_code == status_pb2 .Status .OK :
222
- raise BentoMLDeploymentException (
223
- 'Deployment "{name}" already existed, use Update or Apply for updating'
224
- 'existing deployment, or create the deployment with a different name or'
225
- 'under a different deployment namespace' .format (name = deployment_name )
226
- )
227
- if get_deployment_pb .status .status_code != status_pb2 .Status .NOT_FOUND :
228
- raise BentoMLDeploymentException (
229
- 'Failed accesing YataiService deployment store. {error_code}:'
230
- '{error_message}' .format (
231
- error_code = Status .Name (get_deployment_pb .status .status_code ),
232
- error_message = get_deployment_pb .status .error_message ,
233
- )
226
+ if get_deployment_pb .status .status_code != status_pb2 .Status .NOT_FOUND :
227
+ raise BentoMLDeploymentException (
228
+ 'Failed accesing YataiService deployment store. {error_code}:'
229
+ '{error_message}' .format (
230
+ error_code = Status .Name (get_deployment_pb .status .status_code ),
231
+ error_message = get_deployment_pb .status .error_message ,
234
232
)
233
+ )
235
234
236
- deployment_dict = {
237
- "name" : deployment_name ,
238
- "namespace" : namespace or config ().get ('deployment' , 'default_namespace' ),
239
- "labels" : labels ,
240
- "annotations" : annotations ,
241
- "spec" : {
242
- "bento_name" : bento_name ,
243
- "bento_version" : bento_version ,
244
- "operator" : platform ,
245
- },
235
+ deployment_dict = {
236
+ "name" : deployment_name ,
237
+ "namespace" : namespace or config ().get ('deployment' , 'default_namespace' ),
238
+ "labels" : labels ,
239
+ "annotations" : annotations ,
240
+ "spec" : {
241
+ "bento_name" : bento_name ,
242
+ "bento_version" : bento_version ,
243
+ "operator" : platform ,
244
+ },
245
+ }
246
+
247
+ operator = platform .replace ('-' , '_' ).upper ()
248
+ try :
249
+ operator_value = DeploymentSpec .DeploymentOperator .Value (operator )
250
+ except ValueError :
251
+ return ApplyDeploymentResponse (
252
+ status = Status .INVALID_ARGUMENT ('Invalid platform "{}"' .format (platform ))
253
+ )
254
+ if operator_value == DeploymentSpec .AWS_SAGEMAKER :
255
+ deployment_dict ['spec' ]['sagemaker_operator_config' ] = {
256
+ 'region' : operator_spec .get ('region' )
257
+ or config ().get ('aws' , 'default_region' ),
258
+ 'instance_count' : operator_spec .get ('instance_count' )
259
+ or config ().getint ('sagemaker' , 'default_instance_count' ),
260
+ 'instance_type' : operator_spec .get ('instance_type' )
261
+ or config ().get ('sagemaker' , 'default_instance_type' ),
262
+ 'api_name' : operator_spec .get ('api_name' , '' ),
263
+ }
264
+ elif operator_value == DeploymentSpec .AWS_LAMBDA :
265
+ deployment_dict ['spec' ]['aws_lambda_operator_config' ] = {
266
+ 'region' : operator_spec .get ('region' )
267
+ or config ().get ('aws' , 'default_region' )
268
+ }
269
+ for field in ['api_name' , 'memory_size' , 'timeout' ]:
270
+ if operator_spec .get (field ):
271
+ deployment_dict ['spec' ]['aws_lambda_operator_config' ][
272
+ field
273
+ ] = operator_spec [field ]
274
+ elif operator_value == DeploymentSpec .GCP_FCUNTION :
275
+ deployment_dict ['spec' ]['gcp_function_operatorConfig' ] = {
276
+ 'region' : operator_spec .get ('region' )
277
+ or config ().get ('google-cloud' , 'default_region' )
246
278
}
279
+ if operator_spec .get ('api_name' ):
280
+ deployment_dict ['spec' ]['gcp_function_operator_config' ][
281
+ 'api_name'
282
+ ] = operator_spec ['api_name' ]
283
+ elif operator_value == DeploymentSpec .KUBERNETES :
284
+ deployment_dict ['spec' ]['kubernetes_operator_config' ] = {
285
+ 'kube_namespace' : operator_spec .get ('kube_namespace' , '' ),
286
+ 'replicas' : operator_spec .get ('replicas' , 0 ),
287
+ 'service_name' : operator_spec .get ('service_name' , '' ),
288
+ 'service_type' : operator_spec .get ('service_type' , '' ),
289
+ }
290
+ else :
291
+ raise BentoMLDeploymentException (
292
+ 'Platform "{}" is not supported in the current version of '
293
+ 'BentoML' .format (platform )
294
+ )
247
295
248
- operator = platform .replace ('-' , '_' ).upper ()
249
- try :
250
- operator_value = DeploymentSpec .DeploymentOperator .Value (operator )
251
- except ValueError :
252
- return ApplyDeploymentResponse (
253
- status = Status .INVALID_ARGUMENT ('Invalid platform "{}"' .format (platform ))
254
- )
255
- if operator_value == DeploymentSpec .AWS_SAGEMAKER :
256
- deployment_dict ['spec' ]['sagemaker_operator_config' ] = {
257
- 'region' : operator_spec .get ('region' )
258
- or config ().get ('aws' , 'default_region' ),
259
- 'instance_count' : operator_spec .get ('instance_count' )
260
- or config ().getint ('sagemaker' , 'default_instance_count' ),
261
- 'instance_type' : operator_spec .get ('instance_type' )
262
- or config ().get ('sagemaker' , 'default_instance_type' ),
263
- 'api_name' : operator_spec .get ('api_name' , '' ),
264
- }
265
- elif operator_value == DeploymentSpec .AWS_LAMBDA :
266
- deployment_dict ['spec' ]['aws_lambda_operator_config' ] = {
267
- 'region' : operator_spec .get ('region' )
268
- or config ().get ('aws' , 'default_region' )
269
- }
270
- for field in ['api_name' , 'memory_size' , 'timeout' ]:
271
- if operator_spec .get (field ):
272
- deployment_dict ['spec' ]['aws_lambda_operator_config' ][
273
- field
274
- ] = operator_spec [field ]
275
- elif operator_value == DeploymentSpec .GCP_FCUNTION :
276
- deployment_dict ['spec' ]['gcp_function_operatorConfig' ] = {
277
- 'region' : operator_spec .get ('region' )
278
- or config ().get ('google-cloud' , 'default_region' )
279
- }
280
- if operator_spec .get ('api_name' ):
281
- deployment_dict ['spec' ]['gcp_function_operator_config' ][
282
- 'api_name'
283
- ] = operator_spec ['api_name' ]
284
- elif operator_value == DeploymentSpec .KUBERNETES :
285
- deployment_dict ['spec' ]['kubernetes_operator_config' ] = {
286
- 'kube_namespace' : operator_spec .get ('kube_namespace' , '' ),
287
- 'replicas' : operator_spec .get ('replicas' , 0 ),
288
- 'service_name' : operator_spec .get ('service_name' , '' ),
289
- 'service_type' : operator_spec .get ('service_type' , '' ),
290
- }
291
- else :
292
- raise BentoMLDeploymentException (
293
- 'Platform "{}" is not supported in the current version of '
294
- 'BentoML' .format (platform )
295
- )
296
+ apply_response = apply_deployment (deployment_dict , yatai_service )
296
297
297
- return apply_deployment (deployment_dict , yatai_service )
298
- except BentoMLException as error :
299
- return ApplyDeploymentResponse (status = Status .INTERNAL (str (error )))
298
+ if apply_response .status .status_code == status_pb2 .Status .OK :
299
+ describe_response = describe_deployment (
300
+ deployment_name , namespace , yatai_service
301
+ )
302
+ if describe_response .status .status_code == status_pb2 .Status .OK :
303
+ deployment_state = describe_response .state
304
+ apply_response .deployment .state .CopyFrom (deployment_state )
305
+ return apply_response
306
+
307
+ return apply_response
300
308
301
309
302
310
# TODO update_deployment is not finished. It will be working on along with cli command
0 commit comments