{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set loggerConfigmapName = "openvswitch-db-server" %}
{%- set netHostTrue = "yes" %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: openvswitch-ovsdb-{{ kolla_kubernetes.template.vars.type}}
  labels:
    component: openvswitch
    system: openvswitch-db-{{ kolla_kubernetes.template.vars.type}}
  namespace: {{ kolla_kubernetes_namespace }}
spec:
  template:
    metadata:
      labels:
        component: openvswitch
        system: openvswitch-db-{{ kolla_kubernetes.template.vars.type}}
      annotations:
        pod.alpha.kubernetes.io/init-containers: '[
          {
             "name": "initialize-ovs-db",
             "image": "{{ openvswitch_db_image_full }}",
             "command": [
                 "sh",
                 "-xec",
                 "mkdir -p /var/log/kolla/openvswitch;
                  DB=/etc/openvswitch/conf.db;
                  /usr/bin/ovsdb-tool create $DB;
                  echo $(date) >> /var/log/kolla/db-create.log;" ],
             "volumeMounts": [
                {
                   "name": "openvswitch-db",
                   "mountPath": "/etc/openvswitch/"
                },
                {
                   "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_openvswitch_ovsdb_compute |
                   default(kolla_kubernetes_hostlabel_compute) %}
{%- else %}
{%- set selector = global['kolla_kubernetes_hostlabel_openvswitch_ovsdb_' ~ kolla_kubernetes.template.vars.type] |
                   default(kolla_kubernetes_hostlabel_network |
                     default(kolla_kubernetes_hostlabel_controller)
                   )
%}
{%- endif %}
          {{ selector.key }}: {{ selector.value }}
      containers:
        - image: "{{ openvswitch_db_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-db-config
              readOnly: true
            - mountPath: /etc/openvswitch
              name: openvswitch-db
            - mountPath: /var/run/openvswitch
              name: openvswitch-run
            - mountPath: /dev
              name: host-dev
          command: ["sh", "-c"]
          args:
            - /usr/sbin/ovsdb-server
              /etc/openvswitch/conf.db
              -vconsole:emer
              -vsyslog:err
              -vfile:info
              --remote=punix:/var/run/openvswitch/db.sock
              --remote=ptcp:6640:0.0.0.0
              --log-file=/var/log/kolla/openvswitch/ovsdb-server.log
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: openvswitch-db-config
          configMap:
            name: openvswitch-db-server
        - name: openvswitch-db
          emptyDir: {}
        - name: openvswitch-run
          hostPath:
            path: /var/run/openvswitch
        - name: host-dev
          hostPath:
            path: /dev
