apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nats
  labels:
    app: nats
spec:
  selector:
    matchLabels:
      app: nats
  replicas: 1
  serviceName: "nats"
  template:
    metadata:
      labels:
        app: nats
    spec:
      volumes:
        - name: config-volume
          configMap:
            name: nats-server
        - name: pid
          emptyDir: {}
      shareProcessNamespace: true
      terminationGracePeriodSeconds: 60
      containers:
        - name: nats
          image: registry.homelab.dsb.dev/library/nats:2.1.7-alpine3.11
          ports:
            - containerPort: 4222
              name: client
              hostPort: 4222
            - containerPort: 7422
              name: leafnodes
              hostPort: 7422
            - containerPort: 6222
              name: cluster
            - containerPort: 8222
              name: monitor
            - containerPort: 7777
              name: metrics
          command:
            - "nats-server"
            - "--config"
            - "/etc/nats-config/nats.conf"
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: CLUSTER_ADVERTISE
              value: $(POD_NAME).nats.$(POD_NAMESPACE).svc
          volumeMounts:
            - name: config-volume
              mountPath: /etc/nats-config
            - name: pid
              mountPath: /var/run/nats
          livenessProbe:
            httpGet:
              path: /
              port: 8222
            initialDelaySeconds: 10
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /
              port: 8222
            initialDelaySeconds: 10
            timeoutSeconds: 5
          lifecycle:
            preStop:
              exec:
                command: ["/bin/sh", "-c", "/nats-server -sl=ldm=/var/run/nats/nats.pid && /bin/sleep 60"]
