{{ if not .Values.existingSecret }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "rabbitmq-ha.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "rabbitmq-ha.name" . }}
    chart: {{ template "rabbitmq-ha.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
type: Opaque
data:
  {{- $password := .Values.rabbitmqPassword | default (randAlphaNum 24 | nospace) -}}
  {{- $_ := set .Values "rabbitmqPassword" $password }}
  {{- $managementPassword := .Values.managementPassword | default (randAlphaNum 24 | nospace) -}}
  {{- $_ := set .Values "managementPassword" $managementPassword }}
  rabbitmq-username: {{ .Values.rabbitmqUsername | b64enc | quote }}
  rabbitmq-password: {{ .Values.rabbitmqPassword | b64enc | quote }}
  rabbitmq-management-username: {{ .Values.managementUsername | b64enc | quote }}
  rabbitmq-management-password: {{ .Values.managementPassword | b64enc | quote }}
  rabbitmq-erlang-cookie: {{ .Values.rabbitmqErlangCookie | default (randAlphaNum 32) | b64enc | quote }}
  {{ .Values.definitionsSource }}: {{ include "rabbitmq-ha.definitions" . | b64enc | quote }}
{{ end }}
{{- if and .Values.rabbitmqCert.enabled (not .Values.rabbitmqCert.existingSecret) }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "rabbitmq-ha.fullname" . }}-cert
  labels:
    app: {{ template "rabbitmq-ha.name" . }}
    chart: {{ template "rabbitmq-ha.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
{{- if .Values.extraLabels }}
{{ toYaml .Values.extraLabels | indent 4 }}
{{- end }}
type: Opaque
data:
  cacert.pem: {{ .Values.rabbitmqCert.cacertfile | quote }}
  cert.pem: {{ .Values.rabbitmqCert.certfile | quote }}
  key.pem: {{ .Values.rabbitmqCert.keyfile | quote }}
{{- end }}
