{{- $ingress := .Values.ingress -}}
{{- if $ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ template "rocketchat.fullname" . }}
{{- with $ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
  labels:
    app.kubernetes.io/name: {{ include "rocketchat.name" . }}
    helm.sh/chart: {{ include "rocketchat.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
{{- if .Values.ingress.ingressClassName }}
  ingressClassName: {{ .Values.ingress.ingressClassName }}
{{- end }}
{{- if $ingress.tls }}
  tls:
  {{- range $ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . }}
      {{- end }}
      {{- if .secretName }}
      secretName: {{ .secretName }}
      {{- end }}
  {{- end }}
{{- end }}
  rules:
  - 
  {{- if .Values.host }}
    host: {{ .Values.host }}
  {{- end }}
    http:
      paths:
      - path: {{ $ingress.path }}
        pathType: {{ if $ingress.pathType }} {{ $ingress.pathType }} {{ else }} {{ print "Prefix" }} {{ end }}
        backend:
          service:
            name: {{ template "rocketchat.fullname" . }}
            port:
              name: http
      {{- if .Values.microservices.enabled }}
      - path: /sockjs
        pathType: Prefix
        backend:
          service:
            name: {{ .Release.Name }}-ddp-streamer
            port:
              name: http
      - path: /websocket
        pathType: Prefix
        backend:
          service:
            name: {{ .Release.Name }}-ddp-streamer
            port:
              name: http
      {{- end }}
      {{- if .Values.federation.enabled }}
      {{- if .Values.ingress.federation.serveWellKnown }}
      - path: /.well-known/matrix/server
        pathType: Prefix
        backend:
          service:
            name: {{ template "rocketchat.fullname" . }}-wellknown
            port:
              name: http
      - path: /.well-known/matrix/client
        pathType: Prefix
        backend:
          service:
            name: {{ template "rocketchat.fullname" . }}-wellknown
            port:
              name: http
      {{- end }}
  - host: {{ .Values.federation.host }}
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: {{ template "rocketchat.fullname" . }}-synapse
              port:
                name: http
    {{end}}
{{- end }}
