# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: {{ .Release.Name }}-static-routes
  namespace: {{ .Release.Namespace }}
spec:
  parentRefs:
    - name: {{ .Release.Name }}-gateway
  {{- if and .Values.gatewayConfig .Values.gatewayConfig.hostname }}
  hostnames:
    - {{ .Values.gatewayConfig.hostname }}
  {{- end }}
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/computing-unit
      backendRefs:
        - name: workflow-computing-unit-manager-svc
          port: 8888
    - matches:
        - path:
            type: PathPrefix
            value: /api/compile
      backendRefs:
        - name: workflow-compiling-service-svc
          port: 9090
    - matches:
        - path:
            type: PathPrefix
            value: /api/dataset
        - path:
            type: PathPrefix
            value: /api/access/dataset
      backendRefs:
        - name: file-service-svc
          port: 9092
    - matches:
        - path:
            type: PathPrefix
            value: /api/access/computing-unit
      backendRefs:
        - name: workflow-computing-unit-manager-svc
          port: 8888
    - matches:
        - path:
            type: PathPrefix
            value: /api/config
      backendRefs:
        - name: config-service-svc
          port: 9094
    - matches:
        - path:
            type: PathPrefix
            value: /rtc
      backendRefs:
        - name: y-websocket-server-svc
          port: 1234
    - matches:
        - path:
            type: PathPrefix
            value: /python-language-server
      backendRefs:
        - name: python-language-server-svc
          port: 3000
    - matches:
        - path:
            type: PathPrefix
            value: /api
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: webserver-svc
          port: 8080
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: {{ .Release.Name }}-dynamic-routes
  namespace: {{ .Release.Namespace }}
spec:
  parentRefs:
    - name: {{ .Release.Name }}-gateway
  {{- if and .Values.gatewayConfig .Values.gatewayConfig.hostname }}
  hostnames:
    - {{ .Values.gatewayConfig.hostname }}
  {{- end }}
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /wsapi
        - path:
            type: RegularExpression
            value: "^/api/executions/\\d+/stats/\\d+$"
        - path:
            type: PathPrefix
            value: /api/executions/result/export
      backendRefs:
        - group: gateway.envoyproxy.io
          kind: Backend
          name: texera-dynamic-backend
---
# MinIO Route
{{- if .Values.minio.gateway.enabled }}
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: texera-minio-route
  namespace: {{ .Release.Namespace }}
spec:
  parentRefs:
    - name: {{ .Release.Name }}-gateway
  hostnames:
    - {{ .Values.minio.gateway.hostname }}
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: {{ .Release.Name }}-minio
          port: 9000
{{- end }}
