apiVersion: v1
kind: Service
metadata:
{{- if .Values.controller.service.annotations }}
  annotations:
{{ toYaml .Values.controller.service.annotations | indent 4 }}
{{- end }}
  labels:
{{- if .Values.controller.service.labels }}
{{ toYaml .Values.controller.service.labels | indent 4 }}
{{- end }}
    app: {{ template "haproxy-ingress.labels.app.controller" . }}
    chart: {{ template "haproxy-ingress.chart" . }}
    component: "{{ .Values.controller.name }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  name: {{ template "haproxy-ingress.controller.fullname" . }}
  namespace: {{ .Release.Namespace }}
spec:
{{- if .Values.controller.service.clusterIP }}
  clusterIP: "{{ .Values.controller.service.clusterIP }}"
{{- end }}
{{- if .Values.controller.service.externalTrafficPolicy }}
  externalTrafficPolicy: "{{ .Values.controller.service.externalTrafficPolicy }}"
{{- end }}
{{- if .Values.controller.service.externalIPs }}
  externalIPs:
{{ toYaml .Values.controller.service.externalIPs | indent 4 }}
{{- end }}
{{- if .Values.controller.service.loadBalancerIP }}
  loadBalancerIP: "{{ .Values.controller.service.loadBalancerIP }}"
{{- end }}
{{- if .Values.controller.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.controller.service.loadBalancerSourceRanges | indent 4 }}
{{- end }}
{{- if (.Values.controller.service.healthCheckNodePort) }}
  healthCheckNodePort: {{ .Values.controller.service.healthCheckNodePort }}
{{- end }}
  ports:
  {{- if .Values.controller.enableStaticPorts }}
  {{- range .Values.controller.service.httpPorts }}
    - name: "http-{{ .port }}"
      port: {{ .port }}
      protocol: TCP
      targetPort: {{ .targetPort }}
      {{- if (not (empty .nodePort)) }}
      nodePort: {{ .nodePort }}
      {{- end }}
  {{- end }}
  {{- range .Values.controller.service.httpsPorts }}
    - name: "https-{{ .port }}"
      port: {{ .port }}
      protocol: TCP
      targetPort: {{ .targetPort }}
      {{- if (not (empty .nodePort)) }}
      nodePort: {{ .nodePort }}
      {{- end }}
  {{- end }}
  {{- end }}
  {{- range $key, $value := .Values.controller.tcp }}
    - name: "{{ tpl $key $ }}-tcp"
      port: {{ tpl $key $ }}
      protocol: TCP
      targetPort: "{{ tpl $key $ }}-tcp"
  {{- end }}
  selector:
    app: {{ template "haproxy-ingress.labels.app.controller" . }}
    component: "{{ .Values.controller.name }}"
    release: {{ .Release.Name }}
  type: "{{ .Values.controller.service.type }}"
