apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: elasticsearch
  namespace: elastic-search
  annotations:
    eck.k8s.elastic.co/downward-node-labels: "topology.kubernetes.io/zone"
spec:
  version: 8.15.2
  http:
    tls:
      selfSignedCertificate:
        disabled: true
    service:
      spec:
        type: ClusterIP
  nodeSets:
    - name: default
      count: 3
      volumeClaimTemplates:
        - metadata:
            name: elasticsearch-data
          spec:
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 128Gi
            storageClassName: managed-premium
      config:
        node.roles: [ "master", "data", "ingest" ]
        node.store.allow_mmap: false
        node.attr.zone: "${ZONE}"
        cluster.routing.allocation.awareness.attributes: k8s_node_name,zone
      podTemplate:
        metadata:
          annotations:
            traffic.sidecar.istio.io/includeInboundPorts: "*"
            traffic.sidecar.istio.io/excludeOutboundPorts: "9300"
            traffic.sidecar.istio.io/excludeInboundPorts: "9300"
        spec:
          tolerations:
            - effect: NoSchedule
              key: app
              value: "cluster"
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                  - matchExpressions:
                      - key: agentpool
                        operator: In
                        values:
                          - poolz1
                          - poolz2
                          - poolz3
                  - matchExpressions:
                      - key: topology.kubernetes.io/zone
                        operator: In
                        values:
                          - "$(ZONE)-1"
                          - "$(ZONE)-2"
                          - "$(ZONE)-3"
          containers:
            - name: elasticsearch
              env:
                - name: ES_JAVA_OPTS
                  value: "-Xms1g -Xmx1g"
                - name: ZONE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.annotations['topology.kubernetes.io/zone']
              resources:
                requests:
                  memory: 2Gi
                  cpu: 0.2
                limits:
                  memory: 2Gi
                  cpu: 2
          topologySpreadConstraints:
            - maxSkew: 1
              topologyKey: topology.kubernetes.io/zone
              whenUnsatisfiable: DoNotSchedule
              labelSelector:
                matchLabels:
                  elasticsearch.k8s.elastic.co/cluster-name: elasticsearch
                  elasticsearch.k8s.elastic.co/statefulset-name: elasticsearch-es-default

