{{ if .Values.downscaler.enabled }}
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: downscaler
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: kube-downscaler
    server: 'https://kubernetes.default.svc'
  project: sys
  syncPolicy:
    managedNamespaceMetadata:
      labels:
        component: sys
    syncOptions:
      - CreateNamespace=true
      - ApplyOutOfSyncOnly=true
    {{- if .Values.downscaler.autosync }}
    automated:
      prune: true
      # selfHeal: true
    {{- end }}
  sources:
  - repoURL: '{{ .Values.cluster_config.manifests }}'
    path: {{ .Values.cluster_config.policies }}/downscaler
    targetRevision: HEAD
  - repoURL: 'https://caas-team.github.io/helm-charts/'
    targetRevision: {{ .Values.downscaler.version }}
    chart: py-kube-downscaler
    helm:
      values: |
        arguments:
        - --interval=60
        - --include-resources=deployments,statefulsets,scaledobjects,cronjobs
        # Namespaces being excluded from the 'downscale for non-work hours'
        # DEFAULT_UPTIME: "Mon-Fri 07:30-20:30 CET"
        {{- with .Values.downscaler.excludedNamespaces }}
        excludedNamespaces: {{ join "," . }}
        {{- end }}
        extraConfig: {{- toYaml .Values.downscaler.extraConfig | nindent 8 }}
{{ end }}

