apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: neutron-openvswitch-agent
  labels:
    component: neutron
    system: openvswitch-agent
spec:
  template:
    metadata:
      name: neutron-openvswitch-agent
      labels:
        component: neutron
        system: openvswitch-agent
      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": "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
      containers:
        - image: "{{ neutron_openvswitch_agent_image_full }}"
          name: main
          securityContext:
            privileged: true
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
          volumeMounts:
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
              readOnly: true
            - mountPath: /var/lib/kolla-kubernetes/neutron-openvswitch-agent
              name: openvswitch-config
              readOnly: true
            - mountPath: /run
              name: host-run
            - mountPath: /dev
              name: host-dev
            - 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

# 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: /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:
        - name: neutron-openvswitch-agent-configmap
          configMap:
            name: neutron-openvswitch-agent-configmap
            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: /run
        - name: host-dev
          hostPath:
            path: /dev
        - name: host-etc-localtime
          hostPath:
            path: /etc/localtime
        - name: host-lib-modules
          hostPath:
            path: /lib/modules
        - name: kolla-logs
          emptyDir: {}
