{{- $app := .Values.server }}
{{- $service := $app.service }}
{{- if $app.enabled -}}
{{- $ctx := dict "helm" . "appKey" "server" }}
{{- $fullname := include "vm.plain.fullname" $ctx }}
{{- $ns := include "vm.namespace" $ctx }}
apiVersion: v1
kind: Service
metadata:
  namespace: {{ $ns }}
  {{- with $service.annotations }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- $_ := set $ctx "extraLabels" $app.service.labels }}
  labels: {{ include "vm.labels" $ctx | nindent 4 }}
  {{- $_ := unset $ctx "extraLabels" }}
  name: {{ $fullname }}
spec:
  {{- with $service.trafficDistribution }}
  trafficDistribution: {{ . }}
  {{- end }}
  {{- $type := $service.type }}
  {{- if and (not $type) (eq $app.mode "statefulSet") }}
    {{- $type = "ClusterIP" }}
  {{- end }}
  type: {{ $type }}
  {{- if eq $type "ClusterIP" }}
  {{- with $service.clusterIP }}
  clusterIP: {{ . }}
  {{- end }}
  {{- end }}
  {{- with $service.externalIPs }}
  externalIPs: {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- with $service.loadBalancerIP }}
  loadBalancerIP: {{ . }}
  {{- end }}
  {{- with $service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{ toYaml . | nindent 4 }}
  {{- end }}
  {{- with $service.healthCheckNodePort }}
  healthCheckNodePort: {{ . }}
  {{- end }}
  {{- with $service.externalTrafficPolicy }}
  externalTrafficPolicy: {{ . }}
  {{- end }}
  {{- with $service.ipFamilyPolicy }}
  ipFamilyPolicy: {{ . }}
  {{- end }}
  {{- with $service.ipFamilies }}
  ipFamilies: {{ toYaml . | nindent 4 }}
  {{- end }}
  ports:
    - name: http
      port: {{ $service.servicePort }}
      protocol: TCP
      targetPort: {{ $service.targetPort }}
      {{- with $service.nodePort }}
      nodePort: {{ . }}
      {{- end }}
    {{- with $app.extraArgs.graphiteListenAddr }}
    - name: graphite-tcp
      protocol: TCP
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: graphite-tcp
    - name: graphite-udp
      protocol: UDP
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: graphite-udp
    {{- end }}
    {{- with $app.extraArgs.influxListenAddr }}
    - name: influx-tcp
      protocol: TCP
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: influx-tcp
    - name: influx-udp
      protocol: UDP
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: influx-udp
    {{- end }}
    {{- with $app.extraArgs.opentsdbHTTPListenAddr }}
    - name: opentsdbhttp
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: opentsdbhttp
    {{- end }}
    {{- with $app.extraArgs.opentsdbListenAddr }}
    - name: opentsdb-udp
      protocol: UDP
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: opentsdb-udp
    - name: opentsdb-tcp
      protocol: TCP
      port: {{ include "vm.port.from.flag" (dict "flag" .) }}
      targetPort: opentsdb-tcp
    {{- end }}
  selector: {{ include "vm.selectorLabels" $ctx | nindent 4 }}
{{- end }}
