{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: ceph-mon
  labels:
    component: ceph
    system: mon
  namespace: {{ kolla_kubernetes_namespace }}
spec:
  template:
    metadata:
      labels:
        component: ceph
        system: mon
      annotations:
#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/;
                  STORAGE_INTERFACE={{ storage_interface }};
                  F=/var/lib/kolla-kubernetes/ceph-mon/storage_interface;
                  [ -f $F ] && STORAGE_INTERFACE=$(<$F);
                  IP=$(ip addr list {{ storage_interface }} | grep ''inet '' |cut -d'' '' -f6|cut -d/ -f1);
                  CONF=/srv/pod-main-config/ceph.conf;
                  found=0;
{%- for k in storage_ceph.monitors %}
                  [ $IP == ''{{ k }}'' ] && found=1 || true;
{%- endfor %}
                  echo $found > /srv/pod-main-config/inlist;
                  sed -i ''/^mon host.*/d;/^mon host/d;/^mon addr/d'' $CONF;
                  sed -i ''/^\\[global\\]/a mon host = ''$IP $CONF;
                  sed -i ''/^\\[global\\]/a mon addr = ''$IP'':6789'' $CONF;
                  sed -i ''/^\\[global\\]/a mon initial members = {{ storage_ceph.initial_mon }}'' $CONF;
                  sed -i ''s/@MONID@/''$(hostname)''/;s/@MONADDR@/''$IP''/'' /srv/pod-main-config/config.json;"
              ],
              "volumeMounts": [
                  {
                      "name": "ceph-mon-config",
                      "mountPath": "/srv/configmap"
                  },
                  {
                      "name": "pod-main-config",
                      "mountPath": "/srv/pod-main-config"
                  },
                  {
                      "name": "ceph-mon",
                      "mountPath": "/var/lib/ceph/mon"
                  }
              ]
          }
        ]'
    spec:
      hostNetwork: True
      hostPID: True
      nodeSelector:
{%- set selector = kolla_kubernetes_hostlabel_ceph_mon |
                   default(kolla_kubernetes_hostlabel_storage|
                       default(kolla_kubernetes_hostlabel_controller)
                   )
%}
          {{ selector.key }}: {{ selector.value }}
      containers:
        - image: "{{ ceph_mon_image_full }}"
          name: main
          command:
            - /bin/bash
            - -ec
            - |
              if [ $(cat {{ container_config_directory }}/inlist) == "0" ]; then
                echo "Not a mon... blocking.";
                while true; do sleep 10000; done;
              fi;
              kolla_start
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: {{ container_config_directory }}/ceph.conf
              name: pod-main-config
              readOnly: true
              subPath: ceph.conf
            - mountPath: {{ container_config_directory }}/config.json
              name: pod-main-config
              readOnly: true
              subPath: config.json
            - mountPath: /var/lib/ceph
              name: ceph-mon
            - mountPath: {{ container_config_directory }}/ceph.client.mon.keyring
              subPath: data
              name: ceph-client-mon-keyring
            - mountPath: {{ container_config_directory }}/ceph.client.admin.keyring
              subPath: data
              name: ceph-client-admin-keyring
            - mountPath: {{ container_config_directory }}/ceph.monmap
              subPath: data
              name: ceph-monmap
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: pod-main-config
          emptyDir: {}
        - name: ceph-mon-config
          configMap:
            name: ceph-mon
        - name: ceph-client-mon-keyring
          secret:
            secretName: ceph-client-mon-keyring
        - name: ceph-client-admin-keyring
          secret:
            secretName: ceph-client-admin-keyring
        - name: ceph-monmap
          secret:
            secretName: ceph-monmap
        - name: ceph-mon
          hostPath:
            path: /var/lib/kolla/volumes/ceph-mon
