{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.filer.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "seaweedfs.filer.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: filer
  {{- if or .Values.filer.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.filer.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.filer.service.type }}
  {{- if and .Values.filer.service.clusterIP (eq .Values.filer.service.type "ClusterIP") }}
  clusterIP: {{ .Values.filer.service.clusterIP }}
  {{- end }}
  {{- if .Values.filer.service.sessionAffinity }}
  sessionAffinity: {{ .Values.filer.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.filer.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.filer.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.filer.service.type "LoadBalancer") (eq .Values.filer.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.filer.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.filer.service.type "LoadBalancer") (not (empty .Values.filer.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.filer.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.filer.service.type "LoadBalancer") (not (empty .Values.filer.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.filer.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.filer.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if and (or (eq .Values.filer.service.type "NodePort") (eq .Values.filer.service.type "LoadBalancer")) (not (empty .Values.filer.service.nodePorts.http)) }}
      nodePort: {{ .Values.filer.service.nodePorts.http }}
      {{- else if eq .Values.filer.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: grpc
      port: {{ .Values.filer.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      {{- if and (or (eq .Values.filer.service.type "NodePort") (eq .Values.filer.service.type "LoadBalancer")) (not (empty .Values.filer.service.nodePorts.grpc)) }}
      nodePort: {{ .Values.filer.service.nodePorts.grpc }}
      {{- else if eq .Values.filer.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.filer.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.filer.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.filer.podLabels .Values.commonLabels) "context" .) | fromYaml }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: filer
{{- end }}
