{{- if .Values.nats.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "eddi.fullname" . }}-nats
  labels:
    {{- include "eddi.labels" . | nindent 4 }}
    app.kubernetes.io/component: messaging
spec:
  serviceName: {{ include "eddi.fullname" . }}-nats
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "eddi.fullname" . }}-nats
      app.kubernetes.io/component: messaging
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "eddi.fullname" . }}-nats
        app.kubernetes.io/component: messaging
        app.kubernetes.io/part-of: eddi
    spec:
      containers:
        - name: nats
          image: "{{ .Values.nats.image.repository }}:{{ .Values.nats.image.tag }}"
          command: ["nats-server", "--jetstream", "--store_dir=/data", "-m", "8222"]
          ports:
            - containerPort: 4222
              name: client
            - containerPort: 8222
              name: monitoring
          volumeMounts:
            - name: nats-data
              mountPath: /data
          resources:
            {{- toYaml .Values.nats.resources | nindent 12 }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: monitoring
            initialDelaySeconds: 5
            periodSeconds: 15
          readinessProbe:
            httpGet:
              path: /healthz
              port: monitoring
            initialDelaySeconds: 3
            periodSeconds: 10
  volumeClaimTemplates:
    - metadata:
        name: nats-data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: {{ .Values.nats.storage.size }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "eddi.fullname" . }}-nats
  labels:
    {{- include "eddi.labels" . | nindent 4 }}
    app.kubernetes.io/component: messaging
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app.kubernetes.io/name: {{ include "eddi.fullname" . }}-nats
    app.kubernetes.io/component: messaging
  ports:
    - name: client
      port: 4222
      targetPort: client
    - name: monitoring
      port: 8222
      targetPort: monitoring
{{- end }}
