apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "jupyterlab.name" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "jupyterlab.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ include "jupyterlab.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        {{- include "jupyterlab.labels" . | nindent 8 }}
    spec:
      securityContext:
        runAsUser: 1000
        fsGroup: 100
        {{- if .Values.tcpKeepalive.enabled }}
        sysctls:
          - name: net.ipv4.tcp_keepalive_time
            value: {{ .Values.tcpKeepalive.time | quote }}
          - name: net.ipv4.tcp_keepalive_intvl
            value: {{ .Values.tcpKeepalive.intvl | quote }}
          - name: net.ipv4.tcp_keepalive_probes
            value: {{ .Values.tcpKeepalive.probes | quote }}
        {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.imagePullSecrets | nindent 8 }}
      {{- end }}
      containers:
        - name: jupyterlab
          image: {{ include "jupyterlab.image" . | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            {{- include "jupyterlab.command" . | nindent 12 }}
          ports:
            - containerPort: 8888
              protocol: TCP
          env:
            - name: NVIDIA_DRIVER_CAPABILITIES
              value: "compute,utility"
          resources:
            limits:
              cpu: {{ .Values.resources.limits.cpu | quote }}
              memory: {{ .Values.resources.limits.memory | quote }}
              {{- if .Values.resources.gpu }}
              nvidia.com/gpu: {{ .Values.resources.gpu | quote }}
              {{- end }}
            requests:
              cpu: {{ .Values.resources.requests.cpu | quote }}
              memory: {{ .Values.resources.requests.memory | quote }}
              {{- if .Values.resources.gpu }}
              nvidia.com/gpu: {{ .Values.resources.gpu | quote }}
              {{- end }}
          securityContext:
            runAsUser: 0
          readinessProbe:
            tcpSocket:
              port: 8888
            initialDelaySeconds: 30
            periodSeconds: 10
            failureThreshold: 27
          volumeMounts:
            - name: localtime
              mountPath: /etc/localtime
              readOnly: true
            - name: startup-script
              mountPath: /usr/local/bin/start-notebook.d/startup-script.sh
              subPath: startup-script.sh
            - name: startup-script
              mountPath: /etc/jupyter/jupyter_server_config.py
              subPath: jupyter_server_config.py
              readOnly: true
            {{- if .Values.homeVolume.enabled }}
            - name: home-volume
              mountPath: {{ include "jupyterlab.homeDir" . }}
            {{- end }}
            {{- if .Values.dataVolume.enabled }}
            - name: data-volume
              mountPath: {{ include "jupyterlab.dataDir" . }}
            {{- end }}
            {{- range .Values.groupVolumes }}
            - name: gv-{{ .claimName }}
              mountPath: {{ .mountPath }}
            {{- end }}
            - name: dshm
              mountPath: /dev/shm
      volumes:
        - name: localtime
          hostPath:
            path: /usr/share/zoneinfo/Asia/Seoul
        - name: startup-script
          configMap:
            name: {{ include "jupyterlab.configMapName" . }}
            defaultMode: 0755
        {{- if .Values.homeVolume.enabled }}
        - name: home-volume
          persistentVolumeClaim:
            claimName: {{ include "jupyterlab.homePvcName" . }}
        {{- end }}
        {{- if .Values.dataVolume.enabled }}
        - name: data-volume
          persistentVolumeClaim:
            claimName: {{ include "jupyterlab.dataPvcName" . }}
        {{- end }}
        {{- range .Values.groupVolumes }}
        - name: gv-{{ .claimName }}
          persistentVolumeClaim:
            claimName: {{ .claimName }}
        {{- end }}
        - name: dshm
          emptyDir:
            medium: Memory
            {{- if .Values.shmSizeLimit }}
            sizeLimit: {{ .Values.shmSizeLimit }}
            {{- end }}
