{{- $apiVersions := (include "k8s.apiVersions" .) | fromJson }}
{{- $fullName := (include "k8s.serviceFullname" .) }}
{{- $labels := (include "k8s.labels" .) | fromJson }}
{{- $selectorLabels := (include "k8s.selectorLabels" .) | fromJson }}
{{- $appPort := .Values.service.port.servicePort | default .Values.app.port }}
{{- if or .Values.kong.plugin.enabled (eq (include "k8s.deploymentEnabled" .) "true") }}
---
apiVersion: {{ get $apiVersions "Service" }}
kind: Service
metadata:
  name: {{ $fullName }}
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
  {{- if .Values.service.hostname.externalDNS }}
    "external-dns.alpha.kubernetes.io/hostname": {{ .Values.service.hostname.hostOverride | default (include "service.hostname" .) }}
  {{- end }}
  {{- with .Values.service.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.service.type | default "ClusterIP" }}
  {{- if eq .Values.service.type "ExternalName" }}
  externalName: {{ .Values.service.externalName | required "External Name Required!"}}
  {{- end }}
  ports:
    - port: {{ $appPort }}
      targetPort: {{ .Values.service.port.name | default "http" }}
      protocol: {{ .Values.service.port.protocol | default "TCP" }}
      name: {{ .Values.service.port.name | default "http" }}
  {{- if .Values.service.healthcheck.servicePort }}
    {{- if (eq (int .Values.service.healthcheck.servicePort) (int $appPort)) }}
      {{- fail "error: healthcheck port must be unique!" }}
    {{- else }}
    - port: {{ .Values.service.healthcheck.servicePort }}
      targetPort: healthcheck
      protocol: {{ .Values.service.healthcheck.protocol | default "TCP" }}
      name: healthcheck
    {{- end }}
  {{- end }}
  {{- if .Values.service.ports }}
  {{- $portList := list }}
  {{- range $port := .Values.service.ports }}
  {{- if not $port.port }}
    {{- fail "error: service.ports[].port required!" }}
  {{- else }}
  {{- if or (has $port.port $portList) (eq (int $port.port) (int $appPort)) (eq (int $port.port) (int $.Values.service.healthcheck.servicePort)) }}
    {{- fail (cat "error: service.ports[].port must be uinque!" $port.port) }}
  {{- end }}
    {{- $portList = append $portList $port.port }}
  {{- $portName := $port.name | required "service.ports[].name required!" }}
    {{- if empty $port.targetPort }}
      {{- fail "service.ports[].targetPort required!" }}
    {{- end }}
    - port: {{ $port.port }}
      targetPort: {{ $portName }}
      protocol: {{ $port.protocol | default "TCP" }}
      name: {{ $portName }}
  {{- end }}
  {{- end }}
  {{- end }}
  selector:
  {{- range $key, $value := $selectorLabels }}
    {{ $key }}: {{ $value }}
  {{- end }}
{{- end }}

