{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set loggerConfigmapName = "neutron-openvswitch-agent" %}
{%- set netHostTrue = "yes" %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: neutron-openvswitch-agent-{{ kolla_kubernetes.template.vars.type}}
  labels:
    component: neutron
    system: openvswitch-agent-{{ kolla_kubernetes.template.vars.type}}
  namespace: {{ kolla_kubernetes_namespace }}
spec:
  template:
    metadata:
      labels:
        component: neutron
        system: openvswitch-agent-{{ kolla_kubernetes.template.vars.type}}
      annotations:
#FIXME Once out of alpha, this should be converted to yaml.
        pod.alpha.kubernetes.io/init-containers: '[
          {
              "name": "update-config",
              "image": "{{ kolla_toolbox_image_full }}",
              "command": [
                  "/bin/sh",
                  "-c",
                  "cp -a /srv/configmap/..data/* /srv/pod-main-config/;
                  TUNNEL_INTERFACE={{ tunnel_interface }};
                  F=/var/lib/kolla-kubernetes/neutron-openvswitch-agent/tunnel_interface;
                  [ -f $F ] && TUNNEL_INTERFACE=$(<$F);
                  IP=$(ip addr list \"$TUNNEL_INTERFACE\" | grep ''inet '' |cut -d'' '' -f6|cut -d/ -f1);
{%- if enable_openvswitch_tcp == "yes" -%}
                  crudini --set /srv/pod-main-config/ml2_conf.ini ovs ovsdb_connection tcp:$IP:6640;
{%- endif -%}
                  crudini --set /srv/pod-main-config/ml2_conf.ini ovs local_ip $IP"
              ],
              "volumeMounts": [
                  {
                      "name": "neutron-openvswitch-agent-configmap",
                      "mountPath": "/srv/configmap"
                  },
                  {
                      "name": "pod-main-config",
                      "mountPath": "/srv/pod-main-config"
                  },
                  {
                      "name": "host-var-lib-kk",
                      "mountPath": "/var/lib/kolla-kubernetes"
                  }
              ]
          },
          {
              "name": "update-permissions",
              "image": "{{ neutron_openvswitch_agent_image_full }}",
              "securityContext": {
                  "runAsUser": 0
              },
              "command": [
                  "/bin/sh",
                  "-c",
                  "chown --recursive neutron.kolla /var/log/kolla;"
              ],
              "volumeMounts": [
                  {
                      "name": "kolla-logs",
                      "mountPath": "/var/log/kolla"
                  }
              ]
           }
        ]'
    spec:
      hostNetwork: True
      hostIPC: True
      nodeSelector:
{%- if kolla_kubernetes.template.vars.type == "compute" %}
{%- set selector = kolla_kubernetes_hostlabel_compute %}
{%- else %}
{%- set selector = kolla_kubernetes_hostlabel_network |
                   default(kolla_kubernetes_hostlabel_controller)
%}
{%- endif %}
          {{ selector.key }}: {{ selector.value }}
      containers:
        - image: "{{ neutron_openvswitch_agent_image_full }}"
          name: main
          securityContext:
            privileged: true
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
              readOnly: true
            - mountPath: /var/lib/kolla-kubernetes/neutron-openvswitch-agent
              name: openvswitch-config
              readOnly: true
            - mountPath: /var/run
              name: host-run
            - mountPath: /dev
              name: host-dev
            - mountPath: /lib/modules
              name: host-lib-modules
              readOnly: true
{{ lib.common_containers(indent=8) }}
# NOTE: (sbezverk) Linux bridge will be moved to its own file at the time of development
# keeping it here just for future reference.
#
#    - image: "{{ neutron_linuxbridge_agent_image_full }}"
#      name: neutron-linuxbridge-agent
#      securityContext:
#        privileged: true
#      volumeMounts:
#        - mountPath: {{ container_config_directory }}/neutron-linuxbridge-agent/
#          name: neutron-linuxbridge-agent-config
#          readOnly: true
#        - mountPath: /var/run
#          name: host-run
#        - mountPath: /etc/localtime
#          name: host-etc-localtime
#          readOnly: true
#        - mountPath: /lib/modules
#          name: host-lib-modules
#          readOnly: true
#        - mountPath: /var/log/kolla/
#          name: kolla-logs
#      env:
#        - name: KOLLA_CONFIG_STRATEGY
#          value: {{ config_strategy }}
#        - name: NEUTRON_BRIDGE
#          value: "br-ex"
#        - name: NEUTRON_INTERFACE
#          value: {{ neutron_external_interface }}
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: neutron-openvswitch-agent-configmap
          configMap:
            name: neutron-openvswitch-agent
            items:
            - key: neutron.conf
              path: neutron.conf
            - key: config.json
              path: config.json
            - key: ml2-conf.ini
              path: ml2_conf.ini
        - name: pod-main-config
          emptyDir: {}
# NOTE: (sbezverk) Linux bridge will be moved to its own file at the time of development
# keeping it here just for future reference.
#        - name: neutron-linuxbridge-agent-config
#          configMap:
#            name: neutron-linuxbridge-agent-configmap
        - name: openvswitch-config
          hostPath:
            path: /var/lib/kolla-kubernetes/neutron-openvswitch-agent
        - name: host-run
          hostPath:
            path: /var/run
        - name: host-dev
          hostPath:
            path: /dev
        - name: host-lib-modules
          hostPath:
            path: /lib/modules
        - name: host-var-lib-kk
          hostPath:
            path: /var/lib/kolla-kubernetes
