apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: {{ template "oauth-proxy.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "oauth-proxy.fullname" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
    {{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        app: {{ template "oauth-proxy.name" . }}
        release: "{{ .Release.Name }}"
      {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
      {{- end }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        {{- range $key, $value := .Values.extraArgs }}
          {{- if $value }}
          - --{{ $key }}={{ $value }}
          {{- else }}
          - --{{ $key }}
          {{- end }}
        {{- end }}
        env:
        - name: OAUTH2_PROXY_CLIENT_ID
          valueFrom:
            secretKeyRef:
              name:  {{ template "oauth-proxy.fullname" . }}
              key: client-id
        - name: OAUTH2_PROXY_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              name:  {{ template "oauth-proxy.fullname" . }}
              key: client-secret
        - name: OAUTH2_PROXY_COOKIE_SECRET
          valueFrom:
            secretKeyRef:
              name:  {{ template "oauth-proxy.fullname" . }}
              key: cookie-secret
        ports:
          - containerPort: {{ .Values.service.internalPort }}
        livenessProbe:
          httpGet:
            path: /ping
            port: {{ .Values.service.internalPort }}
        readinessProbe:
          httpGet:
            path: /ping
            port: {{ .Values.service.internalPort }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
      {{- 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 }}
