{{- include "nplus.initOnly" $ -}}
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: {{ .Release.Name }}
  namespace: {{ (.this.argocd).namespace }}
  labels:
    # Common Kubernetes Lables are not allowed here for ArgoCD Application Manifests,
    # as ArgoCD then tries to immediately delete the synced Application
    # so we just use minimal Labels, which are the selectorLabels
    {{- include "nplus.selectorLabels" . | nindent 4 }}
  finalizers:
    # The default behaviour is foreground cascading deletion
    # - resources-finalizer.argocd.argoproj.io
    # Alternatively, you can use background cascading deletion
    - resources-finalizer.argocd.argoproj.io/background
spec:
  {{- if or (.this.argocd).selfHeal (.this.argocd).prune }}
  syncPolicy:
    automated:
      {{- if (.this.argocd).selfHeal }}
      selfHeal: {{ (.this.argocd).selfHeal }}
      {{- end }}
      {{- if (.this.argocd).prune }}
      prune: {{ (.this.argocd).prune }}
      {{- end }}
  {{- end }}{{/* if or (.this.argocd).selfHeal (.this.argocd).prune */}}
  {{- if (.this.argocd).project }}
  project: {{ (.this.argocd).project }}
  {{- end }}
  source:
    chart: {{ required "you must set the name of the chart to use" (.this.argocd).chart }}
    targetRevision: "{{ .Chart.Version }}"
    repoURL: {{ required "you must set the repo url to your charts repo" (.this.argocd).repo | quote }}
    helm:
      releaseName: {{ .Release.Name }}
      values: | 
        {{- toYaml (omit .Values "globals" "argocd") | nindent 8 }}
  destination:
    server: {{ tpl (required "you must set the name of the destination server" ((.this.argocd).destinationServer | default (.this.argocd).server)) . | quote }}
    namespace: {{ tpl (required "you must set the name of the destination namespace" (.this.argocd).destinationNamespace) . | quote }}