# Copyright 2021 IBM Corporation
#
# 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.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: model-mesh
  labels:
    component: model-mesh
spec:
  replicas: 2
  selector:
    matchLabels:
      app: model-mesh
      component: model-mesh
  template:
    metadata:
      labels:
        app: model-mesh
        component: model-mesh
        # Required in Watson Cloud envs to allow outbound access to etcd
        network-policy: allow-egress
    spec:
      containers:
        - name: mm
          image: kserve/modelmesh:latest

          ports:
            - name: grpc
              containerPort: 8033
          env:
            - name: MM_SERVICE_NAME
              value: $(MM_SERVICE_NAME)
              # External gRPC port of the service, should match ports.containerPort
            - name: MM_SVC_GRPC_PORT
              value: "8033"
            - name: WKUBE_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: WKUBE_POD_IPADDR
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: MM_LOCATION
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
              # Overriden by etcd_patch.yaml
            - name: KV_STORE
              value: etcd:model-mesh-etcd:2379
              # Use prometheus_metrics_patch.yaml to override
            - name: MM_METRICS
              value: disabled

              # set this to equal terminationGracePeriodSeconds setting
            - name: SHUTDOWN_TIMEOUT_MS
              value: "90000"

          resources:
            requests:
              cpu: 1500m
            limits:
              cpu: 7500m
              memory: 1280Mi

          readinessProbe:
            httpGet:
              path: /ready
              port: 8089
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 1

          livenessProbe:
            httpGet:
              path: /live
              port: 8089
            initialDelaySeconds: 90
            periodSeconds: 30
            timeoutSeconds: 5
            failureThreshold: 2

          lifecycle:
            preStop:
              exec:
                command: [/opt/kserve/mmesh/stop.sh, wait]

      # Kustomize overlays will add a model runtime container here

      # longer grace period to allow for model propagation
      terminationGracePeriodSeconds: 90

  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 15%
