#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# database services
---

{{- if eq .Values.option.database "mysql" }}
{{- if not .Values.mysql.useExternal }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "devlake.fullname" . }}-mysql
  labels:
    {{- include "devlake.labels" . | nindent 4 }}
    {{- with .Values.mysql.service.extraLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  type: {{ .Values.mysql.service.type }}
  {{- if and (eq .Values.mysql.service.type "LoadBalancer") .Values.mysql.service.loadBalancerIP }}
  loadBalancerIP: {{ .Values.mysql.service.loadBalancerIP }}
  {{- end }}
  selector:
    {{- include "devlake.selectorLabels" . | nindent 4 }}
    devlakeComponent: mysql
  ports:
    - protocol: TCP
      name: mysql
      port: 3306
      targetPort: 3306
      {{- if and (eq .Values.mysql.service.type "NodePort") .Values.mysql.service.nodePort }}
      nodePort: {{ .Values.mysql.service.nodePort }}
      {{- end }}
{{- end }}
{{- end }}
# devlake services
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "devlake.fullname" . }}-lake
  labels:
    {{- include "devlake.labels" . | nindent 4 }}
    {{- with .Values.lake.service.extraLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  selector:
    {{- include "devlake.selectorLabels" . | nindent 4 }}
    devlakeComponent: lake
  ports:
    - protocol: TCP
      name: devlake
      port: {{ .Values.lake.port }}
      targetPort: {{ .Values.lake.port }}

---
# ui
apiVersion: v1
kind: Service
metadata:
  name: {{ include "devlake.fullname" . }}-ui
  labels:
    {{- include "devlake.labels" . | nindent 4 }}
    {{- with .Values.ui.service.extraLabels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  type: {{ .Values.service.type }}
  selector:
    {{- include "devlake.selectorLabels" . | nindent 4 }}
    devlakeComponent: ui
  ports:
    - protocol: TCP
      name: ui
      port: 4000
      targetPort: 4000
      {{- if eq .Values.service.type "NodePort" }}
      nodePort: {{ .Values.service.uiPort }}
      {{- end }}
