#
# 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.
#
---
# devlake-ui
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "devlake.fullname" . }}-ui
  labels:
    {{- include "devlake.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- include "devlake.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "devlake.selectorLabels" . | nindent 8 }}
        devlakeComponent: ui
        {{- with .Values.ui.extraLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.ui.securityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: config-ui
{{- if .Values.ui.image.tag }}
          image: "{{ .Values.ui.image.repository }}:{{ .Values.ui.image.tag }}"
{{- else }}
          image: "{{ .Values.ui.image.repository }}:{{ .Values.imageTag }}"
{{- end }}
          imagePullPolicy: {{ .Values.ui.image.pullPolicy }}
          ports:
            - containerPort: 4000
          {{- if .Values.ui.basicAuth.enabled }}
          envFrom:
            - secretRef:
                name: {{ include "devlake.ui.auth.secret" . }}
          {{- end }}
          env:
            - name: DEVLAKE_ENDPOINT
              value: {{ include "devlake.fullname" . }}-lake.{{ .Release.Namespace }}.svc.cluster.local:8080
{{- if not .Values.grafana.enabled }}
            - name: GRAFANA_ENDPOINT
              value: {{ .Values.grafana.external.url }}
            - name: USE_EXTERNAL_GRAFANA
              value: "true"
{{- else }}
            - name: GRAFANA_ENDPOINT
              value: {{ .Release.Name }}-grafana.{{ .Release.Namespace }}.svc.cluster.local:80
{{- end }}
            {{- with .Values.envs }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- with .Values.ui.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.ui.containerSecurityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.ui.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ui.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ui.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

---
# devlake
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "devlake.fullname" . }}-lake
  labels:
  {{- include "devlake.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
  {{- include "devlake.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "devlake.selectorLabels" . | nindent 8 }}
        devlakeComponent: lake
        {{- with .Values.lake.extraLabels }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.lake.securityContext }}
      securityContext:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      initContainers:
        {{- include "common.initContainerWaitDatabase" . | nindent 8 }}
        {{- with .Values.lake.containerSecurityContext }}
        securityContext:
        {{- toYaml . | nindent 12 }}
      {{- end }}
      containers:
        - name: lake
          {{- if .Values.lake.image.tag }}
          image: "{{ .Values.lake.image.repository }}:{{ .Values.lake.image.tag }}"
          {{- else }}
          image: "{{ .Values.lake.image.repository }}:{{ .Values.imageTag }}"
          {{- end }}
          imagePullPolicy: {{ .Values.lake.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.lake.port }}
          livenessProbe:
            httpGet:
              path: /ping
              port: {{ .Values.lake.port }}
              scheme: HTTP
            initialDelaySeconds: 60
            timeoutSeconds: 30
          envFrom:
            - secretRef:
                name: {{ include "devlake.mysql.secret" . }}
            - secretRef:
                name: {{ include "devlake.lake.encryption.secret" . }}
          env:
            - name: PORT
              value: "{{ .Values.lake.port }}"
            {{- with .Values.lake.envs }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
            {{- with .Values.envs }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.lake.resources }}
          resources:
          {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.lake.containerSecurityContext }}
          securityContext:
          {{- toYaml . | nindent 12 }}
      {{- end }}
      {{- if .Values.lake.hostNetwork }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      {{- end }}
      {{- with .Values.lake.nodeSelector }}
      nodeSelector:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.lake.affinity }}
      affinity:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.lake.tolerations }}
      tolerations:
      {{- toYaml . | nindent 8 }}
  {{- end }}


