{{- if .Values.controller.create }}
# Controller Service
kind: Deployment
apiVersion: apps/v1
metadata:
  name: {{ .Values.controller.name }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "aws-efs-csi-driver.name" . }}
    {{- with .Values.controller.additionalLabels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.controller.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Values.controller.name }}
      app.kubernetes.io/name: {{ include "aws-efs-csi-driver.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  {{- with .Values.controller.updateStrategy }}
  strategy:
    {{ toYaml . | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels:
        app: {{ .Values.controller.name }}
        app.kubernetes.io/name: {{ include "aws-efs-csi-driver.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- with .Values.controller.podLabels }}
        {{ toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.controller.podAnnotations }}
      annotations: {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- if hasKey .Values.controller "hostNetwork" }}
      hostNetwork: {{ .Values.controller.hostNetwork }}
      {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      nodeSelector:
        kubernetes.io/os: linux
        {{- with .Values.controller.nodeSelector }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      serviceAccountName: {{ .Values.controller.serviceAccount.name }}
      priorityClassName: {{ .Values.controller.priorityClassName | default "system-cluster-critical" }}
      {{- with .Values.controller.tolerations }}
      tolerations: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.controller.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.dnsPolicy }}
      dnsPolicy: {{ .Values.controller.dnsPolicy }}
      {{- end }}
      {{- with .Values.controller.dnsConfig }}
      dnsConfig: {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: efs-plugin
          {{- with .Values.controller.containerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: {{ printf "%s:%s" .Values.image.repository (default (printf "v%s" .Chart.AppVersion) (toString .Values.image.tag)) }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - --endpoint=$(CSI_ENDPOINT)
            - --logtostderr
            {{- if .Values.controller.tags }}
            - --tags={{ include "aws-efs-csi-driver.tags" .Values.controller.tags }}
            {{- end }}
            - --v={{ .Values.controller.logLevel }}
            - --delete-access-point-root-dir={{ hasKey .Values.controller "deleteAccessPointRootDir" | ternary .Values.controller.deleteAccessPointRootDir false }}
          env:
            - name: CSI_ENDPOINT
              value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
            {{- if .Values.controller.regionalStsEndpoints }}
            - name: AWS_STS_REGIONAL_ENDPOINTS
              value: regional
            {{- end }}
            - name: CSI_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            {{- if .Values.useFIPS }}
            - name: AWS_USE_FIPS_ENDPOINT
              value: "true"
            {{- end }}
            - name: PORT_RANGE_UPPER_BOUND
              value: "{{ .Values.portRangeUpperBound }}"
            {{- with .Values.controller.env }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
            {{- with .Values.controller.volumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: healthz
              containerPort: {{ .Values.controller.healthPort }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: healthz
            initialDelaySeconds: 10
            timeoutSeconds: 3
            periodSeconds: 10
            failureThreshold: 5
          {{- with .Values.controller.resources }}
          resources: {{ toYaml . | nindent 12 }}
          {{- end }}
        - name: csi-provisioner
          image: {{ printf "%s:%s" .Values.sidecars.csiProvisioner.image.repository .Values.sidecars.csiProvisioner.image.tag }}
          imagePullPolicy: {{ .Values.sidecars.csiProvisioner.image.pullPolicy }}
          args:
            - --csi-address=$(ADDRESS)
            - --v={{ .Values.controller.logLevel }}
            - --feature-gates=Topology=true
            {{- if .Values.controller.extraCreateMetadata }}
            - --extra-create-metadata
            {{- end }}
            - --leader-election
            {{- if hasKey .Values.controller "leaderElectionRenewDeadline" }}
            - --leader-election-renew-deadline={{ .Values.controller.leaderElectionRenewDeadline }}
            {{- end }}
            {{- if hasKey .Values.controller "leaderElectionLeaseDuration" }}
            - --leader-election-lease-duration={{ .Values.controller.leaderElectionLeaseDuration }}
            {{- end }}
            {{- range .Values.sidecars.csiProvisioner.additionalArgs }}
            - {{ . }}
            {{- end }}
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
          {{- with default .Values.controller.resources .Values.sidecars.csiProvisioner.resources }}
          resources: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.sidecars.csiProvisioner.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        - name: liveness-probe
          image: {{ printf "%s:%s" .Values.sidecars.livenessProbe.image.repository .Values.sidecars.livenessProbe.image.tag }}
          imagePullPolicy: {{ .Values.sidecars.livenessProbe.image.pullPolicy }}
          args:
            - --csi-address=/csi/csi.sock
            - --health-port={{ .Values.controller.healthPort }}
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
            {{- with .Values.controller.volumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- with default .Values.controller.resources .Values.sidecars.livenessProbe.resources }}
          resources: {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.sidecars.livenessProbe.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      volumes:
        - name: socket-dir
          emptyDir: {}
        {{- with .Values.controller.volumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.controller.affinity }}
      affinity: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.topologySpreadConstraints }}
      {{- $tscLabelSelector := dict "labelSelector" ( dict "matchLabels" ( dict "app" "efs-csi-controller" ) ) }}
      {{- $constraints := list }}
      {{- range .Values.controller.topologySpreadConstraints }}
        {{- $constraints = mustAppend $constraints (mergeOverwrite . $tscLabelSelector) }}
      {{- end }}
      topologySpreadConstraints:
        {{- $constraints | toYaml | nindent 8 }}
      {{- end }}
{{- end }}
