apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "osticket.fullname" . }}
  labels:
{{ include "osticket.labels" . | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "osticket.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "osticket.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      initContainers:
        - name: {{ .Chart.Name }}-install
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: INSTALL_SECRET
              value: "{{ .Values.installSecret }}"
            - name: MYSQL_HOST
              value: "{{ include "osticket.name" . }}-mariadb"
            - name: MYSQL_PASSWORD
              value: "{{ .Values.mariadb.auth.password }}"
            - name: USE_MEMCACHE
              value: "true"
            - name: MEMCACHE_NAME
              value: "{{ include "osticket.name" . }}-memcached"
          command:
            - "php"
            - "/data/bin/install.php"
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          env:
            - name: INSTALL_SECRET
              value: "{{ .Values.installSecret }}"
            - name: MYSQL_HOST
              value: "{{ include "osticket.name" . }}-mariadb"
            - name: MYSQL_PASSWORD
              value: "{{ .Values.mariadb.auth.password }}"
            - name: USE_MEMCACHE
              value: "true"
            - name: MEMCACHE_NAME
              value: "{{ include "osticket.name" . }}-memcached"
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 1
            httpGet:
              path: /
              port: http
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 2
            timeoutSeconds: 1
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}