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

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