{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.serviceBindings.enabled }}
{{- $host := include "common.names.fullname" . }}
{{- if not .Values.sentinel.enabled }}
{{- $host = printf "%s-primary" (include "common.names.fullname" .) }}
{{- end }}
{{- $port := print .Values.primary.service.ports.valkey }}
{{- if .Values.sentinel.enabled }}
{{- $port = print .Values.sentinel.service.ports.valkey }}
{{- end }}
{{- $password := include "valkey.password" . }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}-svcbind
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: valkey
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: servicebinding.io/valkey
data:
  provider: {{ print "bitnami" | b64enc | quote }}
  type: {{ print "valkey" | b64enc | quote }}
  host: {{ print $host | b64enc | quote }}
  port: {{ print $port | b64enc | quote }}
  password: {{ print $password | b64enc | quote }}
  {{- if $password }}
  uri: {{ printf "valkey://:%s@%s:%s" $password $host $port | b64enc | quote }}
  {{- else }}
  uri: {{ printf "valkey://%s:%s" $host $port | b64enc | quote }}
  {{- end }}
{{- end }}
