{%- set resourceName = kolla_kubernetes.cli.args.resource_name %}
{%- import "services/common/common-lib.yml.j2" as lib with context %}
apiVersion: apps/v1beta1
kind: StatefulSet
spec:
  serviceName: "elasticsearch"
  replicas: {{ elasticsearch_replicas }}
  template:
    metadata:
      labels:
        service: elasticsearch
        type: elasticsearch
      annotations:
        pod.beta.kubernetes.io/init-containers: '[
          {
             "name": "initialize-elasticsearch",
             "image": "{{ kolla_toolbox_image_full }}",
             "imagePullPolicy": "IfNotPresent",
             "command": [
                 "sh",
                 "-c",
                 "cp -a /config/..data/* /elasticsearch/;
                  IP=$(ip addr list eth0 | grep ''inet '' |cut -d'' '' -f6|cut -d/ -f1);
                  sed -i ''s|^cluster.name:.*|cluster.name: \"''$KUBERNETES_SERVICE_HOST''\"|g'' /elasticsearch/elasticsearch.yml;
                  sed -i ''s|^network.host:.*|network.host: _eth0:ipv4_|g'' /elasticsearch/elasticsearch.yml;
                  sed -i ''s|^node.name:.*|node.name: \"''$IP''\"|g'' /elasticsearch/elasticsearch.yml;
                  sed -i ''s|^discovery.zen.ping.unicast.hosts:.*|discovery.zen.ping.unicast.hosts: \"''$IP''\"|g'' /elasticsearch/elasticsearch.yml; "],
             "volumeMounts": [
                {
                   "name": "elasticsearch-config",
                   "mountPath": "/config/"
                },
                {
                   "name": "pod-main-config",
                   "mountPath": "/elasticsearch/"
                }
             ]
          }
        ]'
    spec:
      nodeSelector:
{%- set selector = kolla_kubernetes_hostlabel_elasticsearch |
                       default(kolla_kubernetes_hostlabel_controller)
%}
          {{ selector.key }}: {{ selector.value }}
      containers:
        - name: main
          image: "{{ elasticsearch_image_full }}"
          volumeMounts:
{{ lib.common_volume_mounts(indent=12) }}
            - mountPath: {{ container_config_directory }}
              name: pod-main-config
            - mountPath: /var/lib/elasticsearch/data
              name: elasticsearch-persistent-storage
          env:
            - name: KOLLA_CONFIG_STRATEGY
              value: {{ config_strategy }}
          ports:
            - containerPort: {{ elasticsearch_port }}
              name: elasticsearch
      volumes:
{{ lib.common_volumes(indent=8) }}
        - name: elasticsearch-config
          configMap:
            name: elasticsearch
        - name: pod-main-config
          emptyDir: {}
        - name: elasticsearch-persistent-storage
          persistentVolumeClaim:
            claimName: {{ resourceName }}
metadata:
   name: elasticsearch
   namespace: {{ kolla_kubernetes_namespace }}
