{{- $ns := include "vm.namespace" . }}
{{- range $name, $config := .Values }}
  {{- if and (kindIs "map" $config) (hasKey $config "ingress") ($config.enabled) (empty (index $.Subcharts $name)) }}
    {{- $ingresses := $config.ingress }}
    {{- $appKey := list }}
    {{- $spec := $.Values }}
    {{- if hasKey $ingresses "enabled" }}
      {{- $ingresses = dict $name $ingresses }}
    {{- else }}
      {{- $appKey = concat $appKey (list $name "spec") }}
      {{- $spec = dig $name "spec" (dict) ($spec | merge (dict)) }}
    {{- end }}
    {{- range $ingName, $ingress := $ingresses }}
      {{- if $ingress.enabled }}
        {{- if not (hasKey $spec $ingName) }}
          {{- $ingName = printf "vm%s" $ingName }}
        {{- end }}
        {{- $ctx := dict "helm" $ "appKey" (append $appKey $ingName) -}}
        {{- $fullname := include "vm.managed.fullname" $ctx }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ $fullname }}
  namespace: {{ $ns }}
  {{- $_ := set $ctx "extraLabels" $ingress.labels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- with $ingress.annotations }}
  annotations: {{ tpl (toYaml .) $ | nindent 4 }}
  {{- end }}
spec:
  {{- with $ingress.ingressClassName }}
  ingressClassName: {{ tpl (.) $ }}
  {{- end }}
  {{- with $ingress.tls }}
  tls: {{ tpl (toYaml .) $ | nindent 4 }}
  {{- end }}
  rules:
    {{- $hosts := $ingress.hosts | default (list "") }}
    {{- range $hosts }}
    - http:
        paths:
          {{- with $ingress.extraPaths }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
          - backend:
              service:
                name: {{ $fullname }}
                port:
                  name: {{ $ingress.portName | default "http" }}
            {{- with ($ingress.path | default (dig "spec" "extraArgs" "http.pathPrefix" "/" $config)) }}
            path: {{ tpl . $ }}
            {{- end }}
            {{- with $ingress.pathType }}
            pathType: {{ . }}
            {{- end }}
      {{- with . }}
      host: {{ tpl . $ | quote }}
      {{- end }}
    {{- end }}

      {{- end }}
    {{- end }}
  {{- end }}
{{- end }}
