apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: {{ template "nats.fullname" . }}
  labels:
    app: "{{ template "nats.name" . }}"
    chart: "{{ template "nats.chart" . }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  serviceName: {{ template "nats.fullname" . }}-headless
  replicas: {{ .Values.replicaCount }}
  updateStrategy:
    type: {{ .Values.statefulset.updateStrategy }}
    {{- if .Values.statefulset.rollingUpdatePartition }}
    rollingUpdate:
      partition: {{ .Values.statefulset.rollingUpdatePartition }}
    {{- end }}
  selector:
    matchLabels:
      app: "{{ template "nats.name" . }}"
      release: {{ .Release.Name | quote }}
  template:
    metadata:
      labels:
        app: "{{ template "nats.name" . }}"
        chart: "{{ template "nats.chart" . }}"
        release: {{ .Release.Name | quote }}
        {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
        {{- end }}
{{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
{{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
{{- if .Values.metrics.podAnnotations }}
{{ toYaml .Values.metrics.podAnnotations | indent 8 }}
{{- end }}
{{- end }}
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if eq .Values.antiAffinity "hard" }}
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app: "{{ template "nats.name" . }}"
                  release: {{ .Release.Name | quote }}
      {{- else if eq .Values.antiAffinity "soft" }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            podAffinityTerm:
              topologyKey: kubernetes.io/hostname
              labelSelector:
                matchLabels:
                  app: "{{ template "nats.name" . }}"
                  release: {{ .Release.Name | quote }}
      {{- end }}
      containers:
      - name: {{ template "nats.name" . }}
        image: {{ template "nats.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
        - gnatsd
        args:
        - -c
        - /opt/bitnami/nats/gnatsd.conf
        {{- if .Values.extraArgs }}
{{ toYaml .Values.extraArgs | indent 8 }}
        {{- end }}
        ports:
        - name: client
          containerPort: {{ .Values.client.service.port }}
        - name: cluster
          containerPort: {{ .Values.cluster.service.port }}
        - name: monitoring
          containerPort: {{ .Values.monitoring.service.port }}
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /
            port: monitoring
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
        {{- end }}
        {{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /
            port: monitoring
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
        {{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
          - name: config
            mountPath: /opt/bitnami/nats/gnatsd.conf
            subPath: gnatsd.conf
        {{- if .Values.sidecars }}
{{ toYaml .Values.sidecars | indent 6 }}
        {{- end }}
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ template "metrics.image" . }}
        imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
        args:
{{ toYaml .Values.metrics.args | indent 10 -}}
          - "http://localhost:{{ .Values.monitoring.service.port }}"
        ports:
        - name: metrics
          containerPort: {{ .Values.metrics.port }}
        livenessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: 15
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: 5
          timeoutSeconds: 1
        resources:
{{ toYaml .Values.metrics.resources | indent 10 }}
{{- end }}
      volumes:
      - name: config
        configMap:
          name: {{ template "nats.fullname" . }}
