apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "oauth2-proxy.name" . }}
    chart: {{ template "oauth2-proxy.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "oauth2-proxy.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "oauth2-proxy.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/config-emails: {{ include (print $.Template.BasePath "/configmap-authenticated-emails-file.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        checksum/google-secret: {{ include (print $.Template.BasePath "/google-secret.yaml") . | sha256sum }}
{{- if .Values.htpasswdFile.enabled }}
        checksum/htpasswd: {{ include (print $.Template.BasePath "/configmap-htpasswd-file.yaml") . | sha256sum }}
{{- end }}
    {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "oauth2-proxy.name" . }}
        release: "{{ .Release.Name }}"
      {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
      {{- end }}
    spec:
    {{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
    {{- end }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "oauth2-proxy.serviceAccountName" . }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
          - --http-address=0.0.0.0:4180
        {{- range $key, $value := .Values.extraArgs }}
          {{- if $value }}
          - --{{ $key }}={{ $value }}
          {{- else }}
          - --{{ $key }}
          {{- end }}
        {{- end }}
        {{- if or .Values.config.existingConfig .Values.config.configFile }}
          - --config=/etc/oauth2_proxy/oauth2_proxy.cfg
        {{- end }}
        {{- if .Values.authenticatedEmailsFile.enabled }}
        {{- if .Values.authenticatedEmailsFile.template }}
          - --authenticated-emails-file=/etc/oauth2-proxy/{{ .Values.authenticatedEmailsFile.template }}
        {{- else }}
          - --authenticated-emails-file=/etc/oauth2-proxy/authenticated-emails-list
        {{- end }}
        {{- end }}
        {{- with .Values.config.google }}
        {{- if and .adminEmail (or .serviceAccountJson .existingSecret) }}
          - --google-admin-email={{ .adminEmail }}
          - --google-service-account-json=/google/service-account.json
        {{- end }}
        {{- end }}
        {{- if .Values.htpasswdFile.enabled }}
          - --htpasswd-file=/etc/oauth2_proxy/htpasswd/users.txt
        {{- end }}
        env:
        {{- if .Values.proxyVarsAsSecrets }}
        - 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
        {{- end }}
        {{- if .Values.extraEnv }}
{{ toYaml .Values.extraEnv | indent 8 }}
        {{- end }}
        ports:
          - containerPort: 4180
            name: {{ .Values.httpScheme }}
            protocol: TCP
{{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /ping
            port: {{ .Values.httpScheme }}
            scheme: {{ .Values.httpScheme | upper }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
{{- end }}
{{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /ping
            port: {{ .Values.httpScheme }}
            scheme: {{ .Values.httpScheme | upper }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
{{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
{{- with .Values.config.google }}
{{- if and .adminEmail (or .serviceAccountJson .existingSecret) }}
        - name: google-secret
          mountPath: /google
          readOnly: true
{{- end }}
{{- end }}
{{- if or .Values.config.existingConfig .Values.config.configFile }}
        - mountPath: /etc/oauth2_proxy
          name: configmain
{{- end }}
{{- if .Values.authenticatedEmailsFile.enabled }}
        - mountPath: /etc/oauth2-proxy
          name: configaccesslist
          readOnly: true
{{- end }}
{{- if .Values.htpasswdFile.enabled }}
        - mountPath: /etc/oauth2_proxy/htpasswd
          name: {{ template "oauth2-proxy.fullname" . }}-htpasswd-file
          readOnly: true
{{- end }}
{{- if ne (len .Values.extraVolumeMounts) 0 }}
{{ toYaml .Values.extraVolumeMounts | indent 8 }}
{{- end }}
{{- if .Values.securityContext.enabled }}
        securityContext:
          runAsNonRoot: {{ .Values.securityContext.runAsNonRoot }}
{{- end}}
      volumes:
{{- with .Values.config.google }}
{{- if and .adminEmail (or .serviceAccountJson .existingSecret) }}
      - name: google-secret
        secret:
          secretName: {{ if .existingSecret }}{{ .existingSecret }}{{ else }} {{ template "oauth2-proxy.secretName" $ }}{{ end }}
{{- end }}
{{- end }}

{{- if .Values.htpasswdFile.enabled }}
      - name: {{ template "oauth2-proxy.fullname" . }}-htpasswd-file
        secret:
          secretName: {{ if .Values.htpasswdFile.existingSecret }}{{ .Values.htpasswdFile.existingSecret }}{{ else }} {{ template "oauth2-proxy.fullname" . }}-htpasswd-file {{ end }}
{{- end }}

{{- if or .Values.config.existingConfig .Values.config.configFile }}
      - configMap:
          defaultMode: 420
          name: {{ if .Values.config.existingConfig }}{{ .Values.config.existingConfig }}{{ else }}{{ template "oauth2-proxy.fullname" . }}{{ end }}
        name: configmain
{{- end }}
{{- if ne (len .Values.extraVolumes) 0 }}
{{ toYaml .Values.extraVolumes | indent 6 }}
{{- end }}
{{- if .Values.authenticatedEmailsFile.enabled }}
      - configMap:
{{- if .Values.authenticatedEmailsFile.template }}
          name: {{ .Values.authenticatedEmailsFile.template }}
{{- else }}
          name: {{ template "oauth2-proxy.fullname" . }}-accesslist
{{- end }}
          items:
          - key: restricted_user_access
{{- if .Values.authenticatedEmailsFile.template }}
            path: {{ .Values.authenticatedEmailsFile.template }}
{{- else }}
            path: authenticated-emails-list
{{- end }}
        name: configaccesslist
{{- end }}

      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
