# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "tensorflow-notebook.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-notebook.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-notebook.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "tensorflow-notebook.fullname" . }}
  labels:
    app: {{ template "tensorflow-notebook.name" . }}
    chart: {{ template "tensorflow-notebook.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "tensorflow-notebook.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "tensorflow-notebook.name" . }}
        release: {{ .Release.Name }}
    spec:
      containers:
        - name: jupyter
          image: "{{ .Values.jupyter.image.repository }}:{{ .Values.jupyter.image.tag }}"
          imagePullPolicy: {{ .Values.jupyter.image.pullPolicy }}
          ports:
            - containerPort: 8888
              name: jupyter
          command:
            - jupyter
          args:
            - notebook
            - --allow-root      
          env:
            - name: PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "tensorflow-notebook.fullname" . }}
                  key: password
          livenessProbe:
            httpGet:
              path: /
              port: 8888
          readinessProbe:
            httpGet:
              path: /
              port: 8888
          volumeMounts:
            - mountPath: /output/training_logs
              name: training-logs-volume
        - name: tensorboard
          image: "{{ .Values.tensorboard.image.repository }}:{{ .Values.tensorboard.image.tag }}"
          imagePullPolicy: {{ .Values.tensorboard.image.pullPolicy }}
          ports:
            - containerPort: 6006
              name: tensorboard
          command:
            - tensorboard
          args:
            - --logdir
            - /output/training_logs
            - --host
            - 0.0.0.0
          livenessProbe:
            httpGet:
              path: /
              port: 6006
          readinessProbe:
            httpGet:
              path: /
              port: 6006
          volumeMounts:
            - mountPath: /output/training_logs
              name: training-logs-volume
      volumes:
        - name: training-logs-volume
          emptyDir: {}
 # secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "tensorflow-notebook.fullname" . }}
  labels:
    app: {{ template "tensorflow-notebook.name" . }}
    chart: {{ template "tensorflow-notebook.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
type: Opaque
data:
  password: {{ .Values.jupyter.password | b64enc | quote }} # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ template "tensorflow-notebook.fullname" . }}
  labels:
    app: {{ template "tensorflow-notebook.name" . }}
    chart: {{ template "tensorflow-notebook.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: 6006
      targetPort: tensorboard
      protocol: TCP
      name: tensorboard
    - port: 8888
      targetPort: jupyter
      protocol: TCP
      name: jupyter
  selector:
    app: {{ template "tensorflow-notebook.name" . }}
    release: {{ .Release.Name }}

# Default values for ack-tensorflow-dev.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
jupyter:
  image:
    repository: tensorflow/tensorflow
    tag: 1.6.0-devel
    pullPolicy: IfNotPresent
  password: tensorflow
  resources: {}
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  #  nvidia.com/gpu: 1
  # requests:
  #  cpu: 100m
  #  memory: 128Mi
  #  nvidia.com/gpu: 1
tensorboard:
  image:
    repository: tensorflow/tensorflow
    tag: 1.6.0-devel
    pullPolicy: IfNotPresent
service:
  type: LoadBalancer
