{{- if .Values.ingress.enabled -}}
{{- $serviceName := include "artifactory-ha.fullname" . -}}
{{- $servicePort := .Values.artifactory.externalPort -}}
{{- $artifactoryServicePort := .Values.artifactory.externalArtifactoryPort -}}
{{- if semverCompare ">=v1.14.0" .Capabilities.KubeVersion.GitVersion }}
apiVersion: networking.k8s.io/v1beta1
{{- else }}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ template "artifactory-ha.fullname" . }}
  labels:
    app: {{ template "artifactory-ha.name" . }}
    chart: {{ template "artifactory-ha.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
{{- if .Values.ingress.labels }}
{{ .Values.ingress.labels | toYaml | trimSuffix "\n"| indent 4 -}}
{{- end}}
{{- if .Values.ingress.annotations }}
  annotations:
    force-update: "{{ randAlpha 63 | lower }}"
{{ .Values.ingress.annotations | toYaml | trimSuffix "\n" | indent 4 -}}
{{- end }}
spec:
  {{- if .Values.ingress.defaultBackend.enabled }}
  backend:
    serviceName: {{ $serviceName }}
    servicePort: {{ $servicePort }}
  {{- end }}
  rules:
{{- if .Values.ingress.hosts }}
  {{- range $host := .Values.ingress.hosts }}
  - host: {{ $host | quote }}
    http:
      paths:
        - path: {{ $.Values.ingress.routerPath }}
          backend:
            serviceName: {{ $serviceName }}
            servicePort: {{ $servicePort }}
        - path: {{ $.Values.ingress.artifactoryPath }}
          backend:
            serviceName: {{ $serviceName }}
            servicePort: {{ $artifactoryServicePort }}
  {{- end -}}
{{- end -}}
  {{- with .Values.ingress.additionalRules }}
{{ tpl . $ | indent 2 }}
  {{- end }}

  {{- if .Values.ingress.tls }}
  tls:
{{ toYaml .Values.ingress.tls | indent 4 }}
  {{- end -}}
{{- end -}}