{{- if .Values.service.headless.enabled }}
---
apiVersion: {{ get $apiVersions "Service" }}
kind: Service
metadata:
  name: "{{ $fullName }}-headless"
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
  {{- with .Values.service.headless.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  type: "ClusterIP"
  clusterIP: "None"
  ports:
    - port: {{ $appPort }}
      targetPort: {{ .Values.service.port.name | default "http" }}
      protocol: {{ .Values.service.port.protocol | default "TCP" }}
      name: {{ .Values.service.port.name | default "http" }}
  {{- if .Values.service.healthcheck.servicePort }}
    {{- if (eq (int .Values.service.healthcheck.servicePort) (int $appPort)) }}
      {{- fail "error: healthcheck port must be unique!" }}
    {{- else }}
    - port: {{ .Values.service.healthcheck.servicePort }}
      targetPort: healthcheck
      protocol: {{ .Values.service.healthcheck.protocol | default "TCP" }}
      name: healthcheck
    {{- end }}
  {{- end }}
  {{- if .Values.service.ports }}
  {{- $portList := list }}
  {{- range $port := .Values.service.ports }}
  {{- if not $port.port }}
    {{- fail "error: service.ports[].port required!" }}
  {{- else }}
  {{- if or (has $port.port $portList) (eq (int $port.port) (int $appPort)) (eq (int $port.port) (int $.Values.service.healthcheck.servicePort)) }}
    {{- fail (cat "error: service.ports[].port must be uinque!" $port.port) }}
  {{- end }}
    {{- $portList = append $portList $port.port }}
  {{- $portName := $port.name | required "service.ports[].name required!" }}
    {{- if empty $port.targetPort }}
      {{- fail "service.ports[].targetPort required!" }}
    {{- end }}
    - port: {{ $port.port }}
      targetPort: {{ $portName }}
      protocol: {{ $port.protocol | default "TCP" }}
      name: {{ $portName }}
  {{- end }}
  {{- end }}
  {{- end }}
  selector:
  {{- range $key, $value := $selectorLabels }}
    {{ $key }}: {{ $value }}
  {{- end }}
{{- end }}

{{- if .Values.canary.enabled }}
{{- $apiVersions := (include "k8s.apiVersions" .) | fromJson }}
{{- $fullName := (include "k8s.serviceFullname" .) }}
{{- $labels := (include "k8s.labels" .) | fromJson }}
{{- $selectorLabels := (include "k8s.selectorLabels" .) | fromJson }}
{{- $appPort := .Values.service.port.servicePort | default .Values.app.port }}
{{- if or .Values.kong.plugin.enabled (eq (include "k8s.deploymentEnabled" .) "true") }}
---
apiVersion: {{ get $apiVersions "Service" }}
kind: Service
metadata:
  name: {{ $fullName }}-canary
  labels:
  {{- range $key, $value := $labels }}
    {{ $key }}: {{ $value }}
  {{- end }}
  annotations:
  {{- if .Values.service.hostname.externalDNS }}
    "external-dns.alpha.kubernetes.io/hostname": {{ .Values.service.hostname.hostOverride | default (include "service.hostname" .) }}
  {{- end }}
  {{- with .Values.service.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.service.type | default "ClusterIP" }}
  {{- if eq .Values.service.type "ExternalName" }}
  externalName: {{ .Values.service.externalName | required "External Name Required!"}}
  {{- end }}
  ports:
    - port: {{ $appPort }}
      targetPort: {{ .Values.service.port.name | default "http" }}
      protocol: {{ .Values.service.port.protocol | default "TCP" }}
      name: {{ .Values.service.port.name | default "http" }}
  {{- if .Values.service.healthcheck.servicePort }}
    {{- if (eq (int .Values.service.healthcheck.servicePort) (int $appPort)) }}
      {{- fail "error: healthcheck port must be unique!" }}
    {{- else }}
    - port: {{ .Values.service.healthcheck.servicePort }}
      targetPort: healthcheck
      protocol: {{ .Values.service.healthcheck.protocol | default "TCP" }}
      name: healthcheck
    {{- end }}
  {{- end }}
  {{- if .Values.service.ports }}
  {{- $portList := list }}
  {{- range $port := .Values.service.ports }}
  {{- if not $port.port }}
    {{- fail "error: service.ports[].port required!" }}
  {{- else }}
  {{- if or (has $port.port $portList) (eq (int $port.port) (int $appPort)) (eq (int $port.port) (int $.Values.service.healthcheck.servicePort)) }}
    {{- fail (cat "error: service.ports[].port must be uinque!" $port.port) }}
  {{- end }}
    {{- $portList = append $portList $port.port }}
  {{- $portName := $port.name | required "service.ports[].name required!" }}
    {{- if empty $port.targetPort }}
      {{- fail "service.ports[].targetPort required!" }}
    {{- end }}
    - port: {{ $port.port }}
      targetPort: {{ $portName }}
      protocol: {{ $port.protocol | default "TCP" }}
      name: {{ $portName }}
  {{- end }}
  {{- end }}
  {{- end }}
  selector:
  {{- range $key, $value := $selectorLabels }}
    {{ $key }}: {{ $value }}
  {{- end }}
{{- end }}
{{- end }}
