{{/*
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 "parquet.spark.helm.fullname" . }}
  labels:
    {{- include "parquet.spark.helm.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "parquet.spark.helm.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "parquet.spark.helm.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "parquet.spark.helm.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: {{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          env:
            {{- with .Values.aws_creds }}
            - name: aws_access_key_id
              valueFrom:
                secretKeyRef:
                  name: {{ $.Release.Name }}-auth
                  key: awskey
            - name: aws_secret_access_key
              valueFrom:
                secretKeyRef:
                  name: {{ $.Release.Name }}-auth
                  key: awssecret
            - name: aws_session_token
              valueFrom:
                secretKeyRef:
                  name: {{ $.Release.Name }}-auth
                  key: awstoken
            {{- end }}
            - name: flask_prefix
              value: {{ .Values.flask_env.flask_prefix }}
            - name: parquet_file_name
              value: {{ .Values.flask_env.parquet_file_name }}
            {{- if ( index .Values "bitnami-spark" "enabled" ) }}
            - name: master_spark_url
              value: spark://{{ .Release.Name }}-bitnami-spark-master-0.{{ .Release.Name }}-bitnami-spark-headless:7077
            {{- end }}
            - name: parquet_metadata_tbl
              value: {{ .Values.flask_env.parquet_metadata_tbl }}
            - name: es_url
              value: {{ .Values.flask_env.es_url }}
            - name: spark_app_name
              value: {{ .Values.flask_env.spark_app_name }}
            - name: log_level
              value: {{ .Values.flask_env.log_level }}
            - name: spark_config_dict
              value: {{ .Values.flask_env.spark_config_dict | toJson | quote  }}
            - name: geospatial_interval_by_platform
              value: {{ .Values.flask_env.geospatial_interval_by_platform | toJson | quote }}
          {{- if .Values.extraEnvs }}
            {{- toYaml .Values.extraEnvs | nindent 12 }}
          {{- end }}
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
