---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-{{ .Values.searchServiceName }}
spec:
  selector:
    matchLabels:
      run: {{ .Chart.Name }}-{{ .Values.searchServiceName }}
  replicas: {{ default 1 .Values.search.replicas }}
  template:
    metadata:
      labels:
        run: {{ .Chart.Name }}-{{ .Values.searchServiceName }}
    spec:
      {{- with default .Values.nodeSelector .Values.search.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with default .Values.affinity .Values.search.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with default .Values.tolerations .Values.search.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}-{{ .Values.searchServiceName }}
        image: {{- if .Values.searchServiceImage }} {{.Values.searchServiceImage}}{{- else }} {{ .Values.dockerhubImagePath }}/{{ .Chart.Name }}-{{ .Values.searchServiceName }}:{{ .Values.searchImageVersion }}{{- end }}
        ports:
        - containerPort: 5000  
        env:
        - name: PROXY_ENDPOINT
          value: elasticsearch  
        {{- with .Values.search.resources }}
        resources:
{{ toYaml . | indent 10 }}
        {{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-{{ .Values.metadataServiceName }}
spec:
  selector:
    matchLabels:
      run: {{ .Chart.Name }}-{{ .Values.metadataServiceName }}
  replicas: {{ default 1 .Values.metadataReplicas }}
  template:
    metadata:
      labels:
        run: {{ .Chart.Name }}-{{ .Values.metadataServiceName }}
    spec:
      {{- with default .Values.nodeSelector .Values.metadata.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with default .Values.affinity .Values.metadata.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with default .Values.tolerations .Values.metadata.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      volumes:
      {{- if .Values.oidcEnabled }}
        - name: oidc-config
          secret:
            secretName: oidc-config
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}-{{ .Values.metadataServiceName }}
        {{- with .Values.metadataServiceImage }}
        image: {{ . }}
        {{- else }}
        image: {{ .Values.dockerhubImagePath }}/{{ .Chart.Name }}-{{ .Values.metadataServiceName }}{{ if .Values.oidcEnabled }}-oidc{{ end }}:{{ .Values.metadataImageVersion }}
        {{- end }}
        imagePullPolicy: Never
        ports:
        - containerPort: 5000
        env:
        - name: PROXY_HOST
          value: bolt://neo4j
        {{- if .Values.oidcEnabled }}
        - name: FLASK_OIDC_CLIENT_SECRETS
          value: /etc/client_secrets.json
        - name: FLASK_OIDC_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: oidc-config
              key: OIDC_CLIENT_SECRET
        {{- end }}
        volumeMounts:
        {{- if .Values.oidcEnabled }}
          - name: oidc-config
            mountPath: /etc/client_secrets.json
            subPath: client_secrets.json
        {{- end }}
        {{- with .Values.metadata.resources }}
        resources:
{{ toYaml . | indent 10 }}
        {{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}-{{ .Values.frontEndServiceName }}
spec:
  selector:
    matchLabels:
      run: {{ .Chart.Name }}-{{ .Values.frontEndServiceName }}
  replicas: {{ default 1 .Values.frontEnd.replicas }}
  template:
    metadata:
      labels:
        run: {{ .Chart.Name }}-{{ .Values.frontEndServiceName }}
    spec:
      {{- with default .Values.nodeSelector .Values.frontEnd.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with default .Values.affinity .Values.frontEnd.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with default .Values.tolerations .Values.frontEnd.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      volumes:
      {{- if .Values.oidcEnabled }}
        - name: oidc-config
          secret:
            secretName: oidc-config
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}-{{ .Values.frontEndServiceName }}
        {{- with .Values.frontEndServiceImage }}
        image: {{ . }}
        {{- else }}
        image: {{ .Values.dockerhubImagePath }}/{{ .Chart.Name }}-{{ .Values.frontEndServiceName }}{{ if .Values.oidcEnabled }}-oidc{{ end }}:{{ .Values.frontEndImageVersion }}
        {{- end }}
        ports:
          - containerPort: 5000
        env:
          - name: SEARCHSERVICE_BASE
            value: http://{{ .Chart.Name }}-{{ .Values.searchServiceName }}:5001
          - name: METADATASERVICE_BASE
            value: http://{{ .Chart.Name }}-{{ .Values.metadataServiceName }}:5002
          - name: LONG_RANDOM_STRING
            value: {{ quote .Values.LONG_RANDOM_STRING }}
        {{- if .Values.oidcEnabled }}
          - name: FLASK_OIDC_CLIENT_SECRETS
            value: /etc/client_secrets.json
          - name: FLASK_OIDC_SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: oidc-config
                key: OIDC_CLIENT_SECRET
        {{- end }}
        volumeMounts:
        {{- if .Values.oidcEnabled }}
          - name: oidc-config
            mountPath: /etc/client_secrets.json
            subPath: client_secrets.json
        {{- end }}
