{%- 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: neutron-dhcp-agent
  namespace: {{ kolla_kubernetes_namespace }}
  labels:
    component: neutron
    system: dhcp-agent
spec:
  template:
    metadata:
      labels:
        component: neutron
        system: dhcp-agent
      annotations:
#FIXME Its unclear if ovsdb_connection, local_ip, and/or hostPID required. Try removing them once we have a working gate to test with.
#FIXME Once out of alpha, this should be converted to yaml.
        pod.beta.kubernetes.io/init-containers: '[
            {
                "name": "update-config",
                "image": "{{ kolla_toolbox_image_full }}",
                "imagePullPolicy": "IfNotPresent",
                "command": [
                    "/bin/sh",
                    "-c",
                    "cp -a /srv/configmap/..data/* /srv/pod-main-config/;
{%- if enable_openvswitch_tcp == "yes" -%}
                    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);
                    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-dhcp-agent-configmap",
                        "mountPath": "/srv/configmap"
                    },
                    {
                        "name": "pod-main-config",
                        "mountPath": "/srv/pod-main-config"
                    }
                ]
            },
            {
                "name": "update-permissions",
                "image": "{{ neutron_dhcp_agent_image_full }}",
                "imagePullPolicy": "IfNotPresent",
                "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
      hostPID: True
      nodeSelector:
{%- set selector = kolla_kubernetes_hostlabel_dhcp_agent |
                       default(kolla_kubernetes_hostlabel_network |
                         default(kolla_kubernetes_hostlabel_controller)
                       )
%}
        {{ selector.key }}: {{ selector.value }}
      containers:
        - image: "{{ neutron_dhcp_agent_image_full }}"
          name: main
          securityContext:
            privileged: true
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
              readOnly: true
            - mountPath: /var/lib/neutron/kolla/
              name: neutron-metadata-socket
            - mountPath: /var/run
              name: host-run
            - mountPath: /run/netns/
              name: host-run-netns
              # shared: true # This seems not support in K8s yet :(
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: pod-main-config
          emptyDir: {}
        - name: neutron-dhcp-agent-configmap
          configMap:
            name: neutron-dhcp-agent
            items:
            - key: neutron.conf
              path: neutron.conf
            - key: config.json
              path: config.json
            - key: ml2-conf.ini
              path: ml2_conf.ini
            - key: dhcp-agent.ini
              path: dhcp_agent.ini
            - key: dnsmasq.conf
              path: dnsmasq.conf
        - name: host-run-netns
          hostPath:
            path: /run/netns
        - name: host-run
          hostPath:
            path: /var/run
        - name: neutron-metadata-socket
          hostPath:
            path: /var/lib/neutron/kolla/metadata_proxy
