# Controller Service
kind: Deployment
apiVersion: apps/v1
metadata:
  name: ebs-csi-controller
  namespace: kube-system
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: ebs-csi-controller
      app.kubernetes.io/name: {{ include "aws-ebs-csi-driver.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: ebs-csi-controller
        app.kubernetes.io/name: {{ include "aws-ebs-csi-driver.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
      {{- if .Values.podAnnotations }}
      annotations: {{ toYaml .Values.podAnnotations | nindent 8 }}
      {{- end }}
    spec:
      nodeSelector:
        kubernetes.io/os: linux
        {{- with .Values.nodeSelector }}
{{ toYaml . | indent 8 }}
        {{- end }}
      serviceAccountName: ebs-csi-controller-sa
      priorityClassName: system-cluster-critical
      {{- with .Values.affinity }}
      affinity: {{ toYaml . | nindent 8 }}
      {{- end }}
      tolerations:
        - operator: Exists
      {{- with .Values.tolerations }}
{{ toYaml . | indent 8 }}
      {{- end }}
      containers:
        - name: ebs-plugin
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - controller
            - --endpoint=$(CSI_ENDPOINT)
            {{ include "aws-ebs-csi-driver.extra-volume-tags" . }}
            - --logtostderr
            - --v=5
          env:
            - name: CSI_ENDPOINT
              value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
            - name: AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: aws-secret
                  key: key_id
                  optional: true
            - name: AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: aws-secret
                  key: access_key
                  optional: true
            {{- if .Values.region }}
            - name: AWS_REGION
              value: {{ .Values.region }}
            {{- end }}
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
          ports:
            - name: healthz
              containerPort: 9808
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: healthz
            initialDelaySeconds: 10
            timeoutSeconds: 3
            periodSeconds: 10
            failureThreshold: 5
          {{- with .Values.resources }}
          resources: {{ toYaml . | nindent 12 }}
          {{- end }}
        - name: csi-provisioner
          image: {{ printf "%s:%s" .Values.sidecars.provisionerImage.repository .Values.sidecars.provisionerImage.tag }}
          args:
            - --csi-address=$(ADDRESS)
            - --v=5
            {{- if .Values.enableVolumeScheduling }}
            - --feature-gates=Topology=true
            {{- end}}
            - --enable-leader-election
            - --leader-election-type=leases
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
        - name: csi-attacher
          image: {{ printf "%s:%s" .Values.sidecars.attacherImage.repository .Values.sidecars.attacherImage.tag }}
          args:
            - --csi-address=$(ADDRESS)
            - --v=5
            - --leader-election=true
            - --leader-election-type=leases
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
        {{- if .Values.enableVolumeSnapshot }}
        - name: csi-snapshotter
          image: {{ printf "%s:%s" .Values.sidecars.snapshotterImage.repository .Values.sidecars.snapshotterImage.tag }}
          args:
            - --csi-address=$(ADDRESS)
            - --leader-election=true
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
        {{- end }}
        {{- if .Values.enableVolumeResizing }}
        - name: csi-resizer
          image: {{ printf "%s:%s" .Values.sidecars.resizerImage.repository .Values.sidecars.resizerImage.tag }}
          imagePullPolicy: Always
          args:
            - --csi-address=$(ADDRESS)
            - --v=5
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
        {{- end }}
        - name: liveness-probe
          image: {{ printf "%s:%s" .Values.sidecars.livenessProbeImage.repository .Values.sidecars.livenessProbeImage.tag }}
          args:
            - --csi-address=/csi/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
      volumes:
        - name: socket-dir
          emptyDir: {}
