{{- /* S3 ingress works for standalone S3 gateway (s3.enabled), S3 on Filer (filer.s3.enabled), and all-in-one mode (allInOne.s3.enabled) */}}
{{- $s3Enabled := or .Values.s3.enabled (and .Values.filer.s3.enabled (not .Values.allInOne.enabled)) (and .Values.allInOne.enabled .Values.allInOne.s3.enabled) }}
{{- if and $s3Enabled .Values.s3.ingress.enabled }}
{{- /* Determine service name based on deployment mode */}}
{{- $serviceName := ternary (include "seaweedfs.componentName" (list . "all-in-one")) (include "seaweedfs.componentName" (list . "s3")) .Values.allInOne.enabled }}
{{- $s3Port := .Values.allInOne.s3.port | default .Values.s3.port }}
{{- /* Build hosts list - support both legacy .host (string) and new .hosts (array) for backwards compatibility */}}
{{- $hosts := list }}
{{- if kindIs "slice" .Values.s3.ingress.host }}
  {{- $hosts = .Values.s3.ingress.host }}
{{- else if .Values.s3.ingress.host }}
  {{- $hosts = list .Values.s3.ingress.host }}
{{- end }}
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion }}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion }}
apiVersion: networking.k8s.io/v1beta1
{{- else }}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: ingress-{{ include "seaweedfs.fullname" . }}-s3
  namespace: {{ .Release.Namespace }}
  {{- with .Values.s3.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ template "seaweedfs.name" . }}
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: s3
spec:
  ingressClassName: {{ .Values.s3.ingress.className | quote }}
  tls:
    {{ .Values.s3.ingress.tls | default list | toYaml | nindent 6}}
  rules:
{{- if $hosts }}
{{- range $hosts }}
  - host: {{ . | quote }}
    http:
      paths:
      - path: {{ $.Values.s3.ingress.path | quote }}
        pathType: {{ $.Values.s3.ingress.pathType | quote }}
        backend:
{{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
          service:
            name: {{ $serviceName }}
            port:
              number: {{ $s3Port }}
{{- else }}
          serviceName: {{ $serviceName }}
          servicePort: {{ $s3Port }}
{{- end }}
{{- end }}
{{- else }}
  - http:
      paths:
      - path: {{ .Values.s3.ingress.path | quote }}
        pathType: {{ .Values.s3.ingress.pathType | quote }}
        backend:
{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion }}
          service:
            name: {{ $serviceName }}
            port:
              number: {{ $s3Port }}
{{- else }}
          serviceName: {{ $serviceName }}
          servicePort: {{ $s3Port }}
{{- end }}
{{- end }}
{{- end }}
