{%- set podTypeBootstrap = "yes" %}
{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: batch/v1
kind: Job
metadata:
  name: ceph-bootstrap-initial-mon
  namespace: {{ kolla_kubernetes_namespace }}
spec:
  template:
    metadata:
      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) || true;
                  IP=$(ip addr list {{ storage_interface }} | grep ''inet '' |cut -d'' '' -f6|cut -d/ -f1);
                  CONF=/srv/pod-main-config/ceph.conf;
                  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;"
              ],
              "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:
        kubernetes.io/hostname: {{ storage_ceph.initial_mon }}
      containers:
        - image: "{{ ceph_mon_image_full }}"
          name: main
          command:
          - /bin/bash
          - -ec
          - |
            export MON_IP=$(grep '^mon host' {{ container_config_directory }}/ceph.conf | awk '{print $4}');
            kolla_start
            echo -n 'FETCH_CEPH_KEYS: '; fetch_ceph_keys.py
          env:
            - name: KOLLA_BOOTSTRAP
              value: ""
            - name: KOLLA_CONFIG_STRATEGY
              value: "{{ config_strategy }}"
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
              readOnly: true
            - mountPath: /var/lib/ceph
              name: ceph-mon
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: pod-main-config
          emptyDir: {}
        - name: ceph-mon-config
          configMap:
            name: ceph-mon
        - name: ceph-mon
          hostPath:
            path: /var/lib/kolla/volumes/ceph-mon
      restartPolicy: OnFailure
