{{- if .Values.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "logstash.fullname" . }}-metrics
  labels: {{ include "logstash.labels" . | nindent 4 }}
  {{- if .Values.metrics.service.annotations }}
  annotations: {{- include "logstash.tplValue" (dict "value" .Values.metrics.service.annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.metrics.service.type }}
  {{- if and .Values.metrics.service.loadBalancerIP (eq .Values.metrics.service.type "LoadBalancer") }}
  loadBalancerIP: {{ .Values.metrics.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.metrics.service.type "LoadBalancer") .Values.metrics.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml .Values.metrics.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if and (eq .Values.metrics.service.type "ClusterIP") .Values.metrics.service.clusterIP }}
  clusterIP: {{ .Values.metrics.service.clusterIP }}
  {{- end }}
  ports:
    - port: {{ .Values.metrics.service.port }}
      targetPort: metrics
      protocol: TCP
      name: metrics
      {{- if (and (or (eq .Values.metrics.service.type "NodePort") (eq .Values.metrics.service.type "LoadBalancer")) (not (empty .Values.metrics.service.nodePort)))}}
      nodePort: {{ .Values.metrics.service.nodePort }}
      {{- else if eq .Values.metrics.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
  selector: {{ include "logstash.matchLabels" . | nindent 4 }}
{{- end }}
