# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "tensorflow-serving.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "tensorflow-serving.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "tensorflow-serving.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # deployment.yaml
{{- $gpuCount := .Values.gpuCount -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "tensorflow-serving.fullname" . }}
  labels:
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
    chart: {{ template "tensorflow-serving.chart" . }}
    app: {{ template "tensorflow-serving.name" . }}
spec:
  replicas: {{ .Values.replicas }}
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      release: {{ .Release.Name | quote }}
      app: {{ template "tensorflow-serving.name" . }} 
  template:
    metadata:
      labels:
        heritage: {{ .Release.Service | quote }}
        release: {{ .Release.Name | quote }}
        chart: {{ template "tensorflow-serving.chart" . }}
        app: {{ template "tensorflow-serving.name" . }}
    spec:
      containers:
        - name: serving
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: "{{ .Values.image.pullPolicy }}"
          command:
            - "/usr/bin/tensorflow_model_server"
          args:
            - "--port=9090"
            - "--model_name={{ .Values.modelName }}"
            - "--model_base_path={{ .Values.modelBasePath }}"
          ports:
            - containerPort: 9090
              name: serving
              protocol: TCP
          readinessProbe:
            tcpSocket:
              port: serving
            initialDelaySeconds: 15
            timeoutSeconds: 1
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
          {{- if .Values.mountPath }}
            - mountPath: {{ .Values.mountPath }}
          {{- else }}
            - mountPath: /data
          {{- end }}
              name: model-volume
      volumes:
        - name: model-volume
      {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
              claimName: {{ .Values.persistence.existingClaim | default (include "tensorflow-serving.fullname" .) }}
      {{- else }}
          emptyDir: {}
      {{- end }}
 # pvc.yaml
{{- if .Values.persistence.enabled }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ template "tensorflow-serving.fullname" . }}
  labels:
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
    chart: {{ template "tensorflow-serving.chart" . }}
    app: {{ template "tensorflow-serving.fullname" . }}
  annotations:
    "helm.sh/hook": pre-install
spec:
  accessModes:
    - {{ .Values.persistence.accessMode | quote }}
  resources:
    requests:
      storage: {{ .Values.persistence.size | quote }}
{{- if .Values.persistence.storageClass }}
{{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
{{- else }}
  storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
{{- end }}
{{- if .Values.persistence.matchLabels }}
  selector:
    matchLabels:
{{ toYaml .Values.persistence.pvc.matchLabels | indent 12 }}
{{- end}}
{{- end}}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "tensorflow-serving.fullname" . }}
  labels:
    heritage: {{ .Release.Service | quote }}
    release: {{ .Release.Name | quote }}
    chart: {{ template "tensorflow-serving.chart" . }}
    app: {{ template "tensorflow-serving.name" . }}
spec:
  type: {{ .Values.serviceType }}
  ports:
    - name: serving
      port: 9090
      targetPort: serving
  selector:
    release: {{ .Release.Name | quote }}
    app: {{ template "tensorflow-serving.name" . }}
# Default values for tensorflow-serving.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

## Kubernetes configuration
## support NodePort, LoadBalancer
##
serviceType: LoadBalancer

## expose the service to the grpc client
port: 9090
replicas: 1

# repository: "cheyang/tf-model-server-gpu"
image:
  repository: "cheyang/tf-model-server"
  tag: "1.4"
  pullPolicy: "IfNotPresent"
resources: {}
  #  limits:
  #    cpu: 1.0
  #    memory: 512Mi
  #    nvidia.com/gpu: 1
  #  requests:
  #    cpu: 1.0
  #    memory: 512Mi
  #    nvidia.com/gpu: 1


## The command and args to run the pod
modelName: "inception"
modelBasePath: "/serving/inception-export"

## the mount path inside the container
# mountPath: /serving/inception-export

persistence:
  enabled: true
  storageClass: ""
  size: 10Gi
  accessMode: ReadWriteOnce
 # matchLabels: {}
