{{- if .Values.controller.agentListenerEnabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "jenkins.fullname" . }}-agent
  namespace: {{ template "jenkins.namespace" . }}
  labels:
    {{- include "jenkins.labels" . | nindent 4 }}
  {{- if .Values.controller.agentListenerServiceAnnotations }}
  annotations:
    {{- toYaml .Values.controller.agentListenerServiceAnnotations | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.controller.agentListenerExternalTrafficPolicy }}
  externalTrafficPolicy: {{.Values.controller.agentListenerExternalTrafficPolicy}}
  {{- end }}
  ports:
    - port: {{ .Values.controller.agentListenerPort }}
      targetPort: {{ .Values.controller.agentListenerPort }}
      {{- if (and (eq .Values.controller.agentListenerServiceType "NodePort") (not (empty .Values.controller.agentListenerNodePort))) }}
      nodePort: {{ .Values.controller.agentListenerNodePort }}
      {{- end }}
      name: agent-listener
  selector:
    "app.kubernetes.io/component": "{{ .Values.controller.componentName }}"
    "app.kubernetes.io/instance": "{{ .Release.Name }}"
  type: {{ .Values.controller.agentListenerServiceType }}
  {{if eq .Values.controller.agentListenerServiceType "LoadBalancer"}}
{{- if .Values.controller.agentListenerLoadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.controller.agentListenerLoadBalancerSourceRanges | indent 4 }}
{{- end }}
  {{- end }}
  {{- if and (eq .Values.controller.agentListenerServiceType "LoadBalancer") (.Values.controller.agentListenerLoadBalancerIP) }}
  loadBalancerIP: {{  .Values.controller.agentListenerLoadBalancerIP }}
  {{- end }}
  {{- end }}
