#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

{{- if .Values.components.pulsar_manager }}
apiVersion: v1
kind: Secret
metadata:
  name: "{{ template "pulsar.fullname" . }}-{{ .Values.pulsar_manager.component }}-secret"
  namespace: {{ template "pulsar.namespace" . }}
  labels:
    app: {{ template "pulsar.name" . }}
    chart: {{ template "pulsar.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: {{ .Values.pulsar_manager.component }}
    cluster: {{ template "pulsar.fullname" . }}
    "helm.sh/resource-policy": "keep"  # do not remove when uninstalling to keep it for next install
type: Opaque
data:
  {{/*  https://itnext.io/manage-auto-generated-secrets-in-your-helm-charts-5aee48ba6918 */}}
  {{- $namespace := include "pulsar.namespace" . -}}
  {{- $fullname := include "pulsar.fullname" . -}}
  {{- $secretName := printf "%s-%s-secret" $fullname .Values.pulsar_manager.component -}}
  {{- $secretObj := lookup "v1" "Secret" $namespace $secretName | default dict }}
  {{- $secretData := (get $secretObj "data") | default dict }}

  {{- $ui_user := ((get $secretData "UI_USERNAME") | b64dec) | default (.Values.pulsar_manager.admin.ui_username) | default ("pulsar") | b64enc }}
  {{- $ui_password := ((get $secretData "UI_PASSWORD") | b64dec) | default (.Values.pulsar_manager.admin.ui_password) | default (randAlphaNum 32) | b64enc }}
  UI_USERNAME: {{ $ui_user | quote }}
  UI_PASSWORD: {{ $ui_password | quote }}

  {{- $db_user := ((get $secretData "DB_USERNAME") | b64dec) | default (.Values.pulsar_manager.admin.db_username) | default ("pulsar") | b64enc }}
  {{- $db_password := ((get $secretData "DB_PASSWORD") | b64dec) | default (.Values.pulsar_manager.admin.db_password) | default (randAlphaNum 32) | b64enc }}
  DB_USERNAME: {{ $db_user | quote }}
  DB_PASSWORD: {{ $db_password | quote }}

{{- end }}



