{{ if and .Values.ingress.enabled (not .Values.istio.enabled) -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Release.Name }}-ingress
  labels:
    app.kubernetes.io/component: gerrit-replica
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit-replica.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/proxy-body-size: {{ .Values.ingress.maxBodySize | default "50m" }}
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/configuration-snippet: |-
      if ($args ~ service=git-receive-pack){
        set $proxy_upstream_name "{{ .Release.Namespace }}-{{ .Release.Name }}-git-backend-service-http";
        set $proxy_host $proxy_upstream_name;
        set $service_name "{{ .Release.Name }}-git-backend-service";
      }
    {{- if .Values.ingress.additionalAnnotations }}
{{ toYaml .Values.ingress.additionalAnnotations  | indent 4 }}
    {{- end }}
spec:
  {{ if .Values.ingress.tls.enabled -}}
  tls:
  - hosts:
    - {{ .Values.ingress.host }}
    {{ if .Values.ingress.tls.secret.create -}}
    secretName: {{ .Release.Name }}-tls-secret
    {{- else }}
    secretName: {{ .Values.ingress.tls.secret.name }}
    {{- end }}
  {{- end }}
  rules:
  - host: {{required "A host URL is required for the ingress. Please set 'ingress.host'" .Values.ingress.host }}
    http:
      paths:
      - pathType: Prefix
        path: /a/projects
        backend:
          service:
            name: {{ .Release.Name }}-git-backend-service
            port:
              number: {{ .Values.gitBackend.service.http.port }}
      - pathType: Prefix
        path: "/.*/git-receive-pack"
        backend:
          service:
            name: {{ .Release.Name }}-git-backend-service
            port:
              number: {{ .Values.gitBackend.service.http.port }}
      - pathType: Prefix
        path: /
        backend:
          service:
            name: {{ .Release.Name }}-gerrit-replica-service
            port:
              number: {{ .Values.gerritReplica.service.http.port }}
{{- end }}
---
{{ if and (and .Values.ingress.tls.enabled .Values.ingress.tls.secret.create) (not .Values.istio.enabled) -}}
apiVersion: v1
kind: Secret
metadata:
  name:  {{ .Release.Name }}-tls-secret
  labels:
    app.kubernetes.io/component: gerrit-replica
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit-replica.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
type: kubernetes.io/tls
data:
  {{ with .Values.ingress.tls -}}
  tls.crt: {{ .cert | b64enc }}
  tls.key: {{ .key | b64enc }}
  {{- end }}
{{- end }}
