{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- set index = kolla_kubernetes.template.vars.index %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: v1
kind: Pod
metadata:
  name: ceph-osd{{ index }}
  namespace: {{ kolla_kubernetes_namespace }}
  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-osd/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;
              sed -i ''s/@HOSTADDR@/''$IP''/g;s/@CLUSTERADDR@/''$IP''/g'' /srv/pod-main-config/config.json;"
          ],
          "volumeMounts": [
              {
                  "name": "ceph-osd-config",
                  "mountPath": "/srv/configmap"
              },
              {
                  "name": "pod-main-config",
                  "mountPath": "/srv/pod-main-config"
              },
              {
                  "name": "ceph-osd",
                  "mountPath": "/var/lib/ceph/osd"
              }
          ]
      }
    ]'
spec:
  hostNetwork: True
  hostPID: True
  nodeSelector:
    kubernetes.io/hostname: {{ storage_ceph.initial_mon }}
  restartPolicy: Never
  containers:
    - image: "{{ ceph_osd_image_full }}"
      name: main
      command:
        - /bin/bash
        - -ec
        - |
          mount {{ ceph_osd_data_dev[index | int] }} /var/lib/ceph/osd/ceph-{{ index }}
          kolla_start
      securityContext:
        privileged: true
      env:
        - name: KOLLA_CONFIG_STRATEGY
          value: "{{ config_strategy }}"
        - name: OSD_ID
          value: "{{ index }}"
        - name: JOURNAL_PARTITION
          value: {{ ceph_osd_journal_dev[index | int] }}
        - name: HOSTNAME
          value: {{ storage_ceph.initial_mon }}
      volumeMounts:
{{ lib.common_volume_mounts(indent=8) }}
        - 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-osd
        - mountPath: {{ container_config_directory }}/ceph.client.admin.keyring
          subPath: data
          name: ceph-client-admin-keyring
        - mountPath: /dev
          name: host-dev
  volumes:
{{ lib.common_volumes(indent=4) }}
    - name: pod-main-config
      emptyDir: {}
    - name: ceph-osd-config
      configMap:
        name: ceph-osd
    - name: ceph-osd
      hostPath:
        path: /var/lib/kolla/volumes/ceph-osd
    - name: host-dev
      hostPath:
        path: /dev
    - name: ceph-client-admin-keyring
      secret:
        secretName: ceph-client-admin-keyring
