# Copyright 2018 The Kubeflow Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Deploying a trained model to Cloud Machine Learning Engine
description: |
  A Kubeflow Pipeline component to deploy a trained model from a Cloud Storage
  path to a Cloud Machine Learning Engine service.
metadata:
  labels:
    add-pod-env: 'true'
inputs:
  - name: model_uri
    description: >-
      Required. The Cloud Storage URI which contains a model file. Commonly 
      used TF model search paths (export/exporter) will be used if they exist.
    type: GCSPath
  - name: project_id
    description: 'Required.The ID of the parent project of the serving model.'
    type: GCPProjectID
  - name: model_id
    description: >-
      Optional. The user-specified name of the model. If it is not provided, 
      the operation uses a random name.
    default: ''
    type: String
  - name: version_id
    description: >-
      Optional. The user-specified name of the version. If it is not provided, 
      the operation uses a random name.
    default: ''
    type: String
  - name: runtime_version
    description: >-
      Optional. The [Cloud ML Engine runtime version](https://cloud.google.com/ml-engine/docs/tensorflow/runtime-version-list) to use for 
      this deployment. If it is not set, the Cloud ML Engine uses the default 
      stable version, 1.0.
    default: ''
    type: String
  - name: python_version
    description: >-
      Optional. The version of Python used in the prediction. If it is not set, 
      the default version is `2.7`. Python `3.5` is available when the 
      runtime_version is set to `1.4` and above. Python `2.7` works with all 
      supported runtime versions.
    default: ''
    type: String
  - name: model
    description: >-
      Optional. The JSON payload of the new 
      [Model](https://cloud.google.com/ml-engine/reference/rest/v1/projects.models), if it does not exist.
    default: ''
    type: Dict
  - name: version
    description: >-
      Optional. The JSON payload of the new 
      [Version](https://cloud.google.com/ml-engine/reference/rest/v1/projects.models.versions).
    default: ''
    type: Dict
  - name: replace_existing_version
    description: >-
      A Boolean flag that indicates whether to replace existing version in case of conflict.
    default: 'False'
    type: Bool
  - name: set_default
    description: >-
      A Boolean flag that indicates whether to set the new version as default version in the model.
    default: 'False'
    type: Bool
  - name: wait_interval
    description: 'A time-interval to wait for in case the operation has a long run time.'
    default: '30'
    type: Integer
outputs:
  - name: model_uri
    description: 'The Cloud Storage URI of the trained model.'
    type: GCSPath
  - name: model_name
    description: 'The name of the deployed model.'
    type: String
  - name: version_name
    description: 'The name of the deployed version.'
    type: String
  - name: MLPipeline UI metadata
    type: UI metadata
implementation:
  container:
    image: gcr.io/ml-pipeline/ml-pipeline-gcp:1.7.0-rc.3
    command: ["python", -u, -m, "kfp_component.launcher"]
    args: [
      --ui_metadata_path, {outputPath: MLPipeline UI metadata},
      kfp_component.google.ml_engine, deploy,
      --model_uri, {inputValue: model_uri},
      --project_id, {inputValue: project_id},
      --model_id, {inputValue: model_id},
      --version_id, {inputValue: version_id},
      --runtime_version, {inputValue: runtime_version},
      --python_version, {inputValue: python_version},
      --model, {inputValue: model},
      --version, {inputValue: version},
      --replace_existing_version, {inputValue: replace_existing_version},
      --set_default, {inputValue: set_default},
      --wait_interval, {inputValue: wait_interval},
      --model_uri_output_path, {outputPath: model_uri},
      --model_name_output_path, {outputPath: model_name},
      --version_name_output_path, {outputPath: version_name},
    ]
    env:
      KFP_POD_NAME: "{{pod.name}}"
