apiVersion: {{ template "deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "rocketchat.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "rocketchat.name" . }}
    helm.sh/chart: {{ include "rocketchat.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "rocketchat.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "rocketchat.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
        {{- end }}

      annotations:
        checksum/secret: {{ include (print .Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
        {{- end }}


    spec:
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      containers:
      - name: rocketchat
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
        - name: DEPLOY_PLATFORM
          value: helm-chart
        - name: INSTANCE_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MONGO_URL
          valueFrom:
            secretKeyRef:
              name: {{ template "rocketchat.fullname" . }}
              key: mongo-uri
        - name: MONGO_OPLOG_URL
          valueFrom:
            secretKeyRef:
              name: {{ template "rocketchat.fullname" . }}
              key: mongo-oplog-uri
        {{- if .Values.host }}
        - name: ROOT_URL
          value: https://{{ .Values.host }}
        {{- end }}
        {{- if .Values.smtp.enabled }}
        - name: MAIL_URL
          valueFrom:
            secretKeyRef:
              name: {{ template "rocketchat.fullname" . }}
              key: mail-url
        {{- end }}
{{- with .Values.extraEnv }}
{{ tpl . $ | indent 8 }}
{{- end }}
        ports:
        - name: http
          containerPort: 3000
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /api/info
            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.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /api/info
            port: http
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
        {{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: rocket-data
          mountPath: /app/uploads
      volumes:
      - name: rocket-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ template "rocketchat.fullname" . }}{{- end }}
      {{- else }}
        emptyDir: {}
      {{- end }}
{{- if or .Values.podAntiAffinity .Values.affinity }}
      affinity:
{{- if .Values.affinity }}
{{ toYaml .Values.affinity | indent 8 }}
{{- end }}
{{- if eq .Values.podAntiAffinity "hard" }}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: {{ .Values.podAntiAffinityTopologyKey }}
            labelSelector:
              matchLabels:
                app.kubernetes.io/name: {{ include "rocketchat.name" . }}
{{- else if eq .Values.podAntiAffinity "soft" }}
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              topologyKey: {{ .Values.podAntiAffinityTopologyKey }}
              labelSelector:
                matchLabels:
                  app.kubernetes.io/name: {{ include "rocketchat.name" . }}
                  app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{- end }}
