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