# 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.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: es-master
  labels:
    component: elasticsearch
    role: master
spec:
  selector:
    matchLabels:
      component: elasticsearch
      role: master
  serviceName: "elasticsearch-master"
  replicas: 3
  template:
    metadata:
      labels:
        component: elasticsearch
        role: master
    spec:
      # 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
      containers:
      - name: es-master
        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: -Xms256m -Xmx256m
        - 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#minimum_master_nodes
        - name: discovery.zen.minimum_master_nodes
          value: "1"
        # 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-http.html#_settings_2
        - name: http.enabled
          value: "false"
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-node.html#master-node
        - name: node.master
          value: "true"
        # https://www.elastic.co/guide/en/elasticsearch/reference/6.5/modules-node.html#data-node
        - name: node.data
          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:
          requests:
            cpu: "250m"
          limits:
            cpu: "1000m"
        ports:
        - containerPort: 9300
          name: transport
        livenessProbe:
          tcpSocket:
            port: transport
          initialDelaySeconds: 20
          periodSeconds: 10
        volumeMounts:
        - name: storage
          mountPath: /data
      # 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:
                  - master
              topologyKey: kubernetes.io/hostname
  volumeClaimTemplates:
  - metadata:
      name: storage
    spec:
      storageClassName: standard
      accessModes: [ ReadWriteOnce ]
      resources:
        requests:
          storage: "2Gi"