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