# Elasticsearch 7.17.0 for Temporal visibility store
# Deployed to worker nodes (not control plane to save CP resources for LLM work)
# 2Gi heap + 4Gi memory limit for stable operation

apiVersion: v1
kind: ConfigMap
metadata:
  name: elasticsearch-config
  namespace: temporal
data:
  elasticsearch.yml: |
    cluster.name: temporal-elasticsearch
    node.name: temporal-elasticsearch-0
    discovery.type: single-node
    network.host: 0.0.0.0
    http.host: 0.0.0.0

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: temporal-elasticsearch
  namespace: temporal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: temporal-elasticsearch
  template:
    metadata:
      labels:
        app: temporal-elasticsearch
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: node-role.kubernetes.io/worker
                operator: Exists
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
        env:
        - name: discovery.type
          value: single-node
        - name: "ES_JAVA_OPTS"
          value: "-Xms2g -Xmx2g"
        ports:
        - containerPort: 9200
          name: http
        - containerPort: 9300
          name: transport
        livenessProbe:
          httpGet:
            path: /_cluster/health
            port: 9200
          initialDelaySeconds: 180
          periodSeconds: 10
          timeoutSeconds: 10
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /_cluster/health?local=true
            port: 9200
          initialDelaySeconds: 150
          periodSeconds: 10
          timeoutSeconds: 10
          failureThreshold: 5
        resources:
          requests:
            cpu: 500m
            memory: 2Gi
          limits:
            cpu: 2000m
            memory: 4Gi
        volumeMounts:
        - name: config
          mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
          subPath: elasticsearch.yml
      volumes:
      - name: config
        configMap:
          name: elasticsearch-config

---
apiVersion: v1
kind: Service
metadata:
  name: temporal-elasticsearch
  namespace: temporal
spec:
  selector:
    app: temporal-elasticsearch
  ports:
  - port: 9200
    targetPort: 9200
    name: http
  - port: 9300
    targetPort: 9300
    name: transport
  type: ClusterIP
