apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "xray-persist.fullname" . }}
  labels:
    app: {{ template "xray.name" . }}
    chart: {{ template "xray.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    component: {{ .Values.persist.name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "xray.name" . }}
      release: {{ .Release.Name }}
      component: {{ .Values.persist.name }}
  template:
    metadata:
      labels:
        app: {{ template "xray.name" . }}
        release: {{ .Release.Name }}
        component: {{ .Values.persist.name }}
    spec:
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecrets }}
    {{- end }}
      serviceAccountName: {{ template "xray.serviceAccountName" . }}
      securityContext:
        runAsUser: {{ .Values.common.xrayUserId }}
        fsGroup: {{ .Values.common.xrayGroupId }}
      initContainers:
      - name: init-wait
        image: {{ .Values.initContainerImage | quote }}
        env:
        {{- if .Values.mongodb.enabled }}
        - name: MONGODB_USER
          value: {{ .Values.mongodb.mongodbUsername }}
        - name: MONGODB_DATABASE
          value: {{ .Values.mongodb.mongodbDatabase }}
        - name: MONGODB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Release.Name }}-mongodb
              key: mongodb-password
        {{- else }}
        - name: MONGODB_URL
          value: {{ .Values.global.mongoUrl }}
        {{- end }}
        {{- if .Values.postgresql.enabled }}
        - name: POSTGRES_USER
          value: {{ .Values.postgresql.postgresUser }}
        - name: POSTGRESS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Release.Name }}-postgresql
              key: postgres-password
        - name: POSTGRESS_DB
          value: {{ .Values.postgresql.postgresDatabase }}
        {{- else }}
        - name: POSTGRESS_URL
          value: {{ .Values.global.postgresqlUrl }}
        {{- end }}
        - name: RABBITMQ_USER
          value: {{ index .Values "rabbitmq-ha" "rabbitmqUsername" }}
        - name: RABBITMQ_ERLANG_COOKIE
          valueFrom:
            secretKeyRef:
              name: {{ .Release.Name }}-rabbitmq-ha
              key: rabbitmq-erlang-cookie
        - name: RABBITMQ_DEFAULT_PASS
          valueFrom:
            secretKeyRef:
              name: {{ .Release.Name }}-rabbitmq-ha
              key: rabbitmq-password
        command:
        - '/bin/sh'
        - '-c'
        - >
          cp -fv /scripts/setup.sh {{ .Values.common.xrayConfigPath }};
          chmod +x {{ .Values.common.xrayConfigPath }}/setup.sh;
          {{ .Values.common.xrayConfigPath }}/setup.sh;
        volumeMounts:
        - name: data-volume
          mountPath: "{{ .Values.common.xrayConfigPath }}"
        - name: setup
          mountPath: "/scripts"
      containers:
      - name: {{ .Values.persist.name }}
        image: {{ .Values.persist.image }}:{{ default .Chart.AppVersion .Values.common.xrayVersion }}
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        env:
        - name: XRAYCONFIGPATH
          value: "{{ .Values.common.xrayConfigPath }}"
        - name: XRAY_MASTER_KEY
          valueFrom:
            secretKeyRef:
              name: {{ template "xray.fullname" . }}-master-key
              key: master-key
        - name: XRAY_HA_NODE_ID
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        ports:
        - containerPort: {{ .Values.persist.internalPort }}
        volumeMounts:
        - name: data-volume
          mountPath: "{{ .Values.common.xrayConfigPath }}"
        securityContext:
          allowPrivilegeEscalation: false
        resources:
{{ toYaml .Values.persist.resources | indent 10 }}
        readinessProbe:
          httpGet:
            path: /debug/pprof/
            port: {{ .Values.persist.internalPort }}
          initialDelaySeconds: 60
          periodSeconds: 10
          failureThreshold: 10
        livenessProbe:
          httpGet:
            path: /debug/pprof/
            port: {{ .Values.persist.internalPort }}
          initialDelaySeconds: 90
          periodSeconds: 10
      volumes:
      - name: data-volume
        emptyDir:
          sizeLimit: {{ .Values.persist.storage.sizeLimit }}
      - name: config-volume
        emptyDir:
          sizeLimit: 1Gi
      - name: setup
        configMap:
          name: {{ template "xray.fullname" . }}-setup
