{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set replicas = global[kolla_kubernetes.template.vars.replicas] %}
{%- set serviceName = kolla_kubernetes.template.vars.service_name %}
{%- set serviceType = kolla_kubernetes.template.vars.service_type %}
{%- set configFileName = kolla_kubernetes.template.vars.config_file_name %}
{%- set configSectionName = kolla_kubernetes.template.vars.config_section_name %}
{%- set configListenHostParameter = kolla_kubernetes.template.vars.config_listen_host_parameter %}
{%- set configListenPortParameter = kolla_kubernetes.template.vars.config_listen_port_parameter %}
{%- set port = global[kolla_kubernetes.template.vars.port] %}
{%- set portName = kolla_kubernetes.template.vars.port_name %}
{%- set image = global[kolla_kubernetes.template.vars.image] %}
{%- set checkPath = kolla_kubernetes.template.vars.check_path %}
{%- set extraVolume = kolla_kubernetes.template.vars.extra_volumes | to_nice_yaml %}
{%- set extraVolumeMounts = kolla_kubernetes.template.vars.extra_volume_mounts | to_nice_yaml %}
{%- set extraConfigmapConfig = kolla_kubernetes.template.vars.extra_configmap_config | to_nice_yaml %}
{%- set privileged = kolla_kubernetes.template.vars.privileged | bool %}
{%- if serviceType == 'neutron' %}
{%- set selector = global[kolla_kubernetes.template.vars.service_location] |
                   default(kolla_kubernetes_hostlabel_network |
                     default(kolla_kubernetes_hostlabel_controller)
                   )
%}
{%- else %}
{%- set selector = global[kolla_kubernetes.template.vars.service_location] |
                       default(kolla_kubernetes_hostlabel_controller)
%}
{%- endif %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
   name: {{ resourceName }}
   namespace: {{ kolla_kubernetes_namespace }}
spec:
  replicas: {{ replicas }}
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        service: {{ serviceName }}
        type: {{ serviceType }}
      annotations:
        kolla_upgrade: minor_rolling_safe
#FIXME Once out of alpha, this should be converted to yaml.
#FIXME once all services are converted, bind_host can be dropped here and changed to default to 127.0.0.1 instead of 0.0.0.0.
# The init container overrides the listen address and port to ensure it does not conflict with haproxy and prevent
# other containers from directly accessing the service
        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 resourceName == 'kibana' %}
                   IP=\"127.0.0.1\";
                   PORT=\"8080\";
                   sed -i ''s|^elasticsearch.url:.*|elasticsearch.url: \"http://elasticsearch:9200\"|g'' /srv/pod-main-config/kibana.yml;
                   sed -i ''s|^server.host:.*|server.host: \"''$IP''\"|g'' /srv/pod-main-config/kibana.yml;
                   sed -i ''s|^server.port:.*|server.port: \"''$PORT''\"|g'' /srv/pod-main-config/kibana.yml;
{%- else %}
                   CONF=/srv/pod-main-config/{{ configFileName }};
{%- if resourceName == 'nova-api' %}
                   crudini --set $CONF DEFAULT metadata_listen 127.0.0.1;
                   crudini --set $CONF DEFAULT metadata_listen_port 8081;
{%- endif %}
                   crudini --set $CONF {{ configSectionName }} {{ configListenHostParameter }} 127.0.0.1;
                   crudini --set $CONF {{ configSectionName }} {{ configListenPortParameter }} 8080;
{%- endif %}
              "],
              "volumeMounts": [
                  {
                      "name": "service-configmap",
                      "mountPath": "/srv/configmap"
                  },
                  {
                      "name": "pod-main-config",
                      "mountPath": "/srv/pod-main-config"
                  }
              ]
          }
      ]'
    spec:
#You've got 2 days to drain or figure out why it won't.
      terminationGracePeriodSeconds: {{ 2 * 24 * 60 * 60 }}
      nodeSelector:
          {{ selector.key }}: {{ selector.value }}
      containers:
        - name: haproxy
          image: "{{ haproxy_image_full }}"
          command:
          - /bin/bash
          - -c
          - |
            kolla_start;
            touch /var/lib/kolla-kubernetes/event/shutdown;
          readinessProbe:
            httpGet:
              path: {{ checkPath }}
              port: {{ port }}
            initialDelaySeconds: 5
            timeoutSeconds: 5
          lifecycle:
            preStop:
              exec:
#FIXME move script into haproxy container
#NOTE this only works if you arn't doing a haproxy reconfigure too.
#But shouldn't ever have to do that in a setup like this.
                command:
                - /bin/bash
                - -c
                - |
                  kill -USR1 $(</var/run/haproxy.pid);
                  while true; do sleep 1000; done
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: /var/lib/kolla-kubernetes/event
              name: kolla-kubernetes-events
            - mountPath: {{ container_config_directory }}
              name: haproxy-config
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
          ports:
            - containerPort: {{ port }}
              name: {{ portName }}
{%- if resourceName == 'nova-api' %}
            - containerPort: {{ nova_metadata_port }}
              name: nova-metadata
{%- endif %}
        - name: main
          image: "{{ image }}"
          lifecycle:
            preStop:
              exec:
                command:
                - /bin/bash
                - -c
                - while true; do sleep 1; [ -f /var/lib/kolla-kubernetes/event/shutdown ] && break; done
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: /var/lib/kolla-kubernetes/event
              name: kolla-kubernetes-events
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
{{ extraVolumeMounts | indent(12, True) }}
{%- if privileged %}
          securityContext:
            privileged: true
{%- endif %}
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
{{ lib.common_containers(indent=8) }}
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: kolla-kubernetes-events
          emptyDir: {}
        - name: pod-main-config
          emptyDir: {}
        - name: service-configmap
          configMap:
            name: {{ resourceName }}
{{ extraConfigmapConfig | indent(12, True) }}
        - name: haproxy-config
          configMap:
            name: {{ resourceName }}-haproxy
{{ extraVolume | indent(8, True) }}
