{{- define "deploykf-istio-gateway.gateway.server_hosts" }}
## select VirtualServices in any namespace with the deployKF hostname
- "*/{{ .Values.deployKF.gateway.hostname }}"

{{- if .Values.deployKF.argoWorkflows.enabled }}
## select VirtualService for Argo Workflows UI
- "{{ .Values.deployKF.argoWorkflows.namespace }}/argo-server.{{ .Values.deployKF.gateway.hostname }}"
{{- end }}

{{- if .Values.deployKF.minio.enabled }}
## select VirtualServices for Minio API and Console
- "{{ .Values.deployKF.minio.namespace }}/minio-api.{{ .Values.deployKF.gateway.hostname }}"
- "{{ .Values.deployKF.minio.namespace }}/minio-console.{{ .Values.deployKF.gateway.hostname }}"
{{- end }}
{{- end }}

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: {{ .Values.deployKF.gateway.name | quote }}
  labels:
    helm.sh/chart: {{ include "deploykf-istio-gateway.labels.chart" . }}
    app.kubernetes.io/name: {{ include "deploykf-istio-gateway.labels.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  selector:
    {{- toYaml .Values.deployKF.gateway.selectorLabels | nindent 4 }}
  servers:
    {{- if .Values.deployKF.gateway.tls.enabled }}
    ################################################################################
    ## HTTPS
    ################################################################################
    - hosts:
        {{- if .Values.deployKF.gateway.tls.matchSNI }}
        {{- include "deploykf-istio-gateway.gateway.server_hosts" . | indent 8 }}
        {{- else }}
        ## NOTE: hosts is set to "*" because most external load balancers do not forward the SNI after TLS termination
        ##       https://istio.io/v1.19/docs/ops/common-problems/network-issues/#configuring-sni-routing-when-not-sending-sni
        - "*"
        {{- end }}
      port:
        name: https
        number: {{ .Values.deployKF.gateway.ports.https | int }}
        protocol: HTTPS
      tls:
        mode: SIMPLE
        credentialName: deploykf-istio-gateway-cert
    {{- end }}