apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "playmaker.fullname" . }}
  labels:
    {{- include "playmaker.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "playmaker.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "playmaker.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "playmaker.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: init-{{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ['sh', '-c', 'cp /tmp/credentials.conf /tmp/fdroid ; chmod 750 /tmp/fdroid ; chown -R pmuser:pmuser /tmp/fdroid ; exit 0']
          volumeMounts:
            - name: playmaker-data
              mountPath: /tmp/fdroid
            - name: playmaker-creds
              mountPath: /tmp/credentials.conf
              subPath: credentials.conf
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
          {{- toYaml .Values.env | nindent 12 }}
          ports:
            - name: http
              containerPort: 5000
              protocol: TCP
          volumeMounts:
            - name: playmaker-data
              mountPath: /data/fdroid
          livenessProbe:
            httpGet:
              path: /
              port: 5000
          readinessProbe:
            httpGet:
              path: /
              port: 5000
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
      - name: playmaker-data
        {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default (include "playmaker.fullname" .) }}
        {{- else }}
        emptyDir: {}
        {{- end }}
      - name: playmaker-creds
        {{- if .Values.credentials }}
        secret:
          secretName: {{ include "playmaker.fullname" . }}-credentials
        {{- else }}
        emptyDir: {}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}