{{ $fullname := include "dex.fullname" . }}
{{ $httpsTlsBuiltName := printf "%s-tls" $fullname }}
{{ $httpsTlsSecretName := default $httpsTlsBuiltName .Values.certs.web.secret.tlsName }}
{{ $grpcTlsServerBuiltName := printf "%s-server-tls" $fullname }}
{{ $grpcTlsServerSecretName := default $grpcTlsServerBuiltName .Values.certs.grpc.secret.serverTlsName }}
{{ $grpcCaBuiltName := printf "%s-ca" $fullname }}
{{ $grpcCaSecretName := default $grpcCaBuiltName .Values.certs.grpc.secret.caName }}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "dex.fullname" . }}
  labels:
{{ include "dex.labels" . | indent 4 }}
    app.kubernetes.io/component: dex
spec:
  replicas: {{ .Values.replicas }}
  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "dex.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/component: dex
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "dex.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/component: dex
        {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
        {{- end }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
        {{- end }}
    spec:
{{- if .Values.priorityClassName }}
      priorityClassName: "{{ .Values.priorityClassName }}"
{{- end }}
{{- if .Values.initContainers }}
      initContainers:
        {{- toYaml .Values.initContainers | nindent 8 }}
{{- end }}
      serviceAccountName: {{ template "dex.serviceAccountName" . }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 10 }}
{{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
      {{- if .Values.securityContext }}
      securityContext: {{ toYaml .Values.securityContext | nindent 8 }}
      {{- end }}
      containers:
      - name: main
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        command:
        - /usr/local/bin/dex
        - serve
        - /etc/dex/cfg/config.yaml
        resources:
{{ toYaml .Values.resources | indent 10 }}
        ports:
        - name: {{ if .Values.https }}https{{ else }}http{{ end }}
          containerPort: {{ .Values.ports.web.containerPort }}
          protocol: TCP
        {{- if .Values.grpc }}
        - name: grpc
          containerPort: {{ .Values.ports.grpc.containerPort }}
          protocol: TCP
        {{- end }}
        {{- if .Values.telemetry }}
        - name: telemetry
          containerPort: {{ .Values.ports.telemetry.containerPort }}
          protocol: TCP
        {{- end  }}
{{- if and (not .Values.https) .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: {{ .Values.livenessProbe.httpPath }}
            port: {{ if .Values.https }}https{{ else }}http{{ end }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
{{- end }}
{{- if and (not .Values.https) .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: {{ .Values.readinessProbe.httpPath }}
            port: {{ if .Values.https }}https{{ else }}http{{ end }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
{{- end }}
        env:
{{ toYaml .Values.env | indent 10 }}
        volumeMounts:
        - mountPath: /etc/dex/cfg
          name: config
{{- if .Values.https }}
        - mountPath: /etc/dex/tls/https/server
          name: https-tls
{{- end }}
{{- if .Values.grpc }}
        - mountPath: /etc/dex/tls/grpc/server
          name: grpc-tls-server
        - mountPath: /etc/dex/tls/grpc/ca
          name: grpc-tls-ca
{{- end }}
{{- if ne (len .Values.extraVolumeMounts) 0 }}
{{ toYaml .Values.extraVolumeMounts | indent 8 }}
{{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
      volumes:
      - secret:
          defaultMode: 420
          items:
          - key: config.yaml
            path: config.yaml
          secretName: {{ template "dex.fullname" . }}
        name: config
{{- if .Values.https }}
      - name: https-tls
        secret:
          defaultMode: 420
          secretName: {{ $httpsTlsSecretName | quote }}
{{- end }}
{{- if .Values.grpc }}
      - name: grpc-tls-server
        secret:
          defaultMode: 420
          secretName: {{ $grpcTlsServerSecretName | quote }}
      - name: grpc-tls-ca
        secret:
          defaultMode: 420
          secretName: {{ $grpcCaSecretName| quote }}
{{- end }}
{{- if ne (len .Values.extraVolumes) 0 }}
{{ toYaml .Values.extraVolumes | indent 6 }}
{{- end }}
