{{- if .Values.enabled }}
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "speech.fullname" . }}
  labels:
    app: {{ template "speech.name" . }}
    chart: {{ template "speech.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: {{ template "speech.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "speech.name" . }}
        release: {{ .Release.Name }}
    spec:
      securityContext:
        fsGroup: 1000
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      initContainers:
        - name: copy-lbrycrd-config
          image: busybox
          command: ['sh', '-c', 'cp /configmap/chainqueryConfig.json /configmap/lbryConfig.json /configmap/loggerConfig.json /configmap/mysqlConfig.json /configmap/siteConfig.json /configmap/slackConfig.json /configmap/daemon_settings.yml /config && cp /configmap/authConfig.json /private']
          volumeMounts:
            - name: configmap
              mountPath: /configmap
            - name: config
              mountPath: /config
            - name: private
              mountPath: /private
      containers:
        - name: {{ template "speech.fullname" . }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          resources: {{ toYaml .Values.resources | indent 10 }}
          ports:
            - name: port
              containerPort: {{ .Values.service.port }}
          volumeMounts:
            - name: configmap
              mountPath: /spee.ch/site/config
            - name: private
              mountPath: /spee.ch/site/private
            - name: speech-data
              mountPath: /data
        {{- if .Values.lbrynet.enabled }}
        - name: {{ template "speech.fullname" . }}-lbrynet
          image: "{{ .Values.lbrynet.image.repository }}:{{ .Values.lbrynet.image.tag }}"
          imagePullPolicy: {{ .Values.lbrynet.image.pullPolicy }}
          resources: {{ toYaml .Values.resources | indent 10 }}
          ports:
            - name: rpc
              containerPort: {{ .Values.lbrynet.service.rpcPort }}
          volumeMounts:
            - name: configmap
              mountPath: /etc/lbry
            - name: lbrynet-home
              mountPath: /home/lbrynet
            - name: speech-data
              mountPath: /data
        {{- end }}
      volumes:
        - name: config
        - name: private
        - name: configmap
          configMap:
            name: {{ template "speech.fullname" . }}
        - name: speech-data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "speech.fullname" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        - name: lbrynet-home
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            {{- if .Values.lbrynet.persistence.existingClaim }}
            claimName: {{ .Values.lbrynet.persistence.existingClaim }} 
            {{- else }}
            claimName: {{ template "speech.fullname" .}}-lbrynet
            {{- end }}
        {{- else }}
          emptyDir: {}
        {{- end }}
{{- end }}