apiVersion: v1
kind: Service
metadata:
  name: {{ template "traefik.fullname" . }}
  labels:
    app: {{ template "traefik.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
  {{- if .Values.service }}
  {{- range $key, $value := .Values.service.labels }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
  {{- end }}
  annotations:
  {{- if .Values.service }}
  {{- range $key, $value := .Values.service.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
  {{- end }}
spec:
  type: {{ .Values.serviceType }}
  {{- if .Values.loadBalancerIP }}
  loadBalancerIP: {{ .Values.loadBalancerIP }}
  {{- end }}
  {{- if .Values.externalIP }}
  externalIPs:
    - {{ .Values.externalIP }}
  {{- end }}
  {{- if .Values.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
    {{- range $cidr := .Values.loadBalancerSourceRanges }}
    - {{ $cidr }}
    {{- end }}
  {{- end }}
  {{- if .Values.externalTrafficPolicy }}
  externalTrafficPolicy: {{ .Values.externalTrafficPolicy }}
  {{- end }}
  selector:
    app: {{ template "traefik.name" . }}
    release: {{ .Release.Name }}
  ports:
  - port: 80
    name: http
    {{- if (and (eq .Values.serviceType "NodePort") (not (empty .Values.service.nodePorts.http)))}}
    nodePort: {{ .Values.service.nodePorts.http }}
    {{- end }}
    targetPort: http
  - port: 443
    name: https
    {{- if (and (eq .Values.serviceType "NodePort") (not (empty .Values.service.nodePorts.https)))}}
    nodePort: {{ .Values.service.nodePorts.https }}
    {{- end }}
    {{- if not .Values.ssl.enabled }}
    targetPort: httpn
    {{- end }}
  {{- if (and (.Values.metrics.prometheus.enabled) (not (.Values.metrics.prometheus.restrictAccess)))}}
  - port: 8080
    name: metrics
    targetPort: dash
  {{- end }}
