{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: solr
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
  namespace: {{ include "common.names.namespace" . | quote }}
spec:
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: solr
  serviceName: {{ printf "%s-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  updateStrategy: {{- include "common.tplvalues.render" (dict "value" .Values.updateStrategy "context" $ ) | nindent 4 }}
  template:
    metadata:
      annotations:
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if or (include "solr.createSecret" .) (include "solr.createTlsPasswordsSecret" .) }}
        checksum/secrets: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
        {{- end }}
        {{- if (include "solr.createTlsSecret" .)}}
        checksum/tls-secrets: {{ include (print $.Template.BasePath "/tls-secrets.yaml") . | sha256sum }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: solr
    spec:
      serviceAccountName: {{ include "solr.serviceAccountName" . }}
      {{- include "solr.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "solr" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "solr" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      enableServiceLinks: {{ .Values.enableServiceLinks }}
      initContainers:
        - name: prepare-server-dir
          image: {{ include "solr.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash

              . /opt/bitnami/scripts/liblog.sh

              info "Copying server dir to empty dir"
              # In order to not break the application functionality (such as upgrades or plugins) we need
              # to make the base directory writable, so we need to copy it to an empty dir volume
              cp -r --preserve=mode /opt/bitnami/solr/server /emptydir/app-server-dir

              info "Copy operation completed"
          volumeMounts:
            - name: empty-dir
              mountPath: /emptydir
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "solr.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/solr
              chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }} /bitnami/solr
              find /bitnami/solr -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
          {{- if .Values.volumePermissions.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/solr
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: init-certs
          image: {{ include "solr.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          command:
            - /scripts/init-certs.sh
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            {{- if .Values.usePasswordFiles }}
            - name: SOLR_SSL_KEY_STORE_PASSWORD_FILE
              value: "/opt/bitnami/solr/secrets/tls/keystore-password"
            - name: SOLR_SSL_TRUST_STORE_PASSWORD_FILE
              value: "/opt/bitnami/solr/secrets/tls/truststore-password"
            {{- else }}
            - name: SOLR_SSL_KEY_STORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "solr.tlsPasswordsSecret" . }}
                  key: keystore-password
            - name: SOLR_SSL_TRUST_STORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "solr.tlsPasswordsSecret" . }}
                  key: truststore-password
            {{- end }}
          {{- if .Values.tls.resources }}
          resources: {{- toYaml .Values.tls.resources | nindent 12 }}
          {{- else if ne .Values.tls.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.tls.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: scripts
              mountPath: /scripts/init-certs.sh
              subPath: init-certs.sh
            - name: certs
              mountPath: /certs
            - name: empty-dir
              mountPath: /opt/bitnami/solr/certs
              subPath: app-certs-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.usePasswordFiles }}
            - name: solr-secrets
              mountPath: /opt/bitnami/solr/secrets
            {{- end }}
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: solr
          image: {{ include "solr.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: SOLR_ENABLE_CLOUD_MODE
              value: {{ ternary "yes" "no" .Values.cloudEnabled | quote }}
            - name: SOLR_NUMBER_OF_NODES
              value: {{ .Values.replicaCount | quote }}
            - name: SOLR_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: SOLR_SERVER_DIRECTORY
              value: {{ .Values.serverDirectory | quote }}
            {{- if .Values.cloudEnabled }}
            - name: SOLR_COLLECTION
              value: {{ .Values.collection | quote }}
            {{- else }}
            - name: SOLR_CORES
              value: {{ .Values.coreNames | join "," | quote }}
            {{- end }}
            - name: SOLR_COLLECTION_SHARDS
              value: {{ .Values.collectionShards | quote }}
            - name: SOLR_COLLECTION_REPLICAS
              value: {{ .Values.collectionReplicas | quote }}
            {{- if .Values.heap }}
            - name: SOLR_HEAP
              value: {{ .Values.heap | quote }}
            {{- end }}
            {{- if .Values.javaMem }}
            - name: SOLR_JAVA_MEM
              value: {{ .Values.javaMem | quote }}
            {{- end }}
            - name: SOLR_ENABLE_AUTHENTICATION
              value: {{ ternary "yes" "no" .Values.auth.enabled | quote }}
            {{- if .Values.auth.enabled }}
            - name: SOLR_ADMIN_USERNAME
              value: {{ .Values.auth.adminUsername | quote }}
            {{- if .Values.usePasswordFiles }}
            - name: SOLR_ADMIN_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/solr/secrets/pass/%s" (include "solr.secretPasswordKey" .) }}
            {{- else }}
            - name: SOLR_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "solr.secretName" . }}
                  key: {{ include "solr.secretPasswordKey" . }}
            {{- end }}
            {{- end }}
            - name: SOLR_ZK_HOSTS
              value: {{ include "solr.zookeeper.hosts" . | quote }}
            {{- if .Values.tls.enabled }}
            - name: SOLR_SSL_ENABLED
              value: "yes"
            {{- if (include "solr.createTlsSecret" .) }}
            - name: SOLR_SSL_KEY_STORE_TYPE
              value: "JKS"
            - name: SOLR_SSL_TRUST_STORE_TYPE
              value: "JKS"
            {{- end }}
            - name: SOLR_SSL_KEY_STORE
              value: /opt/bitnami/solr/certs/keystore.p12
            - name: SOLR_SSL_TRUST_STORE
              value: /opt/bitnami/solr/certs/truststore.p12
            {{- if .Values.usePasswordFiles }}
            - name: SOLR_SSL_KEY_STORE_PASSWORD_FILE
              value: "/opt/bitnami/solr/secrets/tls/keystore-password"
            - name: SOLR_SSL_TRUST_STORE_PASSWORD_FILE
              value: "/opt/bitnami/solr/secrets/tls/truststore-password"
            {{- else }}
            - name: SOLR_SSL_KEY_STORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "solr.tlsPasswordsSecret" . }}
                  key: keystore-password
            - name: SOLR_SSL_TRUST_STORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "solr.tlsPasswordsSecret" . }}
                  key: truststore-password
            {{- end }}
            - name: SOLR_SSL_CHECK_PEER_NAME
              value: "false"
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.extraEnvVarsCM .Values.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
              - /bin/bash
              - -ec
              - |
                {{- if and .Values.usePasswordFiles .Values.auth.enabled }}
                export SOLR_ADMIN_PASSWORD="$(< $SOLR_ADMIN_PASSWORD_FILE)"
                {{- end }}
                curl --silent --connect-timeout 15000 {{ ternary "--user ${SOLR_ADMIN_USERNAME}:${SOLR_ADMIN_PASSWORD}" "" .Values.auth.enabled }} http://localhost:${SOLR_PORT_NUMBER}/solr/admin/info/system | grep --quiet  '\"status\":0'
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
              - /bin/bash
              - -ec
              - |
                {{- if and .Values.usePasswordFiles .Values.auth.enabled }}
                export SOLR_ADMIN_PASSWORD="$(< $SOLR_ADMIN_PASSWORD_FILE)"
                {{- end }}
                curl --silent --connect-timeout 15000 {{ ternary "--user ${SOLR_ADMIN_USERNAME}:${SOLR_ADMIN_PASSWORD}" "" .Values.auth.enabled }} http://localhost:${SOLR_PORT_NUMBER}/api/node/health | grep --quiet  '\"status\":\"OK\"'
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
              - /bin/bash
              - -ec
              - |
                {{- if and .Values.usePasswordFiles .Values.auth.enabled }}
                export SOLR_ADMIN_PASSWORD="$(< $SOLR_ADMIN_PASSWORD_FILE)"
                {{- end }}
                curl --silent --connect-timeout 15000  {{ ternary "--user ${SOLR_ADMIN_USERNAME}:${SOLR_ADMIN_PASSWORD}" "" .Values.auth.enabled }} http://localhost:${SOLR_PORT_NUMBER}/api/node/health | grep --quiet  '\"status\":\"OK\"'
          {{- end }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/solr/server
              subPath: app-server-dir
            - name: empty-dir
              mountPath: /opt/bitnami/solr/logs
              subPath: app-logs-dir
            - name: empty-dir
              mountPath: /opt/bitnami/solr/tmp
              subPath: app-tmp-dir
            - name: scripts
              mountPath: /scripts/setup.sh
              subPath: setup.sh
            {{- if and .Values.usePasswordFiles .Values.auth.enabled }}
            - name: solr-pass-secret
              mountPath: /opt/bitnami/solr/secrets/pass
            {{- end }}
            {{- if and .Values.usePasswordFiles .Values.tls.enabled }}
            - name: solr-tls-secret
              mountPath: /opt/bitnami/solr/secrets/tls
            {{- end }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
            {{- if .Values.persistence.subPath }}
              subPath: {{ .Values.persistence.subPath }}
            {{- else if .Values.persistence.subPathExpr }}
              subPathExpr: {{ .Values.persistence.subPathExpr }}
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: empty-dir
              mountPath: /opt/bitnami/solr/certs
              subPath: app-certs-dir
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: scripts
          configMap:
            name: {{ printf "%s-scripts" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
            defaultMode: 0755
        {{- if and .Values.usePasswordFiles .Values.auth.enabled }}
        - name: solr-pass-secret
          secret:
            secretName: {{ include "solr.secretName" . }}
        {{- end }}
        {{- if and .Values.usePasswordFiles .Values.tls.enabled }}
        - name: solr-tls-secret
          secret:
            secretName: {{ include "solr.tlsPasswordsSecret" . }}
        {{- end }}
        {{- if and .Values.persistence.enabled .Values.persistence.existingClaim }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ printf "%s" (tpl .Values.persistence.existingClaim .) }}
        {{- else if not .Values.persistence.enabled }}
        - name: data
          emptyDir: {}
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: certs
          secret:
            secretName: {{ include "solr.tlsSecretName" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
        annotations:
          {{- if .Values.persistence.annotations }}
          {{- include "common.tplvalues.render" (dict "value" .Values.persistence.annotations "context" $) | nindent 10 }}
          {{- end }}
          {{- if .Values.commonAnnotations }}
          {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 10 }}
          {{- end }}
        {{- if .Values.commonLabels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.persistence "global" .Values.global) | nindent 8 }}
        {{- if .Values.persistence.selector }}
        selector: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.selector "context" $) | nindent 10 }}
        {{- end }}
  {{- end }}
