kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: {{ include "openstack-manila-csi.nodeplugin.fullname" . }}
  labels:
    app: {{ include "openstack-manila-csi.name" . }}
    chart: {{ include "openstack-manila-csi.chart" . }}
    component: {{ .Values.nodeplugin.name }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ include "openstack-manila-csi.name" . }}
      component: {{ .Values.nodeplugin.name }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ include "openstack-manila-csi.name" . }}
        chart: {{ include "openstack-manila-csi.chart" . }}
        component: {{ .Values.nodeplugin.name }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
    spec:
      serviceAccountName: {{ include "openstack-manila-csi.serviceAccountName.nodeplugin" . }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
        {{- range .Values.shareProtocols }}
        - name: {{ .protocolSelector | lower }}-registrar
          image: "{{ $.Values.nodeplugin.registrar.image.repository }}:{{ $.Values.nodeplugin.registrar.image.tag }}"
          args:
            - "--v=5"
            - "--csi-address=/csi/csi.sock"
            - "--kubelet-registration-path=/var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}/csi.sock"
          lifecycle:
            preStop:
              exec:
                  command: [
                    "/bin/sh", "-c",
                    'rm -rf /registration/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
                  /registration/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}-reg.sock'
                  ]
          env:
            - name: KUBE_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          imagePullPolicy: {{ $.Values.nodeplugin.registrar.image.pullPolicy }}
          volumeMounts:
            - name: {{ .protocolSelector | lower }}-plugin-dir
              mountPath: /csi
            - name: registration-dir
              mountPath: /registration
          resources:
{{ toYaml $.Values.nodeplugin.registrar.resources | indent 12 }}
        - name: {{ .protocolSelector | lower }}-nodeplugin
          securityContext:
            privileged: true
            capabilities:
              add: ["SYS_ADMIN"]
            allowPrivilegeEscalation: true
          image: "{{ $.Values.csimanila.image.repository }}:{{ $.Values.csimanila.image.tag }}"
          command: ["/bin/sh", "-c",
            '/bin/manila-csi-plugin
            --v=5
            --nodeid=$(NODE_ID)
            {{- if $.Values.csimanila.topologyAwarenessEnabled }}
            --with-topology
            --nodeaz={{ $.Values.csimanila.nodeAZ }}
            {{- end }}
            --endpoint=$(CSI_ENDPOINT)
            --drivername=$(DRIVER_NAME)
            --share-protocol-selector=$(MANILA_SHARE_PROTO)
            --fwdendpoint=$(FWD_CSI_ENDPOINT)'
          ]
          env:
            - name: DRIVER_NAME
              value: {{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
            - name: NODE_ID
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: CSI_ENDPOINT
              value: "unix:///var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}/csi.sock"
            - name: FWD_CSI_ENDPOINT
              value: "unix://{{ .fwdNodePluginEndpoint.dir }}/{{ .fwdNodePluginEndpoint.sockFile }}"
            - name: MANILA_SHARE_PROTO
              value: "{{ .protocolSelector }}"
          imagePullPolicy: {{ $.Values.csimanila.image.pullPolicy }}
          volumeMounts:
            - name: {{ .protocolSelector | lower }}-plugin-dir
              mountPath: /var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
            - name: {{ .protocolSelector | lower }}-fwd-plugin-dir
              mountPath: {{ .fwdNodePluginEndpoint.dir }}
          resources:
{{ toYaml $.Values.nodeplugin.nodeplugin.resources | indent 12 }}
        {{- end }}
      volumes:
        - name: registration-dir
          hostPath:
            path: /var/lib/kubelet/plugins_registry
            type: Directory
        {{- range .Values.shareProtocols }}
        - name: {{ .protocolSelector | lower }}-plugin-dir
          hostPath:
            path: /var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
            type: DirectoryOrCreate
        - name: {{ .protocolSelector | lower }}-fwd-plugin-dir
          hostPath:
            path: {{ .fwdNodePluginEndpoint.dir }}
            type: Directory
        {{- end }}
    {{- if .Values.nodeplugin.affinity -}}
      affinity:
{{ toYaml .Values.nodeplugin.affinity . | indent 8 }}
    {{- end -}}
    {{- if .Values.nodeplugin.nodeSelector -}}
      nodeSelector:
{{ toYaml .Values.nodeplugin.nodeSelector | indent 8 }}
    {{- end -}}
    {{- if .Values.nodeplugin.tolerations -}}
      tolerations:
{{ toYaml .Values.nodeplugin.tolerations | indent 8 }}
    {{- end -}}
