{{- define "cozystack.defaultIngressValues" -}}
ingress-nginx:
  fullnameOverride: ingress-nginx
  controller:
    kind: DaemonSet
    {{- if eq .Values.addons.ingressNginx.exposeMethod "Proxied" }}
    hostNetwork: true
    service:
      enabled: false
    {{- end }}
    {{- if not .Values.addons.certManager.enabled }}
    admissionWebhooks:
      certManager:
        enabled: false
    {{- end }}
  nodeSelector:
    node-role.kubernetes.io/ingress-nginx: ""
{{- end }}

{{- if .Values.addons.ingressNginx.enabled }}
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: {{ .Release.Name }}-ingress-nginx
  labels:
    cozystack.io/repository: system
    cozystack.io/target-cluster-name: {{ .Release.Name }}
spec:
  interval: 5m
  releaseName: ingress-nginx
  chart:
    spec:
      chart: cozy-ingress-nginx
      reconcileStrategy: Revision
      sourceRef:
        kind: HelmRepository
        name: cozystack-system
        namespace: cozy-system
      version: '>= 0.0.0-0'
  kubeConfig:
    secretRef:
      name: {{ .Release.Name }}-admin-kubeconfig
      key: super-admin.svc
  targetNamespace: cozy-ingress-nginx
  storageNamespace: cozy-ingress-nginx
  install:
    createNamespace: true
    remediation:
      retries: -1
  upgrade:
    remediation:
      retries: -1
  values:
    {{- toYaml (deepCopy .Values.addons.ingressNginx.valuesOverride | mergeOverwrite (fromYaml (include "cozystack.defaultIngressValues" .))) | nindent 4 }}
  dependsOn:
  {{- if lookup "helm.toolkit.fluxcd.io/v2" "HelmRelease" .Release.Namespace .Release.Name }}
  - name: {{ .Release.Name }}
    namespace: {{ .Release.Namespace }}
  {{- end }}
  - name: {{ .Release.Name }}-cilium
    namespace: {{ .Release.Namespace }}
{{- end }}
