#
# 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.
#
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "dolphinscheduler.fullname" . }}-frontend
  labels:
    app.kubernetes.io/name: {{ include "dolphinscheduler.fullname" . }}-frontend
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/component: frontend
spec:
  replicas: {{ .Values.frontend.replicas }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "dolphinscheduler.fullname" . }}-frontend
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/managed-by: {{ .Release.Service }}
      app.kubernetes.io/component: frontend
  strategy:
    type: {{ .Values.frontend.strategy.type | quote }}
    rollingUpdate:
      maxSurge: {{ .Values.frontend.strategy.rollingUpdate.maxSurge | quote }}
      maxUnavailable: {{ .Values.frontend.strategy.rollingUpdate.maxUnavailable | quote }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "dolphinscheduler.fullname" . }}-frontend
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/component: frontend
      {{- if .Values.alert.annotations }}
      annotations:
        {{- toYaml .Values.alert.annotations | nindent 8 }}
      {{- end }}
    spec:
      {{- if .Values.frontend.affinity }}
      affinity: {{- toYaml .Values.frontend.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.frontend.nodeSelector }}
      nodeSelector: {{- toYaml .Values.frontend.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.frontend.tolerations }}
      tolerations: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      - name: {{ include "dolphinscheduler.image.pullSecrets" . }}
      {{- end }}
      containers:
        - name: {{ include "dolphinscheduler.fullname" . }}-frontend
          image: {{ include "dolphinscheduler.image.repository" . | quote }}
          args:
            - "frontend"
          ports:
            - containerPort: 8888
              name: tcp-port
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: TZ
              value: {{ .Values.timezone }}
            - name: FRONTEND_API_SERVER_HOST
              value: '{{ include "dolphinscheduler.fullname" . }}-api'
            - name: FRONTEND_API_SERVER_PORT
              value: "12345"
          {{- if .Values.frontend.resources }}
          resources:
            limits:
              memory: {{ .Values.frontend.resources.limits.memory | quote }}
              cpu: {{ .Values.frontend.resources.limits.cpu | quote }}
            requests:
              memory: {{ .Values.frontend.resources.requests.memory | quote }}
              cpu: {{ .Values.frontend.resources.requests.cpu | quote }}
          {{- end }}
          {{- if .Values.frontend.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: 8888
            initialDelaySeconds: {{ .Values.frontend.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.frontend.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.frontend.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.frontend.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.frontend.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.frontend.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: 8888
            initialDelaySeconds: {{ .Values.frontend.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.frontend.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.frontend.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.frontend.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.frontend.readinessProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            - mountPath: "/var/log/nginx"
              name: {{ include "dolphinscheduler.fullname" . }}-frontend
      volumes:
        - name: {{ include "dolphinscheduler.fullname" . }}-frontend
          {{- if .Values.frontend.persistentVolumeClaim.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "dolphinscheduler.fullname" . }}-frontend
          {{- else }}
          emptyDir: {}
          {{- end }}