apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "tensorflow-resnet.fullname" . }}
  labels: {{- include "tensorflow-resnet.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{- include "tensorflow-resnet.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels: {{- include "tensorflow-resnet.labels" . | nindent 8 }}
      {{- if or .Values.podAnnotations (and .Values.metrics.enabled .Values.metrics.podAnnotations) }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "tensorflow-resnet.tplValue" ( dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.metrics.enabled .Values.metrics.podAnnotations }}
        {{- include "tensorflow-resnet.tplValue" ( dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
{{- include "tensorflow-resnet.imagePullSecrets" . | indent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "tensorflow-resnet.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "tensorflow-resnet.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "tensorflow-resnet.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        - name: seed
          image: {{ template "tensorflow-resnet.client.image" . }}
          imagePullPolicy: {{ .Values.client.image.pullPolicy | quote }}
          command:
            - "/bin/sh"
            - "-c"
            - |
              if [ -f /seed/.initialized ]; then
                echo "Already initialized. Skipping"
              else
                curl -o /seed/resnet_v2_fp32_savedmodel_NHWC_jpg.tar.gz http://download.tensorflow.org/models/official/20181001_resnet/savedmodels/resnet_v2_fp32_savedmodel_NHWC_jpg.tar.gz
                cd /seed/ && tar -xzf resnet_v2_fp32_savedmodel_NHWC_jpg.tar.gz --strip-components=2
                rm resnet_v2_fp32_savedmodel_NHWC_jpg.tar.gz
                touch /seed/.initialized
              fi
          volumeMounts:
            - name: seed
              mountPath: /seed
      containers:
        - name: tensorflow-serving
          image: "{{ template "tensorflow-resnet.server.image" . }}"
          imagePullPolicy: {{ .Values.server.image.pullPolicy | quote }}
          ports:
            - name: tf-serving
              containerPort: {{ .Values.containerPorts.server }}
            - name: tf-serving-api
              containerPort: {{ .Values.containerPorts.restApi }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: tf-serving
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: tf-serving
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          env:
            - name: TENSORFLOW_SERVING_MODEL_NAME
              value: "resnet"
            - name: TENSORFLOW_SERVING_PORT_NUMBER
              value: "{{ .Values.server.port }}"
            - name: TENSORFLOW_SERVING_REST_API_PORT_NUMBER
              value: "{{ .Values.server.restApiPort }}"
            - name: TENSORFLOW_SERVING_ENABLE_MONITORING
              value: {{ ternary "yes" "no" .Values.metrics.enabled | quote }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: seed
              mountPath: "/bitnami/model-data"
      volumes:
        - name: seed
          emptyDir: {}
