# 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 "tika-helm.fullname" . }}
  namespace: {{ include "tika-helm.namespace" . }}
  labels:
    {{- include "tika-helm.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "tika-helm.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "tika-helm.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "tika-helm.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      volumes:
      - name: sec-ctx-vol
        emptyDir: {}
      {{- if .Values.tikaConfig }}
      - name: tika-config
        configMap:
          name: {{ .Chart.Name }}-config
          defaultMode: 0777
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          {{- if .Values.tikaConfig }}
          args: ["-c" , "/tika-config/tika-config.xml"]
          {{- end }}
          {{- with .Values.env }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
          - name: sec-ctx-vol
            mountPath: /tmp
          {{- if .Values.tikaConfig }}
          - name: tika-config
            mountPath: /tika-config
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            {{- if eq (lower .Values.livenessProbe.scheme) "tcp" }}
            tcpSocket:
              port: {{ .Values.service.port }}
            {{- else }}
            httpGet:
              path: {{if .Values.config.base_url }}{{- with urlParse (tpl .Values.config.base_url .) }}{{ .path }}{{end}}{{else}}/{{end}}
              port: {{ .Values.service.port }}
              scheme: {{ .Values.livenessProbe.scheme | default "http" }}
            {{- end }}
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds | default 15 }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds | default 30 }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold | default 20 }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds | default 5 }}
          readinessProbe:
            {{- if eq (lower .Values.readinessProbe.scheme) "tcp" }}
            tcpSocket:
              port: {{ .Values.service.port }}
            {{- else }}
            httpGet:
              path: {{if .Values.config.base_url }}{{- with urlParse (tpl .Values.config.base_url .) }}{{ .path }}{{end}}{{else}}/{{end}}
              port: {{ .Values.service.port  }}
              scheme: {{ .Values.readinessProbe.scheme | default "http" }}
            {{- end }}
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds | default 15 }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds | default 30 }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold | default 20 }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds | default 5 }}
          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 }}
