{{ if .Values.istio.enabled -}}
{{ if and .Values.istio.tls.enabled .Values.istio.tls.secret.create }}
apiVersion: v1
kind: Secret
metadata:
  name:  {{ .Release.Name }}-istio-tls-secret
  namespace: istio-system
  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.istio.tls -}}
  tls.crt: {{ .cert | b64enc }}
  tls.key: {{ .key | b64enc }}
  {{- end }}
{{- end }}
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: {{ .Release.Name }}-istio-gateway
  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 }}
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - {{ .Values.istio.host }}
  {{ if .Values.istio.tls.enabled }}
    tls:
      httpsRedirect: true
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - {{ .Values.istio.host }}
    tls:
      mode: SIMPLE
      {{ if .Values.istio.tls.secret.create }}
      credentialName: {{ .Release.Name }}-istio-tls-secret
      {{- else  }}
      credentialName: {{ .Values.istio.tls.secret.name }}
      {{- end }}
  {{- end }}
  {{ if .Values.istio.ssh.enabled }}
  - port:
      number: 29418
      name: ssh
      protocol: TCP
    hosts:
    - {{ .Values.istio.host }}
  {{- end }}
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: {{ .Release.Name }}-istio-virtual-service
  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 }}
spec:
  hosts:
  - {{ .Values.istio.host }}
  gateways:
  - {{ .Release.Name }}-istio-gateway
  http:
  - name: apache-git-http-backend
    match:
    - uri:
        prefix: "/a/projects/"
    - uri:
        regex: "/.*/git-receive-pack"
    - uri:
        regex: "/.*/info/refs"
      queryParams:
        service:
          exact: git-receive-pack
    route:
    - destination:
        host: {{ .Release.Name }}-git-backend-service.{{ .Release.Namespace }}.svc.cluster.local
        port:
          number: 80
  - name: gerrit-replica
    route:
    - destination:
        host: {{ .Release.Name }}-gerrit-replica-service.{{ .Release.Namespace }}.svc.cluster.local
        port:
          number: 80
  {{ if .Values.istio.ssh.enabled }}
  tcp:
  - match:
    - port: {{ .Values.gerritReplica.service.ssh.port }}
    route:
    - destination:
        host: {{ .Release.Name }}-gerrit-replica-service.{{ .Release.Namespace }}.svc.cluster.local
        port:
          number: {{ .Values.gerritReplica.service.ssh.port }}
  {{- end }}
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: {{ .Release.Name }}-gerrit-destination-rule
  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 }}
spec:
  host: {{ .Release.Name }}-gerrit-replica-service.{{ .Release.Namespace }}.svc.cluster.local
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN
{{- end }}
