# Addon provider
{{- if .Values.addon }}
{{- $addons := split ";" .Values.addon }}
{{- $addonNamespace := "" }}
{{- $addonName := "" }}
{{- $addonVersion := "" }}
{{- range $addon := $addons }}
{{- $addonArgs := split ":" $addon }}
{{- $addonArgsLen :=  len $addonArgs }}
{{-  if eq $addonArgsLen 3 }}
  {{- $addonNamespace = $addonArgs._0 }}
  {{- $addonName = $addonArgs._1 }}
  {{- $addonVersion = $addonArgs._2 }}
{{-  else if eq $addonArgsLen 2 }}
  {{- $addonNamespace = print $addonArgs._0 "-addon-system" }}
  {{- $addonName = $addonArgs._0 }}
  {{- $addonVersion = $addonArgs._1 }}
{{-  else if eq $addonArgsLen 1 }}
  {{- $addonNamespace = print $addonArgs._0 "-addon-system" }}
  {{- $addonName = $addonArgs._0 }}
{{- else }}
  {{- fail "addon provider argument should have the following format helm:v1.0.0 or mynamespace:helm:v1.0.0" }}
{{- end }}
---
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    "helm.sh/hook": "post-install,post-upgrade"
    "helm.sh/hook-weight": "1"
    "argocd.argoproj.io/sync-wave": "1"
  name: {{ $addonNamespace }}
---
apiVersion: operator.cluster.x-k8s.io/v1alpha2
kind: AddonProvider
metadata:
  name: {{ $addonName }}
  namespace: {{ $addonNamespace }}
  annotations:
    "helm.sh/hook": "post-install,post-upgrade"
    "helm.sh/hook-weight": "2"
    "argocd.argoproj.io/sync-wave": "2"
{{- if or $addonVersion $.Values.secretName }}
spec:
{{- end}}
{{- if $addonVersion }}
  version: {{ $addonVersion }}
{{- end }}
{{- if $.Values.secretName }}
  secretName: {{ $.Values.secretName }}
{{- end }}
{{- if $.Values.secretNamespace }}
  secretNamespace: {{ $.Values.secretNamespace }}
{{- end }}
{{- end }}
{{- end }}
