apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "entity-store.fullname" . }}
  labels:
    {{- include "entity-store.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "entity-store.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "entity-store.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      imagePullSecrets:
        {{ include "nemo-common.imagepullsecrets" . | nindent 8 | trim }}
      serviceAccountName: {{ include "entity-store.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: wait-for-postgres
          image: busybox
          command: ['sh', '-c', 'until nc -z {{ include "entity-store.database.host" . }} {{ include "entity-store.database.port" . }}; do echo "Waiting for Postgres to start"; sleep 5; done;']
        - name: entity-store-db-migration
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          workingDir: /app/services/entity-store
          env:
            {{- if and .Values.externalDatabase.uriSecret .Values.externalDatabase.uriSecret.name .Values.externalDatabase.uriSecret.key }}
            - name: POSTGRES_URI
              {{ include "nemo-common.postgresql.uri" . | nindent 14 | trim }}
            {{- else }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "entity-store.database.secretName" . }}
                  key: {{ include "entity-store.database.passwordKey" . }}
            - name: POSTGRES_USER
              value: {{ include "entity-store.database.user" . }}
            - name: POSTGRES_HOST
              value: {{ include "entity-store.database.host" . }}
            - name: POSTGRES_DB
              value: {{ include "entity-store.database.name" . }}
            {{- end }}
            {{ include "nemo-common.env" . | nindent 12 | trim }}
          command: ["/app/.venv/bin/python3"]
          args: ["-m", "scripts.run_db_migration"]
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: {{ include "nemo-common.mainimage" . | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: APP_VERSION
              value: {{ .Values.image.tag | quote }}
            {{- if .Values.openTelemetry.enabled }}
            # OpenTelemetry
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: OTEL_EXPORTER_OTLP_ENDPOINT
              value: "http://$(HOST_IP):4317" # sends to gRPC receiver on port 4317
            - name: OTEL_RESOURCE_ATTRIBUTES
              value: "deployment.environment=dev"
            - name: OTEL_SERVICE_NAME
              value: "entity-store-service"
            - name: OTEL_PYTHON_EXCLUDED_URLS
              value: "health"
            {{- end }}
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "entity-store.database.secretName" . }}
                  key: {{ include "entity-store.database.passwordKey" . }}
            - name: POSTGRES_USER
              value: {{ include "entity-store.database.user" . }}
            - name: POSTGRES_HOST
              value: {{ include "entity-store.database.host" . }}
            - name: POSTGRES_DB
              value: {{ include "entity-store.database.name" . }}
            {{ include "nemo-common.env" . | nindent 12 | trim }}
          envFrom:
          - configMapRef:
              name: {{ include "entity-store.fullname" . }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- with .Values.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.volumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- 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 }}
