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

{{- if .Values.packaging.helm.enabled }}
{{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.apprepository.image "chart" .Chart ) ) }}
{{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
{{- range .Values.apprepository.initialRepos }}
{{- if or .caCert .authorizationHeader .basicAuth }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "apprepo-%s-secrets" .name }}
  namespace: {{ default (include "kubeapps.helmGlobalPackagingNamespace" $) .namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
  {{- if $.Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  {{- if .caCert }}
  ca.crt: |-
    {{ .caCert | b64enc }}
  {{- end }}
  {{- $authorizationHeader := "" }}
  {{- if .authorizationHeader }}
    {{- $authorizationHeader = .authorizationHeader }}
  {{- else if .basicAuth }}
    {{- $authorizationHeader = printf "Basic %s" (printf "%s:%s" .basicAuth.user .basicAuth.password | b64enc) }}
  {{- end }}
  {{- if $authorizationHeader }}
  authorizationHeader: |-
    {{ $authorizationHeader | b64enc }}
  {{- end }}
---
{{/* credentials are required in the release namespace for syncer jobs */}}
{{- if or .namespace $.Values.apprepository.globalReposNamespaceSuffix $.Values.kubeappsapis.pluginConfig.helm.packages.v1alpha1.globalPackagingNamespace}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-apprepo-%s" (default (include "kubeapps.helmGlobalPackagingNamespace" $) .namespace) .name }}
  namespace: {{ $.Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
  {{- if $.Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  {{- if .caCert }}
  ca.crt: |-
    {{ .caCert | b64enc }}
  {{- end }}
  {{- if $authorizationHeader }}
  authorizationHeader: |-
    {{ $authorizationHeader | b64enc }}
  {{- end }}
---
{{- end }}
{{- end }}
{{- end }}
{{- end }}
