{{- if .Values.metricsGenerator.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "grafana-tempo.metrics-generator.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: metrics-generator
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.commonAnnotations .Values.metricsGenerator.service.annotations }}
  annotations:
    {{- if .Values.metricsGenerator.service.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.metricsGenerator.service.annotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.metricsGenerator.service.type }}
  sessionAffinity: {{ .Values.metricsGenerator.service.sessionAffinity }}
  {{- if .Values.metricsGenerator.service.clusterIP }}
  clusterIP: {{ .Values.metricsGenerator.service.clusterIP }}
  {{- end }}
  {{- if (or (eq .Values.metricsGenerator.service.type "LoadBalancer") (eq .Values.metricsGenerator.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.metricsGenerator.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{ if eq .Values.metricsGenerator.service.type "LoadBalancer" }}
  loadBalancerSourceRanges: {{ .Values.metricsGenerator.service.loadBalancerSourceRanges }}
  {{ end }}
  {{- if (and (eq .Values.metricsGenerator.service.type "LoadBalancer") (not (empty .Values.metricsGenerator.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.metricsGenerator.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.metricsGenerator.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if (and (or (eq .Values.metricsGenerator.service.type "NodePort") (eq .Values.metricsGenerator.service.type "LoadBalancer")) (not (empty .Values.metricsGenerator.service.nodePorts.http))) }}
      nodePort: {{ .Values.metricsGenerator.service.nodePorts.http }}
      {{- else if eq .Values.metricsGenerator.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: grpc
      port: {{ .Values.metricsGenerator.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      {{- if (and (or (eq .Values.metricsGenerator.service.type "NodePort") (eq .Values.metricsGenerator.service.type "LoadBalancer")) (not (empty .Values.metricsGenerator.service.nodePorts.grpc))) }}
      nodePort: {{ .Values.metricsGenerator.service.nodePorts.grpc }}
      {{- else if eq .Values.metricsGenerator.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
  selector: {{- include "common.labels.matchLabels" . | nindent 4 }}
    app.kubernetes.io/component: metrics-generator
{{- end }}
