#
# 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 "tryout-kogito-app.fullname" . }}
  labels:
    {{- include "tryout-kogito-app.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "tryout-kogito-app.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "tryout-kogito-app.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "tryout-kogito-app.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ required "A valid .Values.image.repository entry required!" .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: GENERAL_NS
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: general.namespace
            - name: GENERAL_CLUSTERHOST
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: general.appsclusterhost
            - name: KOGITO_SERVICE_URL
              value: "http://{{ include "tryout-kogito-app.fullname" . }}-$(GENERAL_NS).$(GENERAL_CLUSTERHOST)"
            - name: KAFKA_BOOTSTRAP_SERVERS
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: kafka.bootstrap.servers
            - name: KOGITO_DATAINDEX_HTTP_URL
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: kogito.dataindex.httpurl
            - name: KOGITO_DATAINDEX_WS_URL
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: kogito.dataindex.wsurl
            - name: KOGITO_DATA_INDEX_PROPS
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: kogito.dataindex.props
            - name: KOGITO_PERSISTENCE_TYPE
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: kogito.persistence.type
            - name: QUARKUS_DATASOURCE_JDBC_URL
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: quarkus.datasource.jdbc.url
            - name: QUARKUS_DATASOURCE_REACTIVE_URL
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: quarkus.datasource.reactive.url
            - name: QUARKUS_DATASOURCE_DB_KIND
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: quarkus.datasource.db-kind
            - name: QUARKUS_DATASOURCE_USERNAME
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: quarkus.datasource.username
            - name: QUARKUS_DATASOURCE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: kogito-configs
                  key: quarkus.datasource.password
          ports:
            - name: http
              containerPort: {{ .Values.applicationPort }}
              protocol: TCP
          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 }}
