{{- if and .Values.patch.enabled (not (empty .Values.patch.image.tag)) -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "rule-engine.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "rule-engine.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "rule-engine.chart" . }}
spec:
  ttlSecondsAfterFinished: {{ .Values.patch.ttlSecondsAfterFinished }}
  backoffLimit: {{ .Values.patch.backoffLimit }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "rule-engine.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        helm.sh/chart: {{ include "rule-engine.chart" . }}
    spec:
      containers:
        - name: rule-engine-patch
          image: "{{ .Values.patch.image.repository }}:{{ .Values.patch.image.tag }}"
          imagePullPolicy: {{ .Values.patch.image.pullPolicy }}
          env:
            - name: modular_mongo_user
              valueFrom:
                secretKeyRef:
                  name: mongo-secret
                  key: username
            - name: modular_mongo_password
              valueFrom:
                secretKeyRef:
                  name: mongo-secret
                  key: password
            - name: modular_mongo_url
              value: "{{ .Values.mongoService }}:{{ .Values.mongoPort }}"
            - name: modular_mongo_db_name
              valueFrom:
                configMapKeyRef:
                  name: {{ include "rule-engine.fullname" . }}
                  key: modular-db-name
            - name: modular_service_mode
              value: {{ default "docker" .Values.modularSdk.serviceMode }}
            {{- if ne (default "docker" .Values.modularSdk.serviceMode) "docker" }}
            - name: modular_assume_role_arn
              value: {{ .Values.modularSdk.assumeRoleArn }}
            - name: MODULAR_AWS_REGION
              value: {{ .Values.modularSdk.awsRegion }}
            {{- end }}
            - name: CAAS_SERVICE_MODE
              value: docker
            - name: CAAS_MONGO_URI
              value: "mongodb://$(modular_mongo_user):$(modular_mongo_password)@$(modular_mongo_url)/"
            - name: CAAS_MONGO_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: {{ include "rule-engine.fullname" . }}
                  key: db-name
            - name: VAULT_URL
              value: {{ .Values.vaultService }}
            - name: VAULT_SERVICE_SERVICE_PORT
              value: "{{ .Values.vaultPort }}"
            - name: CAAS_VAULT_ENDPOINT
              value: "http://$(VAULT_URL):$(VAULT_SERVICE_SERVICE_PORT)"
            - name: CAAS_VAULT_TOKEN
              valueFrom:
                secretKeyRef:
                  name: vault-secret
                  key: token
            - name: VAULT_TOKEN
              valueFrom:
                secretKeyRef:
                  name: vault-secret
                  key: token
            - name: CAAS_MINIO_ENDPOINT
              value: "http://{{ .Values.minioService }}:{{ .Values.minioPort }}"
            - name: CAAS_MINIO_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: minio-secret
                  key: username
            - name: CAAS_MINIO_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: minio-secret
                  key: password
      restartPolicy: OnFailure
{{- end }}