apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "carbon-intensity-exporter.fullname" . }}
  namespace: {{ .Values.namespace }}
  labels:
    {{- include "carbon-intensity-exporter.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "carbon-intensity-exporter.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "carbon-intensity-exporter.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ .Values.rbac.serviceAccountName }}
      containers:
        - name: {{ .Values.apiServer.name }}
          image: "{{ .Values.apiServer.image.repository }}:{{ .Values.apiServer.image.tag }}"
          imagePullPolicy: {{ .Values.apiServer.image.pullPolicy }}
          env:
          - name: DataSources__EmissionsDataSource
            value: {{ .Values.providerName }}
          - name: DataSources__ForecastDataSource
            value: {{ .Values.providerName }}
          {{- if eq .Values.providerName "ElectricityMaps" }}
          - name: DataSources__Configurations__ElectricityMaps__Type
            value: {{ .Values.providerName }}
          - name: DataSources__Configurations__ElectricityMaps__BaseURL
            value: {{ .Values.electricityMaps.baseURL | trimSuffix "/" }}/
          - name: DataSources__Configurations__ElectricityMaps__APITokenHeader
            value: {{ .Values.electricityMaps.apiTokenHeader }}
          - name: DataSources__Configurations__ElectricityMaps__APIToken
            valueFrom:
              secretKeyRef:
                name: {{ include "carbon-intensity-exporter.fullname" . }}
                key: apiToken
          {{- end }}
          {{- if eq .Values.providerName "WattTime" }}
          - name: DataSources__Configurations__WattTime__Type
            value: {{ .Values.providerName }}
          - name: DataSources__Configurations__WattTime__BaseURL
            value: {{ .Values.wattTime.baseURL | trimSuffix "/" }}/
          - name: DataSources__Configurations__WattTime__Proxy__useProxy
            value: "false"
          - name: DataSources__Configurations__WattTime__Username
            valueFrom:
              secretKeyRef:
                name: {{ include "carbon-intensity-exporter.fullname" . }}
                key: username
          - name: DataSources__Configurations__WattTime__Password
            valueFrom:
              secretKeyRef:
                name: {{ include "carbon-intensity-exporter.fullname" . }}
                key: password
          {{- end }}
          - name: LocationDataSourcesConfiguration__LocationSourceFiles__0__DataFileLocation
            value: {{ .Values.apiServer.dataFileLocation }}
          - name: Urls
            value: {{ .Values.apiServer.urls }}
          ports:
            - name: api-server-port
              containerPort: 7031
        - name: {{ .Values.carbonDataExporter.name }}
          image: "{{ .Values.carbonDataExporter.image.repository }}:{{ .Values.carbonDataExporter.image.tag }}"
          imagePullPolicy: {{ .Values.carbonDataExporter.image.pullPolicy }}
          ports:
            - containerPort: 9090
          args:
            - --configmap-name={{ .Values.carbonDataExporter.configmapName }}
            - --patrol-interval={{ .Values.carbonDataExporter.patrolInterval }}
            - --region={{ .Values.carbonDataExporter.region }}
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: NAMESPACE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace

      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
