{{- $values := .Values -}}
{{- $namespace := $.Release.Namespace -}}
{{- if $.Values.ingressClass.enabled -}}
  {{- $namespace := (printf "%v-%v" $namespace .Release.Name) -}}
{{- end -}}

{{- range $index, $middlewareData := .Values.middlewares.chain }}
---
# Declaring the user list
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: {{ ternary (printf "%v-%v" $.Release.Name $middlewareData.name) $middlewareData.name $.Values.ingressClass.enabled }}
  namespace: {{ $.Release.Namespace }}
spec:
  chain:
    middlewares:
      {{- range $index, $middleware := .middlewares }}
      - name: {{ printf "%v-%v@%v" $namespace $middleware "kubernetescrd" }}
      {{- end }}
{{- end -}}
