{{- if .Values.gatewayApi.enabled }}
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: {{ include "rustfs.fullname" . }}-route
  namespace: {{ .Release.Namespace }}
  annotations:
    {{- if eq .Values.gatewayApi.gatewayClass "contour" }}
    projectcontour.io/upstream-hash-policy: "cookie"
    {{- end }}
spec:
  parentRefs:
    {{- if .Values.gatewayApi.existingGateway.name }}
    - name: {{ .Values.gatewayApi.existingGateway.name }}
      {{- if .Values.gatewayApi.existingGateway.namespace }}
      namespace: {{ .Values.gatewayApi.existingGateway.namespace }}
      {{- end }}
      sectionName: {{ .Values.gatewayApi.listeners.https.name | default "websecure"}}
    {{- else }}
    - name: {{ include "rustfs.fullname" $ }}-gateway
      sectionName: {{ .Values.gatewayApi.listeners.https.name | default "websecure"}}
    {{- end }}
  hostnames:
    - {{ .Values.gatewayApi.hostname }}
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        {{- if eq .Values.gatewayApi.gatewayClass "traefik" }}
        - name: {{ include "rustfs.fullname" . }}-sticky-svc
          port: {{ .Values.service.console.port }}
          kind: TraefikService
          group: traefik.io
        {{- else }}
        - name: {{ include "rustfs.fullname" $ }}-svc
          port: {{ .Values.service.console.port }}
        {{- end }}

---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: {{ include "rustfs.fullname" . }}-redirect
  namespace: {{ .Release.Namespace }}
spec:
  parentRefs:
    {{- if .Values.gatewayApi.existingGateway.name }}
    - name: {{ .Values.gatewayApi.existingGateway.name }}
      {{- if .Values.gatewayApi.existingGateway.namespace }}
      namespace: {{ .Values.gatewayApi.existingGateway.namespace }}
      {{- end }}
      sectionName: {{ .Values.gatewayApi.listeners.http.name | default "web" }}
    {{- else }}
    - name: {{ include "rustfs.fullname" $ }}-gateway
      sectionName: {{ .Values.gatewayApi.listeners.http.name | default "web" }}
    {{- end }}
  rules:
    - filters:
        - type: RequestRedirect
          requestRedirect:
            scheme: https
            statusCode: 301
{{- end }}
