#
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
#

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "jifa.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels:
    {{- include "jifa.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "jifa.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "jifa.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "jifa.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      volumes:
        - name: {{ include "jifa.fullname" . }}
          persistentVolumeClaim:
            claimName: {{ include "jifa.fullname" . }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default "latest" }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: jifa-port
              containerPort: 8102
          env:
            - name: MYSQL_HOST
              value: {{ include "jifa.mysqlHost" . }}
            - name: MYSQL_DATABASE
              value: {{ .Values.mysql.database }}
            - name: MYSQL_USER
              value: {{ .Values.mysql.user }}
            - name: MYSQL_PASSWORD
              value: {{ .Values.mysql.password }}
          args:
            - --jifa.role=master
            - --jifa.storage-pvc-name={{- include "jifa.fullname" . }}
            - --jifa.storage-path=/jifa-storage
            - --jifa.service-account-name={{- include "jifa.serviceAccountName" . }}
            - --jifa.elastic-worker-image={{- .Values.image.worker.repository }}:{{ .Values.image.worker.tag | default "latest" }}
            - --jifa.port=8102
           # - --jifa.cluster-namespace={{- .Release.Namespace | quote }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: {{ include "jifa.fullname" . }}
              mountPath: "/jifa-storage"
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
