{{- if .Values.controller.secondaryingress.enabled }}
{{- $kubeTargetVersion := default .Capabilities.KubeVersion.GitVersion .Values.kubeTargetVersionOverride }}
{{- $serviceName := include "jenkins.fullname" . -}}
{{- $servicePort := .Values.controller.servicePort -}}
{{- 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.secondaryingress.apiVersion }}
{{- end }}
kind: Ingress
metadata:
  namespace: {{ template "jenkins.namespace" . }}
  labels:
    {{- include "jenkins.labels" . | nindent 4 }}
    {{- if .Values.controller.secondaryingress.labels -}}
    {{ toYaml .Values.controller.secondaryingress.labels | nindent 4 }}
    {{- end }}
  {{- if .Values.controller.secondaryingress.annotations }}
  annotations:
{{ tpl (toYaml .Values.controller.secondaryingress.annotations) . | indent 4 }}
  {{- end }}
  name: {{ template "jenkins.fullname" . }}-secondary
spec:
{{- if .Values.controller.secondaryingress.ingressClassName }}
  ingressClassName: {{ tpl .Values.controller.secondaryingress.ingressClassName . | quote }}
{{- end }}
  rules:
    - host: {{ tpl .Values.controller.secondaryingress.hostName . | quote }}
      http:
        paths:
        {{- range .Values.controller.secondaryingress.paths }}
          - path: {{ . | quote }}
            backend:
{{ if semverCompare ">=1.19-0" $kubeTargetVersion }}
              service:
                name: {{ $serviceName }}
                port:
                  number: {{ $servicePort }}
            pathType: {{ $.Values.controller.secondaryingress.pathType }}
{{ else }}
              serviceName: {{ $serviceName }}
              servicePort: {{ $servicePort }}
{{ end }}
        {{- end}}
{{- if .Values.controller.secondaryingress.tls }}
{{- $withTlsEntries := false }}
{{- range .Values.controller.secondaryingress.tls }}
  {{- if gt (len .) 0 }}
    {{- $withTlsEntries = true }}
  {{- end }}
{{- end }}
{{- if $withTlsEntries }}
  tls:
{{- range .Values.controller.secondaryingress.tls }}
  - hosts:
{{- range .hosts }}
      - {{ tpl . $ | quote }}
{{- end }}
{{- if .secretName }}
    secretName: {{ tpl (.secretName | toString) $ | quote }}
{{- end }}
{{- end }}
{{- end }}
{{- end -}}
{{- end }}
