# helm_charts_distribution-deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "distribution.fullname" . }}
  labels:
    app: {{ template "distribution.name" . }}
    chart: {{ template "distribution.chart" . }}
    component: {{ .Values.distribution.name }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  replicas: {{ .Values.distribution.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "distribution.name" . }}
      release: {{ .Release.Name }}
      component: {{ .Values.distribution.name }}
  template:
    metadata:
      labels:
        app: {{ template "distribution.name" . }}
        component: {{ .Values.distribution.name }}
        release: {{ .Release.Name }}
    spec:
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecrets }}
    {{- end }}
      initContainers:
      - name: "init-data"
        image: "{{ .Values.initContainerImage }}"
        command:
        - '/bin/sh'
        - '-c'
        - >
          until nc -z -w 2 {{ .Release.Name }}-mongodb 27017 && echo {{ .Release.Name }}-mongodb ok; do sleep 2; done;
          until nc -z -w 2 {{ .Release.Name }}-redis {{ .Values.redis.master.port }} && echo {{ .Release.Name }}-redis ok; do sleep 2; done;
      containers:
      - name: {{ .Values.distribution.name }}
        image: '{{ .Values.distribution.image.repository }}:{{ .Values.distribution.image.version }}'
        imagePullPolicy: {{ .Values.distribution.image.imagePullPolicy }}
        ports:
        - containerPort: {{ .Values.distribution.internalPort }}
          protocol: TCP
        env:
        - name: DEFAULT_JAVA_OPTS
          value: '-Ddistribution.home={{ .Values.distribution.persistence.mountPath }} -Dcom.sun.management.jmxremote.authenticate=false
                  -Dcom.sun.management.jmxremote.ssl=false -Duser.timezone=UTC
                  {{- if .Values.distribution.javaOpts.xms }}
                  -Xms{{ .Values.distribution.javaOpts.xms }}
                  {{- end}}
                  {{- if .Values.distribution.javaOpts.xmx }}
                  -Xmx{{ .Values.distribution.javaOpts.xmx }}
                  {{- end}}
                  -Dspring.profiles.active=production'
        - name: mongo_connectionString
          valueFrom:
            secretKeyRef:
              name: {{ template "distribution.fullname" . }}-mongo-connection
              key: mongo_connectionString
        - name: audit_mongo_connectionString
          valueFrom:
            secretKeyRef:
              name: {{ template "distribution.fullname" . }}-mongo-connection
              key: audit_mongo_connectionString
        - name: redis_connectionString
          valueFrom:
            secretKeyRef:
              name: {{ template "distribution.fullname" . }}-redis-connection
              key: redis_connectionString
        - name: BT_ARTIFACTORY_URL
          value: {{ .Values.distribution.env.artifactoryUrl | quote }}
        - name: BT_SERVER_URL
          value: {{ .Values.distribution.env.btServerUrl | quote }}
        {{- if .Values.distribution.env.artifactoryEdge1Url }}
        - name: artifactory_edge_1_url
          value: {{ .Values.distribution.env.artifactoryEdge1Url }}
        {{- end }}
        {{- if .Values.distribution.env.artifactoryEdge2Url }}
        - name: artifactory_edge_2_url
          value: {{ .Values.distribution.env.artifactoryEdge2Url }}
        {{- end }}
        {{- if .Values.distribution.env.artifactoryEdge3Url }}
        - name: artifactory_edge_3_url
          value: {{ .Values.distribution.env.artifactoryEdge3Url }}
        {{- end }}
        {{- if .Values.distribution.env.artifactoryCi1Url }}
        - name: artifactory_ci_1_url
          value: {{ .Values.distribution.env.artifactoryCi1Url }}
        {{- end }}
        volumeMounts:
        - name: distribution-data
          mountPath: {{ .Values.distribution.persistence.mountPath | quote }}
        resources:
{{ toYaml .Values.distribution.resources | indent 10 }}
        readinessProbe:
          httpGet:
            path: /api/v1/system/ping
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 10
          failureThreshold: 10
        livenessProbe:
          httpGet:
            path: /api/v1/system/ping
            port: 8080
          initialDelaySeconds: 180
          periodSeconds: 10
      volumes:
      - name: distribution-data
      {{- if .Values.distribution.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.distribution.persistence.existingClaim }}{{ .Values.distribution.persistence.existingClaim }}{{- else }}{{ template "distribution.fullname" . }}{{- end }}
      {{- else }}
        emptyDir: {}
      {{- end -}}