apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: "{{ .Release.Name }}-adapter"
  namespace: "{{ .Release.Namespace }}"
  labels: &DicomDeploymentLabels
    app.kubernetes.io/name:  "{{ .Release.Name }}"
    app.kubernetes.io/component: dicom-adapter-app
spec:
  replicas: 1
  serviceName: {{ .Release.Name }}-dicom-adapter-lb
  selector:
    matchLabels: *DicomDeploymentLabels
  template:
    metadata:
      labels: *DicomDeploymentLabels
    spec:
      containers:
        {{- if or (eq .Values.import.deployment_type "import adapter") (eq .Values.import.deployment_type "both")}}
        - name: dicom-import-adapter
          image: "{{ .Values.import.image.repo }}:{{ .Values.import.image.tag }}"
          ports:
            - containerPort: 2575
              protocol: TCP
              name: "port"
          args: 
            - "--dimse_aet={{ .Values.import.dimse_aet }}"
            - "--dimse_port=2575"
            - "--dicomweb_address={{ .Values.import.dicomweb_address }}"
            {{- if .Values.import.redact_remove_list }}
            - "--redact_remove_list={{ .Values.import.redact_remove_list }}"
            {{- end }}
            {{- if .Values.import.redact_keep_list }}
            - "--redact_keep_list={{ .Values.import.redact_keep_list }}"
            {{- end }}
            {{- if .Values.import.dimse_cmove_aet }}
            - "--dimse_cmove_aet={{ .Values.import.dimse_cmove_aet }}"
            {{- end }}
            {{- if .Values.import.aet_dictionary_inline }}
            - "--aet_dictionary_inline={{ .Values.import.aet_dictionary_inline }}"
            {{- end }}
            {{- if .Values.import.destination_config_inline }}
            - "--destination_config_inline={{ .Values.import.destination_config_inline }}"
            {{- end }}
            {{- if .Values.import.store_compress_to_transfer_syntax }}
            - "--store_compress_to_transfer_syntax={{ .Values.import.store_compress_to_transfer_syntax }}"
            {{- end }}
            {{- if .Values.import.custom_flags }}
            - "{{ .Values.import.custom_flags }}"
            {{- end }}
            {{- if .Values.import.monitoring_project_id }}
            - "--monitoring_project_id={{ .Values.import.monitoring_project_id }}"
          env:
            - name: ENV_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: ENV_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: ENV_CONTAINER_NAME
              value: dicom-import-adapter
            {{- end }}
        {{- end }}
        
        {{- if or (eq .Values.import.deployment_type "export adapter") (eq .Values.import.deployment_type "both")}}
        - name: dicom-export-adapter
          image: "{{ .Values.export.image.repo }}:{{ .Values.export.image.tag }}"
          args:
            {{- if .Values.export.peer_dimse_aet }}
            - "--peer_dimse_aet={{ .Values.export.peer_dimse_aet }}"
            {{- end }}
            {{- if .Values.export.peer_dimse_ip }}
            - "--peer_dimse_ip={{ .Values.export.peer_dimse_ip }}"
            {{- end }}
            {{- if .Values.export.peer_dimse_port }}
            - "--peer_dimse_port={{ .Values.export.peer_dimse_port }}"
            {{- end }}
            {{- if .Values.export.peer_dicomweb_address }}
            - "--peer_dicomweb_address={{ .Values.export.peer_dicomweb_address }}"
            {{- end }}
            {{- if .Values.export.project_id }}
            - "--project_id={{ .Values.export.project_id }}"
            {{- end }}
            {{- if .Values.export.subscription_id }}
            - "--subscription_id={{ .Values.export.subscription_id }}"
            {{- end }}
            {{- if .Values.export.dicomweb_addr }}
            - "--dicomweb_addr={{ .Values.export.dicomweb_addr }}"
            {{- end }}
            {{- if .Values.export.oauth_scopes }}
            - "--oauth_scopes={{ .Values.export.oauth_scopes }}"
            {{- end }}
            {{- if .Values.export.use_gcp_application_default_credentials }}
            - "--use_gcp_application_default_credentials"
            {{- end }}
            {{- if .Values.export.custom_flags }}
            - "{{ .Values.export.custom_flags }}"
            {{- end }}
            {{- if .Values.export.monitoring_project_id }}
            - "--monitoring_project_id={{ .Values.export.monitoring_project_id }}"
            env:
            - name: ENV_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: ENV_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: ENV_CONTAINER_NAME
              value: dicom-export-adapter
            {{- end }}
        {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-dicom-adapter-lb
  {{- if .Values.lb.internal }}
  annotations:
    cloud.google.com/load-balancer-type: "Internal"
  {{- end }}
  labels:
    app.kubernetes.io/name:  "{{ .Release.Name }}"
    app.kubernetes.io/component: dicom-adapter-app
spec:
  ports:
  - port: 2575
    targetPort: 2575
    protocol: TCP
    name: port
  selector:
    app.kubernetes.io/name:  "{{ .Release.Name }}"
    app.kubernetes.io/component: dicom-adapter-app
  type: LoadBalancer
