{{- $kubeTargetVersion := default .Capabilities.KubeVersion.GitVersion .Values.kubeTargetVersionOverride }}
{{- if .Values.controller.ingress.enabled }}
{{- if semverCompare ">=1.19-0" $kubeTargetVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" $kubeTargetVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: {{ .Values.controller.ingress.apiVersion }}
{{- end }}
kind: Ingress
metadata:
  namespace: {{ template "jenkins.namespace" . }}
  labels:
    {{- include "jenkins.labels" . | nindent 4 }}
{{- if .Values.controller.ingress.labels }}
{{ toYaml .Values.controller.ingress.labels | indent 4 }}
{{- end }}
{{- if .Values.controller.ingress.annotations }}
  annotations:
{{ tpl (toYaml .Values.controller.ingress.annotations) . | indent 4 }}
{{- end }}
  name: {{ template "jenkins.fullname" . }}
spec:
{{- if .Values.controller.ingress.ingressClassName }}
  ingressClassName: {{ tpl .Values.controller.ingress.ingressClassName . | quote }}
{{- end }}
  rules:
  - http:
      paths:
{{- if empty (.Values.controller.ingress.paths) }}
      - backend:
{{- if semverCompare ">=1.19-0" $kubeTargetVersion }}
          service:
            name: {{ template "jenkins.fullname" . }}
            port:
              number: {{ .Values.controller.servicePort }}
        pathType: {{ .Values.controller.ingress.pathType }}
{{- else }}
          serviceName: {{ template "jenkins.fullname" . }}
          servicePort: {{ .Values.controller.servicePort }}
{{- end }}
{{- if .Values.controller.ingress.path }}
        path: {{ .Values.controller.ingress.path }}
{{- end -}}
{{- else }}
{{ tpl (toYaml .Values.controller.ingress.paths | indent 6) . }}
{{- end -}}
{{- if .Values.controller.ingress.hostName }}
    host: {{ tpl .Values.controller.ingress.hostName . | quote }}
{{- end }}
{{- if .Values.controller.ingress.resourceRootUrl }}
  - http:
      paths:
      - backend:
{{- if semverCompare ">=1.19-0" $kubeTargetVersion }}
          service:
            name: {{ template "jenkins.fullname" . }}
            port:
              number: {{ .Values.controller.servicePort }}
        pathType: {{ .Values.controller.ingress.pathType }}
{{- else }}
          serviceName: {{ template "jenkins.fullname" . }}
          servicePort: {{ .Values.controller.servicePort }}
{{- end }}
    host: {{ tpl .Values.controller.ingress.resourceRootUrl . | quote }}
{{- end }}
{{- if .Values.controller.ingress.tls }}
{{- $withTlsEntries := false }}
{{- range .Values.controller.ingress.tls }}
  {{- if gt (len .) 0 }}
    {{- $withTlsEntries = true }}
  {{- end }}
{{- end }}
{{- if $withTlsEntries }}
  tls:
{{- range .Values.controller.ingress.tls }}
  - hosts:
{{- range .hosts }}
      - {{ tpl . $ | quote }}
{{- end }}
{{- if $.Values.controller.ingress.resourceRootUrl }}
      - {{ tpl $.Values.controller.ingress.resourceRootUrl $ | quote }}
{{- end }}
{{- if .secretName }}
    secretName: {{ tpl (.secretName | toString) $ | quote }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
