{{- if .Values.server.ingress.enabled -}}
{{- $fullName := include "woodpecker-ci.fullname" . -}}
{{- $svcPort := .Values.server.service.port -}}
{{- if and .Values.server.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }}
  {{- if not (hasKey .Values.server.ingress.annotations "kubernetes.io/ingress.class") }}
  {{- $_ := set .Values.server.ingress.annotations "kubernetes.io/ingress.class" .Values.server.ingress.className}}
  {{- end }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    {{- include "woodpecker-ci.labels" . | nindent 4 }}
  {{- with .Values.server.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if and .Values.server.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }}
  ingressClassName: {{ .Values.server.ingress.className }}
  {{- end }}
  rules:
    - host: {{ .Values.server.host }}
      http:
        paths:
          {{- range .Values.server.ingress.paths }}
          - path: {{ .path }}
            {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
            pathType: {{ .pathType }}
            {{- end }}
            backend:
              {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
              service:
                name: {{ $fullName }}
                port:
                  number: {{ $svcPort }}
              {{- else }}
              serviceName: {{ $fullName }}
              servicePort: {{ $svcPort }}
              {{- end }}
          {{- end }}
  {{- if .Values.server.tls }}
  tls:
    {{- range .Values.server.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ .Values.server.host | quote }}
        {{- end }}
      secretName: {{ $fullName }}-tls
    {{- end }}
  {{- end }}
{{- end }}
