{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set loggerConfigmapName = "openvswitch-vswitchd" %}
{%- set netHostTrue = "yes" %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: openvswitch-vswitchd-{{ kolla_kubernetes.template.vars.type}}
  labels:
    component: openvswitch
    system: openvswitch-vswitchd-{{ kolla_kubernetes.template.vars.type}}
  namespace: {{ kolla_kubernetes_namespace }}
spec:
  template:
    metadata:
      labels:
        component: openvswitch
        system: openvswitch-vswitchd-{{ kolla_kubernetes.template.vars.type}}
      annotations:
        pod.alpha.kubernetes.io/init-containers: '[
          {
             "name": "initialize-ovs-vswitchd",
             "image": "{{ openvswitch_vswitchd_image_full }}",
             "command": [
                 "sh",
                 "-xec",
                 "mkdir -p /var/log/kolla/openvswitch;
{%- if kolla_kubernetes_ovs_setup_bridge == "yes" %}
                  ovs-vsctl --no-wait --db=unix:/var/run/openvswitch/db.sock add-br {{ neutron_bridge_name }} ;
{%- if kolla_kubernetes_ovs_add_port == "yes" %}
                  ovs-vsctl --no-wait --db=unix:/var/run/openvswitch/db.sock add-port {{ neutron_bridge_name }} {{ neutron_external_interface }};
{%- endif %}
{%- endif %}
                  echo $(date) >> /var/log/kolla/vswitchd-init.log;" ],
             "volumeMounts": [
                {
                   "name": "kolla-logs",
                   "mountPath": "/var/log/kolla/"
                },
                {
                   "name": "openvswitch-run",
                   "mountPath": "/var/run/openvswitch/"
                }
             ]
          }
        ]'
    spec:
      hostNetwork: True
      hostIPC: True
      nodeSelector:
{%- if kolla_kubernetes.template.vars.type == "compute" %}
{%- set selector = kolla_kubernetes_hostlabel_openvswitch_vswitchd_compute |
                   default(kolla_kubernetes_hostlabel_compute) %}
{%- else %}
{%- set selector = global['kolla_kubernetes_hostlabel_openvswitch_vswitchd_' ~ kolla_kubernetes.template.vars.type] |
                   default(kolla_kubernetes_hostlabel_network |
                     default(kolla_kubernetes_hostlabel_controller)
                   )
%}
{%- endif %}
          {{ selector.key }}: {{ selector.value }}
      containers:
        - image: "{{ openvswitch_vswitchd_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: openvswitch-vswitchd-config
              readOnly: true
            - mountPath: /var/run/openvswitch
              name: openvswitch-run
            - mountPath: /dev
              name: host-dev
            - mountPath: /lib/modules
              name: host-lib-modules
              readOnly: true
          command: ["sh", "-xec"]
          args:
            - modprobe openvswitch || true;
              /usr/sbin/ovs-vswitchd
              unix:/var/run/openvswitch/db.sock
              -vconsole:emer
              -vsyslog:err
              -vfile:info
              --mlockall
              --log-file=/var/log/kolla/openvswitch/ovs-vswitchd.log
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: openvswitch-vswitchd-config
          configMap:
            name: openvswitch-vswitchd
        - name: openvswitch-run
          hostPath:
            path: /var/run/openvswitch
        - name: host-dev
          hostPath:
            path: /dev
        - name: host-lib-modules
          hostPath:
            path: /lib/modules
