# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# StatefulSet for ElasticSearch data
# See https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: es-data
  labels:
    component: elasticsearch
    role: data
spec:
  selector:
    matchLabels:
      component: elasticsearch
      role: data
  serviceName: elasticsearch-data
  replicas: 3
  template:
    metadata:
      labels:
        component: elasticsearch
        role: data
    spec:
      serviceAccountName: elasticsearch-data
      # see https://www.elastic.co/guide/en/elasticsearch/reference/current/vm-max-map-count.html
      initContainers:
      - name: init-sysctl
        image: busybox:1.27.2
        command:
        - sysctl
        - -w
        - vm.max_map_count=262144
        securityContext:
          privileged: true
      # This is using pod anti-affinity to ensure that the scheduler requires that pods are spread across all nodes.
      # https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: component
                operator: In
                values:
                  - elasticsearch
              - key: role
                operator: In
                values:
                - data
            topologyKey: kubernetes.io/hostname
      containers:
      - name: es-data
        image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
        # The configuration settings for Elasticsearch docker containers can be set via environment variables.
        # For more information see:
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-configuration-methods
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/important-settings.html
        env:
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/jvm-options.html
        - name: ES_JAVA_OPTS
          value: -Xms1536m -Xmx1536m
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.name.html
        - name: cluster.name
          value: myesdb
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/discovery-settings.html#unicast.hosts
        - name: discovery.zen.ping.unicast.hosts
          value: elasticsearch-discovery
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-http.html#_settings_2
        - name: http.compression
          value: "true"
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-http.html#_settings_2
        - name: http.cors.enabled
          value: "true"
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-http.html#_settings_2
        - name: http.cors.allow-origin
          value: "*"
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-node.html#master-node
        - name: node.master
          value: "false"
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-node.html#ingest-node
        - name: node.ingest
          value: "false"
        # https://www.elastic.co/guide/en/elasticsearch/reference/current/node.name.html
        - name: node.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/ingest-processors.html
        - name: processors
          valueFrom:
            resourceFieldRef:
              resource: limits.cpu
        resources:
          limits:
            cpu: "1000m"
          requests:
            cpu: "25m"
            memory: "1536Mi"
        ports:
        - containerPort: 9300
          name: transport
        - containerPort: 9200
          name: http
        livenessProbe:
          tcpSocket:
            port: transport
          initialDelaySeconds: 20
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /_cluster/health
            port: http
          initialDelaySeconds: 20
          timeoutSeconds: 5
        volumeMounts:
        - name: storage
          mountPath: /data
        - name: config
          mountPath: /pre-stop-hook.sh
          subPath: pre-stop-hook.sh
        lifecycle:
          preStop:
            exec:
              command: ["/bin/bash","/pre-stop-hook.sh"]
      volumes:
      - name: config
        configMap:
          name: es-cm
  volumeClaimTemplates:
    - metadata:
        name: storage
      spec:
        storageClassName: repd-fast
        accessModes: [ ReadWriteOnce ]
        resources:
          requests:
            storage: "12Gi"