apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "jupyterhub.proxy.name" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: proxy
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.proxy.updateStrategy }}
  strategy: {{- toYaml .Values.proxy.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: proxy
  template:
    metadata:
      annotations:
        {{- if .Values.proxy.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.proxy.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        checksum/hub-secret: {{ include (print $.Template.BasePath "/hub/secret.yaml") . | sha256sum }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: proxy
        hub.jupyter.org/network-access-hub: "true"
        hub.jupyter.org/network-access-singleuser: "true"
        {{- if .Values.proxy.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.proxy.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "jupyterhub.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.proxy.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.proxy.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.proxy.podAffinityPreset "component" "proxy" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.proxy.podAntiAffinityPreset "component" "proxy" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.proxy.nodeAffinityPreset.type "key" .Values.proxy.nodeAffinityPreset.key "values" .Values.proxy.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.proxy.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.proxy.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.priorityClassName }}
      priorityClassName: {{ .Values.proxy.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.proxy.schedulerName }}
      schedulerName: {{ .Values.proxy.schedulerName }}
      {{- end }}
      {{- if .Values.proxy.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.proxy.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.proxy.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.proxy.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.proxy.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: proxy
          image: {{ template "jupyterhub.proxy.image" . }}
          imagePullPolicy: {{ .Values.proxy.image.pullPolicy }}
          {{- if .Values.proxy.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.proxy.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.proxy.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.proxy.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.proxy.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - configurable-http-proxy
            - "--ip=::"
            - "--api-ip=::"
            - --api-port={{ .Values.proxy.containerPort.api }}
            - --default-target=http://{{ template "common.names.fullname" . }}-hub:{{ coalesce .Values.hub.service.ports.http .Values.hub.service.port }}
            - --error-target=http://{{ template "common.names.fullname" . }}-hub:{{ coalesce .Values.hub.service.ports.http .Values.hub.service.port }}/hub/error
            - --port={{ .Values.proxy.containerPort.http }}
            {{- if .Values.proxy.metrics.serviceMonitor.enabled }}
            - "--metrics-ip=::"
            - --metrics-port={{ .Values.proxy.containerPort.metrics }}
            {{- end }}
            {{- if .Values.proxy.image.debug }}
            - --log-level=debug
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.proxy.containerPort.http }}
              protocol: TCP
            - name: api
              containerPort: {{ .Values.proxy.containerPort.api }}
              protocol: TCP
            {{- if .Values.proxy.metrics.serviceMonitor.enabled }}
            - name: metrics
              containerPort: {{ .Values.proxy.containerPort.metrics }}
              protocol: TCP
            {{- end }}
          env:
            - name: CONFIGPROXY_AUTH_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ include "jupyterhub.hubSecretName" . }}
                  key: proxy-token
            {{- if .Values.proxy.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.proxy.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.proxy.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.proxy.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.proxy.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.proxy.resources }}
          resources: {{- toYaml .Values.proxy.resources | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.proxy.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.proxy.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.proxy.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /_chp_healthz
              port: http
          {{- end }}
          {{- if .Values.proxy.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.proxy.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.proxy.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /_chp_healthz
              port: http
          {{- end }}
          {{- if .Values.proxy.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.proxy.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.proxy.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /_chp_healthz
              port: http
          {{- end }}
          {{- end }}
          volumeMounts:
          {{- if .Values.proxy.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.proxy.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.proxy.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.proxy.extraVolumes }}
      volumes: {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
