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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: oauth2-proxy
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: oauth2-proxy
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: oauth2-proxy
    spec:
      serviceAccountName: {{ template "oauth2-proxy.serviceAccountName" . }}
      {{- include "oauth2-proxy.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "oauth2-proxy" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "oauth2-proxy" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: oauth2-proxy
          image: {{ template "oauth2-proxy.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.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.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.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.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --http-address=0.0.0.0:{{ .Values.containerPort }}
            {{- if or .Values.configuration.existingConfigmap .Values.configuration.content }}
            - --config=/bitnami/oauth2-proxy/conf/oauth2_proxy.cfg
            {{- end }}
            {{- if .Values.configuration.authenticatedEmailsFile.enabled }}
            - --authenticated-emails-file=/bitnami/oauth2-proxy/conf/authenticated-emails/authenticated-emails-list
            {{- end }}
            {{- if .Values.configuration.google.enabled }}
            - --google-admin-email={{ .Values.configuration.google.adminEmail }}
            {{- if .Values.configuration.google.useApplicationDefaultCredentials }}
            - --google-use-application-default-credentials=true
            {{- else }}
            - --google-service-account-json=/bitnami/oauth2-proxy/conf/google/service-account.json
            {{- end }}
            {{- if .Values.configuration.google.groups }}
            {{- range $group := .Values.configuration.google.groups }}
            - --google-group={{ $group }}
            {{- end }}
            {{- end }}
            {{- end }}
            {{- if .Values.configuration.htpasswdFile.enabled }}
            - --htpasswd-file=/bitnami/oauth2-proxy/conf/htpasswd/users.txt
            {{- end }}
            {{- if .Values.configuration.redirectUrl }}
            - --redirect-url={{.Values.configuration.redirectUrl}}
            {{- end }}
            {{- if .Values.configuration.oidcIssuerUrl }}
            - --oidc-issuer-url={{.Values.configuration.oidcIssuerUrl}}
            {{- end }}
            {{- if .Values.configuration.whiteList }}
            - --whitelist-domain={{.Values.configuration.whiteList | quote}}
            {{- end }}
            {{- if .Values.extraArgs }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.extraArgs "context" $ ) | nindent 12 }}
            {{- end }}
          {{- end }}
          env:
            - name: OAUTH2_PROXY_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: {{ template "oauth2-proxy.secretName" . }}
                  key: client-id
            - name: OAUTH2_PROXY_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "oauth2-proxy.secretName" . }}
                  key: client-secret
            - name: OAUTH2_PROXY_COOKIE_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "oauth2-proxy.secretName" . }}
                  key: cookie-secret
            {{- if or .Values.redis.enabled .Values.externalRedis.host }}
            - name: OAUTH2_PROXY_SESSION_STORE_TYPE
              value: "redis"
            {{- if or .Values.redis.auth.enabled .Values.externalRedis.password }}
            - name: OAUTH2_PROXY_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "oauth2-proxy.redis.secretName" . }}
                  key: {{ include "oauth2-proxy.redis.secretPasswordKey" . }}
            {{- end }}
            - name: OAUTH2_PROXY_REDIS_CONNECTION_URL
              value: {{ include "oauth2-proxy.redis.url" . }}
            {{- if and .Values.redis.sentinel.enabled .Values.redis.enabled }}
            - name: OAUTH2_PROXY_REDIS_USE_SENTINEL
              value: "true"
            - name: OAUTH2_PROXY_REDIS_SENTINEL_MASTER_NAME
              value: {{ .Values.redis.sentinel.masterSet }}
            - name: OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS
              value: {{ include "oauth2-proxy.redis.sentinelUrl" . }}
            {{- if .Values.redis.auth.sentinel }}
            - name: OAUTH2_PROXY_REDIS_SENTINEL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "oauth2-proxy.redis.secretName" . }}
                  key: {{ include "oauth2-proxy.redis.secretPasswordKey" . }}
            {{- end }}
            {{- end }}
            {{- else }}
            - name: OAUTH2_PROXY_SESSION_STORE_TYPE
              value: "cookie"
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.containerPort }}
              name: http
              protocol: TCP
            {{- if .Values.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{ include "common.tplvalues.render" (dict "value" .Values.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /ping
              port: http
              scheme: HTTP
            initialDelaySeconds: {{ .Values.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /ping
              port: http
              scheme: HTTP
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.configuration.google.enabled }}
            - name: google-secret
              mountPath: /bitnami/oauth2-proxy/conf/google
              readOnly: true
          {{- end }}
          {{- if or .Values.configuration.existingConfigmap .Values.configuration.content }}
            - name: main-configuration
              mountPath: /bitnami/oauth2-proxy/conf
          {{- end }}
          {{- if .Values.configuration.authenticatedEmailsFile.enabled }}
            - name: authenticated-emails
              mountPath: /bitnami/oauth2-proxy/conf/authenticated-emails
              readOnly: true
          {{- end }}
          {{- if .Values.configuration.htpasswdFile.enabled }}
            - name: htpasswd-file
              mountPath: /bitnami/oauth2-proxy/conf/htpasswd
              readOnly: true
          {{- end }}
          {{- if .Values.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.configuration.google.enabled }}
        - name: google-secret
          secret:
            secretName: {{ include "oauth2-proxy.googleSecret" . }}
        {{- end }}
        {{- if .Values.configuration.htpasswdFile.enabled }}
        - name: htpasswd-file
          secret:
            secretName: {{ include "oauth2-proxy.httpasswdSecret" . }}
        {{- end }}
        {{- if .Values.configuration.authenticatedEmailsFile.enabled }}
        - name: authenticated-emails
          secret:
            items:
            - key: authenticated-emails-list
              path: authenticated-emails-list
            secretName: {{ include "oauth2-proxy.authenticatedEmailsSecret" . }}
        {{- end }}
        {{- if or .Values.configuration.existingConfigmap .Values.configuration.content }}
        - name: main-configuration
          configMap:
            defaultMode: 420
            name: {{ include "oauth2-proxy.configmapName" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.dnsPolicy }}
      dnsPolicy: {{ .Values.dnsPolicy | quote }}
      {{- end }}
      {{- if .Values.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.dnsConfig "context" $) | nindent 8 }}
      {{- end }}
