apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "fullname" . }}-web
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: {{ .Values.web.replicacount }}
  template:
    metadata:
      {{- if .Values.web.podAnnotations }}
      annotations:
{{ toYaml .Values.web.podAnnotations | indent 8 }}
      {{- end }}
      labels:
        app: {{ template "fullname" . }}
        release: "{{ .Release.Name }}"
        role: web
    spec:
      {{- if .Values.web.affinity }}
      affinity:
{{ toYaml .Values.web.affinity | indent 8 }}
      {{- end }}
      {{- if .Values.web.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.web.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.web.tolerations }}
      tolerations:
{{ toYaml .Values.web.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.web.schedulerName }}
      schedulerName: "{{ .Values.web.schedulerName }}"
      {{- end }}
      {{- if .Values.image.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.imagePullSecrets | indent 8 }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}-web
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - containerPort: {{ .Values.service.internalPort }}
        env:
        - name: SENTRY_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: sentry-secret
        - name: SENTRY_DB_USER
          value: {{ default "sentry" .Values.postgresql.postgresUser | quote }}
        - name: SENTRY_DB_NAME
          value: {{ default "sentry" .Values.postgresql.postgresDatabase | quote }}
        - name: SENTRY_DB_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.postgresql.existingSecret }}
              name: {{ .Values.postgresql.existingSecret }}
            {{- else }}
              name: {{ template "postgresql.secret" . }}
            {{- end }}
              key: postgres-password
        - name: SENTRY_POSTGRES_HOST
          value: {{ template "postgresql.host" . }}
        - name: SENTRY_POSTGRES_PORT
          value: {{ template "postgresql.port" . }}
        {{- if or (.Values.redis.enabled) (.Values.redis.password) }}
        - name: SENTRY_REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.redis.existingSecret }}
              name: {{ .Values.redis.existingSecret }}
            {{- else }}
              name: {{ template "redis.secret" . }}
            {{- end }}
              key: redis-password
        {{- end }}
        - name: SENTRY_REDIS_HOST
          value: {{ template "redis.host" . }}
        - name: SENTRY_REDIS_PORT
          value: {{ template "redis.port" . }}
        - name: SENTRY_EMAIL_HOST
          value: {{ default "" .Values.email.host | quote }}
        - name: SENTRY_EMAIL_PORT
          value: {{ default "" .Values.email.port | quote }}
        - name: SENTRY_EMAIL_USER
          value: {{ default "" .Values.email.user | quote }}
        - name: SENTRY_EMAIL_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.email.existingSecret }}
              name: {{ .Values.email.existingSecret }}
            {{- else }}
              name: {{ template "fullname" . }}
            {{- end }}
              key: smtp-password
        - name: SENTRY_EMAIL_USE_TLS
          value: {{ .Values.email.use_tls | quote }}
        - name: SENTRY_SERVER_EMAIL
          value: {{ .Values.email.from_address | quote }}
{{- if .Values.web.env }}
{{ toYaml .Values.web.env | indent 8 }}
{{- end }}
        volumeMounts:
        - mountPath: /etc/sentry
          name: config
          readOnly: true
        - mountPath: {{ .Values.persistence.filestore_dir }}
          name: sentry-data
        livenessProbe:
          failureThreshold: 5
          httpGet:
            path: /_health/
            port: {{ .Values.service.internalPort }}
            scheme: HTTP
          initialDelaySeconds: 50
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
        readinessProbe:
          failureThreshold: 10
          httpGet:
            path: /_health/
            port: {{ .Values.service.internalPort }}
            scheme: HTTP
          initialDelaySeconds: 50
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
        resources:
{{ toYaml .Values.web.resources | indent 12 }}
      volumes:
      - name: config
        configMap:
          name: {{ template "fullname" . }}
      - name: sentry-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default (include "fullname" .) }}
      {{- else }}
        emptyDir: {}
      {{ end }}
