{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set loggerConfigmapName = "cinder-volume" %}
{%- set netHostTrue = "yes" %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: cinder-volume-lvm
  namespace: {{ kolla_kubernetes_namespace }}
  labels:
    component: cinder-volume-lvm
    system: cinder-volume-lvm
spec:
  template:
    metadata:
      labels:
        component: cinder-volume-lvm
        system: cinder-volume-lvm
      annotations:
        pod.alpha.kubernetes.io/init-containers: '[
          {
             "name": "initialize-cinder-volume-lvm",
             "image": "{{ kolla_toolbox_image_full }}",
             "command": [
                 "bash",
                 "-ce",
                 "cp -a /srv/configmap/..data/* /srv/pod-main-config/;
                  {%- if storage_interface is defined -%}
                    compute_node_mgnt_interface=''{{ storage_interface }}'';
                  {%- else -%}
                    compute_node_mgnt_interface=$(ip route | grep default | awk ''{print $5}'');
                  {%- endif -%}
                  compute_node_ip=$(ip addr show dev $compute_node_mgnt_interface | grep ''state UP'' -A2 | tail -n1 | awk ''{print $2}'' | cut -f1  -d''/'');
                  {%- if cinder_storage_list is defined -%}
                     {%- for backends in cinder_storage_list -%}
                        {%- if 'lvm_backends' in backends -%}
                           {%- for lvm_backend in backends -%}
                              {%- for iscsi_targets in backends[lvm_backend] -%}
                                 {%- for item in iscsi_targets -%}
                                    {%- set vg_ip = iscsi_targets[item]['ip'] -%}
                                    {%- set vg_name = iscsi_targets[item]['vg_name'] -%}
                                    {%- set lvm_enabled_backend = (vg_ip + '_' + vg_name)|string -%}
                                      if [ $compute_node_ip = ''{{ vg_ip }}'' ];
                                      then crudini --set /srv/pod-main-config/cinder.conf DEFAULT enabled_backends {{ lvm_enabled_backend }};
                                           crudini --set /srv/pod-main-config/cinder.conf {{ lvm_enabled_backend }} volume_group {{ vg_name }};
                                           crudini --set /srv/pod-main-config/cinder.conf {{ lvm_enabled_backend }} volume_backend_name {{ lvm_enabled_backend }};
                                           crudini --set /srv/pod-main-config/cinder.conf {{ lvm_enabled_backend }} volume_driver  ''cinder.volume.drivers.lvm.LVMVolumeDriver'';
                                           crudini --set /srv/pod-main-config/cinder.conf {{ lvm_enabled_backend }} iscsi_helper  ''tgtadm'';
                                           crudini --set /srv/pod-main-config/cinder.conf {{ lvm_enabled_backend }} iscsi_protocol  ''iscsi'';
                                           crudini --set /srv/pod-main-config/cinder.conf {{ lvm_enabled_backend }} iscsi_ip_address {{ vg_ip }};
                                      fi;
                                 {%- endfor -%}
                              {%- endfor -%}
                           {%- endfor -%}
                        {%- endif -%}
                     {%- endfor -%}
                  {%- endif -%} "],
             "volumeMounts": [
                {
                   "name": "cinder-volume-configmap",
                   "mountPath": "/srv/configmap"
                },
                {
                   "name": "pod-main-config",
                   "mountPath": "/srv/pod-main-config"
                }
             ]
          },
          {
             "name": "initialize-cinder-logs",
             "image": "{{ cinder_volume_image_full }}",
             "command": [
                 "sh",
                 "-ce",
                 "mkdir /var/log/kolla/cinder ;
                  chown -R cinder: /var/log/kolla/cinder "],
             "volumeMounts": [
                {
                   "name": "kolla-logs",
                   "mountPath": "/var/log/kolla"
                }
             ]
          }
        ]'
    spec:
      hostNetwork: True
      hostIPC: True
      nodeSelector:
{%- set selector = kolla_kubernetes_hostlabel_storage_lvm |
                       default(kolla_kubernetes_hostlabel_storage |
                         default(kolla_kubernetes_hostlabel_controller)
                       )
%}
          {{ selector.key }}: {{ selector.value }}
      containers:
        - image: "{{ cinder_volume_image_full }}"
          name: main
          securityContext:
            privileged: true
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: COPY_ALWAYS
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
              readOnly: true
            - mountPath: /run
              name: run
            - mountPath: /dev
              name: dev
            - mountPath: /var/lib/cinder
              name: cinder-volumes
            - mountPath: /etc/iscsi
              name: iscsi-info
{{ lib.common_containers(indent=8) }}
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: kolla-kubernetes-events
          emptyDir: {}
        - name: pod-main-config
          emptyDir: {}
        - name: cinder-volume-configmap
          configMap:
            name: cinder-volume
        - name: run
          hostPath:
            path: /run
        - name: dev
          hostPath:
            path: /dev
        - name: cinder-volumes
          hostPath:
            path: /var/lib/cinder
        - name: iscsi-info
          hostPath:
            path: /etc/iscsi
