{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "jupyterhub.proxy.name" . }}
  namespace: {{ .Release.Namespace | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.proxy.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: proxy
  {{- 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 }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.proxy.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | 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" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: proxy
        hub.jupyter.org/network-access-hub: "true"
        hub.jupyter.org/network-access-singleuser: "true"
    spec:
      serviceAccountName: {{ template "jupyterhub.proxyServiceAccountName" . }}
      automountServiceAccountToken: {{ .Values.proxy.automountServiceAccountToken }}
      {{- 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" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.proxy.podAntiAffinityPreset "component" "proxy" "customLabels" $podLabels "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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.proxy.podSecurityContext "context" $) | 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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.proxy.containerSecurityContext "context" $) | 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:
            - "--ip=::"
            - "--api-ip=::"
            - --api-port={{ .Values.proxy.containerPort.api }}
            - --default-target=http://{{ template "common.names.fullname" . }}-hub:{{ .Values.hub.service.ports.http }}
            - --error-target=http://{{ template "common.names.fullname" . }}-hub:{{ .Values.hub.service.ports.http }}/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 }}
          {{- else if ne .Values.proxy.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.proxy.resourcesPreset) | 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 }}
            tcpSocket:
              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:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- 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 }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.proxy.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.proxy.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
