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