apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "rabbitmq.fullname" . }}
  labels:
    {{- include "rabbitmq.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "rabbitmq.selectorLabels" . | nindent 6 }}
  serviceName: {{ include "rabbitmq.fullname" . }}-internal
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  replicas: {{ .Values.replicaCount }}
{{- if .Values.revisionHistoryLimit }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
{{- end }}
  updateStrategy:
    type: {{ .Values.updateStrategyType }}
  template:
    metadata:
      annotations:
        checksum/rabbit-config: {{ include (print $.Template.BasePath "/rabbit-config.yaml") . | sha256sum }}
        checksum/plugins-config: {{ include (print $.Template.BasePath "/plugins-config.yaml") . | sha256sum }}
        checksum/scripts: {{ include (print $.Template.BasePath "/scripts.yaml") . | sha256sum }}
        checksum/certs: {{ include (print $.Template.BasePath "/certs.yaml") . | sha256sum }}
    {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "rabbitmq.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "rabbitmq.serviceAccountName" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      initContainers:
        - name: {{ .Chart.Name }}-init
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.initImage.registry }}/{{ .Values.initImage.repository }}:{{ .Values.initImage.tag }}"
          imagePullPolicy: {{ .Values.initImage.pullPolicy }}
          env:
            {{- if .Values.authentication.erlangCookie }}
            - name: ERLANG_COOKIE
              value: {{ .Values.authentication.erlangCookie }}
            {{- end }}
          volumeMounts:
            - name: config
              mountPath: /etc/rabbitmq
            {{- if .Values.extraSecretConfigs }}
            - mountPath: /extraconfigs
              name: extraconfigs-volume
            {{- end }}
            {{- if .Values.extraSecretAdvancedConfigs }}
            - mountPath: /extraadvancedconfigs
              name: extraadvancedconfigs-volume
            {{- end }}
            - name: rabbitconfig
              mountPath: /temp/rabbitmq
            - name: rabbitmq-volume
              mountPath: /var/lib/rabbitmq
            - name: scripts
              mountPath: /scripts
            - name: plugins
              mountPath: /temp/plugins
          command: [ "/scripts/init.sh" ]
      {{- with .Values.extraInitContainers }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          {{- with  .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: amqp
              containerPort: {{ .Values.options.tcp.port }}
            {{- if .Values.options.ssl.enabled }}
            - name: amqps
              containerPort: {{ .Values.options.ssl.port }}
            {{- end }}
            {{- if .Values.managementPlugin.enabled }}
            - name: mgmt
              containerPort: {{ .Values.managementPlugin.tcp.port }}
            {{- end }}
            {{- if .Values.prometheusPlugin.enabled }}
            - name: prometheus
              containerPort: {{ .Values.prometheusPlugin.tcp.port }}
            {{- end }}
            - name: epmd
              containerPort: 4369
            - name: dist
              containerPort: 25672
            {{- range $service := .Values.extraServices }}
            - name: {{ $service.name }}
              containerPort: {{ $service.containerPort}}
              protocol: {{ $service.protocol }}
            {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe:
            {{- toYaml .Values.customStartupProbe | nindent 12 }}
          {{- else }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - rabbitmq-diagnostics -q check_running
          {{- with .Values.startupProbe }}
            initialDelaySeconds: {{ .initialDelaySeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            failureThreshold: {{ .failureThreshold }}
            successThreshold: {{ .successThreshold }}
            periodSeconds: {{ .periodSeconds }}                
          {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe:
            {{- toYaml .Values.customLivenessProbe | nindent 12 }}
          {{- else }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - rabbitmq-diagnostics -q check_running
          {{- with .Values.livenessProbe }}
            initialDelaySeconds: {{ .initialDelaySeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            periodSeconds: {{ .periodSeconds }}
            failureThreshold: {{ .failureThreshold }}
            successThreshold: {{ .successThreshold }}
          {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe:
            {{- toYaml .Values.customReadinessProbe | nindent 12 }}
          {{- else }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            exec:
              command:
                - /bin/bash
                - -ec
                - rabbitmq-diagnostics -q check_running
          {{- with .Values.readinessProbe }}
            initialDelaySeconds: {{ .initialDelaySeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            periodSeconds: {{ .periodSeconds }}
            failureThreshold: {{ .failureThreshold }}
            successThreshold: {{ .successThreshold }}
          {{- end }}
          {{- end }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          lifecycle:
          {{- if gt (.Values.replicaCount | int) 1 }}
            postStart:
              exec:
                command:
                  - /bin/bash
                  - -ec
                  - /scripts/startup.sh
          {{- end }}
            preStop:
              exec:
                command:
                  - bash
                  - -ec
                  - /scripts/shutdown.sh
          env:
            - name: RABBITMQ_USE_LONGNAME
              value: {{ .Values.clustering.useLongName | quote }}
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- if .Values.clustering.useLongName }}
            - name: RABBITMQ_NODENAME
              value: rabbit@$(NODE_NAME).{{ include "rabbitmq.fullname" . }}-internal.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}
            {{- else }}
            - name: RABBITMQ_NODENAME
              value: rabbit@$(NODE_NAME)
            {{- end }}
          {{- with .Values.env }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
          envFrom:
          {{- range .Values.extraEnvSecrets }}
            - secretRef:
                name: {{ . }}
          {{- end }}          
          {{- if .Values.args }}
          args:
            {{- range .Values.args }}
            - {{ . }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: logs
              mountPath: /var/log/rabbitmq
            - name: tmp
              mountPath: /tmp
            - name: rabbitmq-volume
              mountPath: /var/lib/rabbitmq
            - name: config
              mountPath: /etc/rabbitmq
            - name: scripts
              mountPath: /scripts
            {{- if .Values.certificates.enabled }}
            - name: certs
              mountPath: /ssl
            {{- end }}
            {{- range $secret := .Values.extraSecrets }}
            - name: {{ $secret.name }}
              mountPath: {{ $secret.mountPath }}
            {{- end }}
      {{- with .Values.extraContainers }}
      {{- toYaml . | nindent 8 }}
      {{- end }} 
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
        - name: logs
          emptyDir: {}
        - name: config
          emptyDir: {}
        - name: rabbitconfig
          configMap:
            name: {{ include "rabbitmq.fullname" . }}-config
        {{- if .Values.extraSecretConfigs }}
        - name: extraconfigs-volume
          secret:
            secretName: {{ .Values.extraSecretConfigs }}
        {{- end }}
        {{- if .Values.extraSecretAdvancedConfigs }}
        - name: extraadvancedconfigs-volume
          secret:
            secretName: {{ .Values.extraSecretAdvancedConfigs }}
        {{- end }}
        - name: scripts
          configMap:
            name: {{ include "rabbitmq.fullname" . }}-scripts
            defaultMode: 0555
        - name: plugins
          configMap:
            name: {{ include "rabbitmq.fullname" . }}-plugins
        {{- if .Values.certificates.enabled }}
        - name: certs
          secret:
            secretName: {{ include "rabbitmq.fullname" . }}-certs
            defaultMode: 0440
        {{- end }}
        {{- range $secret := .Values.extraSecrets }}
        - name: {{ $secret.name }}
          secret:
            secretName: {{ $secret.name }}
            defaultMode: 0440
        {{- end }}
  {{- if .Values.storage.persistentVolumeClaimName }}
        - name: rabbitmq-volume
          persistentVolumeClaim:
            claimName: {{ .Values.storage.persistentVolumeClaimName }}
  {{- else }}
  {{- if not .Values.storage.requestedSize }}
        - name: rabbitmq-volume
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - metadata:
        name: rabbitmq-volume
      spec:
        {{- with .Values.storage.accessModes }}
        accessModes:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        {{- if .Values.storage.className }}
        storageClassName: {{ .Values.storage.className }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.storage.requestedSize }}
  {{- end }}
  {{- end }}
