{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set netHostTrue = "yes" %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: iscsi-tgtd
  labels:
    component: tgtd
    system: tgtd
  namespace: {{ kolla_kubernetes_namespace }}
spec:
  template:
    metadata:
      labels:
        component: tgtd
        system: tgtd
      annotations:
        pod.alpha.kubernetes.io/init-containers: '[
          {
             "name": "initialize-tgtd",
             "image": "{{ kolla_toolbox_image_full }}",
             "command": [
                 "sh",
                 "-ce",
                 "cp -a /srv/configmap/..data/* /srv/pod-main-config/;
                  {%- if storage_interface is defined -%}
                    compute_storage_interface=''{{ storage_interface }}'';
                  {%- else -%}
                    compute_storage_interface=$(ip route | grep default | awk ''{print $5}'');
                  {%- endif -%}
                  storage_ip=$(ip addr show dev $compute_storage_interface | grep ''state UP'' -A2 | tail -n1 | awk ''{print $2}'' | cut -f1  -d''/'');
                  jq ''.command = \"tgtd -d 1 -f --iscsi portal=''$storage_ip'':3260\"'' /srv/configmap/..data/config.json > /srv/pod-main-config/config.json; "],
             "volumeMounts": [
                {
                   "name": "tgtd-configmap",
                   "mountPath": "/srv/configmap"
                },
                {
                   "name": "pod-main-config",
                   "mountPath": "/srv/pod-main-config"
                }
             ]
          }
        ]'
    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: "{{ tgtd_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: /sys/fs/cgroup
              name: sys-fs-cgroup
            - mountPath: /lib/modules
              name: lib-modules
            - mountPath: /var/lib/cinder
              name: cinder-volumes
            - mountPath: /etc/iscsi
              name: iscsi-info
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: kolla-kubernetes-events
          emptyDir: {}
        - name: pod-main-config
          emptyDir: {}
        - name: tgtd-configmap
          configMap:
            name: tgtd
        - name: lib-modules
          hostPath:
            path: /lib/modules
        - name: run
          hostPath:
            path: /run
        - name: dev
          hostPath:
            path: /dev
        - name: sys-fs-cgroup
          hostPath:
            path: /sys/fs/cgroup
        - name: cinder-volumes
          hostPath:
            path: /var/lib/cinder
        - name: iscsi-info
          hostPath:
            path: /etc/iscsi
