{{- $service := .Values.service -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "rocketchat.fullname" . }}
  annotations:
    {{- if .Values.prometheusScraping.enabled }}
    prometheus.io/path: "/metrics"
    prometheus.io/scrape: "true"
    prometheus.io/port: "{{ .Values.prometheusScraping.port }}"
    {{- end }}
{{- with $service.annotations }}
{{ toYaml . | indent 4 }}
{{- end }}
  labels:
    app.kubernetes.io/name: {{ include "rocketchat.name" . }}
    helm.sh/chart: {{ include "rocketchat.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with $service.labels }}
{{ toYaml . | indent 4 }}
{{- end }}
spec:
  type: {{ $service.type }}
  {{- if eq .Values.service.type "LoadBalancer" }}
  loadBalancerIP: {{ .Values.service.loadBalancerIP }}
  {{- end }}
  ports:
  - name: http
    port: {{ $service.port }}
    targetPort: http
    {{- if and (eq "NodePort" $service.type) $service.nodePort }}
    nodePort: {{ $service.nodePort }}
    {{- end }}
    protocol: TCP
{{- if .Values.prometheusScraping.enabled }}
  {{- if .Values.microservices.enabled }}
  - name: moleculer-metrics
    port: 9458
    targetPort: 9458
    protocol: TCP
  {{- end }}
  - name: metrics
    port: {{ .Values.prometheusScraping.port }}
    targetPort: {{ .Values.prometheusScraping.port }}
    protocol: TCP
{{- end }}
  selector:
    app.kubernetes.io/name: {{ include "rocketchat.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
---
{{ if .Values.federation.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "rocketchat.fullname" . }}-bridge
  labels:
    app.kubernetes.io/name: {{ include "rocketchat.fullname" . }}-bridge
    helm.sh/chart: {{ include "rocketchat.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 3300
    targetPort: 3300
    protocol: TCP
  selector:
    app.kubernetes.io/name: {{ include "rocketchat.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{end}}
