apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "distributor.fullname" . }}
  labels:
    app: {{ template "distribution.name" . }}
    chart: {{ template "distribution.chart" . }}
    component: {{ .Values.distributor.name }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  replicas: {{ .Values.distributor.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "distribution.name" . }}
      release: {{ .Release.Name }}
      component: {{ .Values.distributor.name }}
  template:
    metadata:
      labels:
        app: {{ template "distribution.name" . }}
        component: {{ .Values.distributor.name }}
        release: {{ .Release.Name }}
    spec:
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecrets }}
    {{- end }}
      initContainers:
      - name: "prepare-data"
        image: "{{ .Values.initContainerImage }}"
        imagePullPolicy: {{ .Values.distributor.image.pullPolicy }}
        command:
        - '/bin/sh'
        - '-c'
        - >
          until nc -z -w 2 {{ .Release.Name }}-redis {{ .Values.redis.master.port }} && echo {{ .Release.Name }}-redis ok; do sleep 2; done;
          {{- if .Values.distributor.token }}
          mkdir -pv {{ .Values.distributor.persistence.mountPath }}/etc/security;
          cp -fv /tmp/security/token {{ .Values.distributor.persistence.mountPath }}/etc/security/token;
          chmod 400 {{ .Values.distributor.persistence.mountPath }}/etc/security/token;
          {{- end }}
          chown -R 1020:1020 {{ .Values.distributor.persistence.mountPath }}
        volumeMounts:
        - name: distributor-data
          mountPath: {{ .Values.distributor.persistence.mountPath | quote }}
        {{- if .Values.distributor.token }}
        - name: distributor-token
          mountPath: "/tmp/security/token"
          subPath: token
        {{- end }}
      containers:
      - name: {{ .Values.distributor.name }}
        image: '{{ .Values.distributor.image.repository }}:{{ .Values.distributor.image.version }}'
        imagePullPolicy: {{ .Values.distributor.image.imagePullPolicy }}
        env:
        - name: DEFAULT_JAVA_OPTS
          value: '-Ddistribution.home={{ .Values.distributor.persistence.mountPath }} -Dfile.encoding=UTF8 -Dcom.sun.management.jmxremote.authenticate=false
                  -Dcom.sun.management.jmxremote.ssl=false -Duser.timezone=UTC
                  {{- if .Values.distributor.javaOpts.xms }}
                  -Xms{{ .Values.distributor.javaOpts.xms }}
                  {{- end}}
                  {{- if .Values.distributor.javaOpts.xmx }}
                  -Xmx{{ .Values.distributor.javaOpts.xmx }}
                  {{- end}}
                  -Dspring.profiles.active=production'
        - name: redis_connectionString
          valueFrom:
            secretKeyRef:
              name: {{ template "distribution.fullname" . }}-redis-connection
              key: redis_connectionString
        - name: BT_SERVER_URL
          value: 'http://{{ include "distribution.fullname" . }}:{{ .Values.distribution.internalPort }}'
        volumeMounts:
        - name: distributor-data
          mountPath: {{ .Values.distributor.persistence.mountPath | quote }}
        resources:
{{ toYaml .Values.distributor.resources | indent 10 }}
      volumes:
      {{- if .Values.distributor.token }}
      - name: distributor-token
        configMap:
          name: {{ template "distributor.fullname" . }}-token
      {{- end }}
      - name: distributor-data
      {{- if .Values.distributor.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.distributor.persistence.existingClaim }}{{ .Values.distributor.persistence.existingClaim }}{{- else }}{{ template "distributor.fullname" . }}{{- end }}
      {{- else }}
        emptyDir: {}
      {{- end -}}
