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

{{- if eq .Values.architecture "replication" }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "mariadb.secondary.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mariadb
    app.kubernetes.io/component: secondary
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.secondary.replicaCount }}
  revisionHistoryLimit: {{ .Values.secondary.revisionHistoryLimit }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.secondary.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: mariadb
      app.kubernetes.io/component: secondary
  serviceName: {{ printf "%s-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  podManagementPolicy: {{ .Values.secondary.podManagementPolicy }}
  {{- if .Values.secondary.updateStrategy }}
  updateStrategy: {{- toYaml .Values.secondary.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      annotations:
        {{- if (include "mariadb.secondary.createConfigmap" .) }}
        checksum/configuration: {{ include (print $.Template.BasePath "/secondary/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.passwordUpdateJob.enabled }}
        charts.bitnami.com/password-last-update: {{ now | date "20060102150405" | quote }}
        {{- end }}
        {{- if .Values.secondary.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.secondary.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: mariadb
        app.kubernetes.io/component: secondary
    spec:
      {{- include "mariadb.imagePullSecrets" . | nindent 6 }}
      {{- if or .Values.secondary.schedulerName .Values.schedulerName }}
      schedulerName: {{ (coalesce .Values.secondary.schedulerName .Values.schedulerName) | quote }}
      {{- end }}
      serviceAccountName: {{ template "mariadb.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.secondary.automountServiceAccountToken }}
      {{- if .Values.secondary.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.secondary.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.secondary.podAffinityPreset "component" "secondary" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.secondary.podAntiAffinityPreset "component" "secondary" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.secondary.nodeAffinityPreset.type "key" .Values.secondary.nodeAffinityPreset.key "values" .Values.secondary.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.secondary.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.secondary.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.secondary.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.secondary.priorityClassName }}
      priorityClassName: {{ .Values.secondary.priorityClassName | quote }}
      {{- else if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.secondary.runtimeClassName }}
      runtimeClassName: {{ .Values.secondary.runtimeClassName | quote }}
      {{- else if .Values.runtimeClassName }}
      runtimeClassName: {{ .Values.runtimeClassName | quote }}
      {{- end }}
      {{- if .Values.secondary.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.secondary.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.secondary.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.secondary.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        - name: preserve-logs-symlinks
          image: {{ include "mariadb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.secondary.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.primary.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.secondary.resources }}
          resources: {{ toYaml .Values.secondary.resources | nindent 12 }}
          {{- else if ne .Values.secondary.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.secondary.resourcesPreset) | nindent 12 }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash

              . /opt/bitnami/scripts/libfs.sh
              # We copy the logs folder because it has symlinks to stdout and stderr
              if ! is_dir_empty /opt/bitnami/mariadb/logs; then
                cp -r /opt/bitnami/mariadb/logs /emptydir/app-logs-dir
              fi
          volumeMounts:
            - name: empty-dir
              mountPath: /emptydir
        {{- if and .Values.secondary.podSecurityContext.enabled .Values.volumePermissions.enabled .Values.secondary.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "mariadb.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.secondary.containerSecurityContext.runAsUser }}:{{ .Values.secondary.podSecurityContext.fsGroup }} /bitnami/mariadb
          securityContext:
            runAsUser: 0
          {{- 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/mariadb
              {{- if .Values.secondary.persistence.subPath }}
              subPath: {{ .Values.secondary.persistence.subPath }}
              {{- end }}
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- end }}
        {{- if .Values.secondary.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.secondary.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: mariadb
          image: {{ include "mariadb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.secondary.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.secondary.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.secondary.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.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.secondary.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.secondary.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: MARIADB_REPLICATION_MODE
              value: "slave"
            - name: MARIADB_MASTER_HOST
              value: {{ include "mariadb.primary.fullname" . }}
            - name: MARIADB_MASTER_PORT_NUMBER
              value: {{ coalesce .Values.primary.service.ports.mysql .Values.primary.service.port | quote }}
            - name: MARIADB_MASTER_ROOT_USER
              value: "root"
            {{- if .Values.auth.usePasswordFiles }}
            - name: MARIADB_MASTER_ROOT_PASSWORD_FILE
              value: {{ default "/opt/bitnami/mariadb/secrets/mariadb-root-password" .Values.auth.customPasswordFiles.root }}
            {{- else }}
            - name: MARIADB_MASTER_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-root-password
            {{- end }}
            - name: MARIADB_REPLICATION_USER
              value: {{ .Values.auth.replicationUser | quote }}
            {{- if .Values.auth.usePasswordFiles }}
            - name: MARIADB_REPLICATION_PASSWORD_FILE
              value: {{ default "/opt/bitnami/mariadb/secrets/mariadb-replication-password" .Values.auth.customPasswordFiles.replicator }}
            {{- else }}
            - name: MARIADB_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-replication-password
            {{- end }}
            {{- if .Values.secondary.extraFlags }}
            - name: MARIADB_EXTRA_FLAGS
              value: "{{ .Values.secondary.extraFlags }}"
            {{- end }}
            {{- if .Values.secondary.startupWaitOptions }}
            - name: MARIADB_STARTUP_WAIT_RETRIES
              value: "{{ .Values.secondary.startupWaitOptions.retries | default 300 }}"
            - name: MARIADB_STARTUP_WAIT_SLEEP_TIME
              value: "{{ .Values.secondary.startupWaitOptions.sleepTime | default 2 }}"
            {{- end }}
            - name: MARIADB_ENABLE_SSL
              value: {{ ternary "yes" "no" .Values.tls.enabled | quote }}
            {{- if and .Values.tls.enabled (include "mariadb.tlsCACert" .) }}
            - name: MYSQL_CLIENT_CA_FILE
              value: {{ include "mariadb.tlsCACert" . | quote }}
            {{- end }}
            {{- if .Values.secondary.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.secondary.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.secondary.extraEnvVarsCM .Values.secondary.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.secondary.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.secondary.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.secondary.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.secondary.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          ports:
            - name: mysql
              containerPort: {{ .Values.secondary.containerPorts.mysql }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.secondary.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.secondary.startupProbe.enabled }}
          startupProbe: {{- omit .Values.secondary.startupProbe "enabled" | toYaml | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  password_aux="${MARIADB_MASTER_ROOT_PASSWORD:-}"
                  if [[ -f "${MARIADB_MASTER_ROOT_PASSWORD_FILE:-}" ]]; then
                      password_aux=$(cat "$MARIADB_MASTER_ROOT_PASSWORD_FILE")
                  fi
                  mariadb-admin ping -uroot -p"${password_aux}"
          {{- end }}
          {{- if .Values.secondary.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.secondary.livenessProbe.enabled }}
          livenessProbe: {{- omit .Values.secondary.livenessProbe "enabled" | toYaml | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  password_aux="${MARIADB_MASTER_ROOT_PASSWORD:-}"
                  if [[ -f "${MARIADB_MASTER_ROOT_PASSWORD_FILE:-}" ]]; then
                      password_aux=$(cat "$MARIADB_MASTER_ROOT_PASSWORD_FILE")
                  fi
                  mariadb-admin status -uroot -p"${password_aux}"
          {{- end }}
          {{- if .Values.secondary.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.secondary.readinessProbe.enabled }}
          readinessProbe: {{- omit .Values.secondary.readinessProbe "enabled" | toYaml | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  password_aux="${MARIADB_MASTER_ROOT_PASSWORD:-}"
                  if [[ -f "${MARIADB_MASTER_ROOT_PASSWORD_FILE:-}" ]]; then
                      password_aux=$(cat "$MARIADB_MASTER_ROOT_PASSWORD_FILE")
                  fi
                  mariadb-admin ping -uroot -p"${password_aux}"
          {{- end }}
          {{- end }}
          {{- if .Values.secondary.resources }}
          resources: {{ toYaml .Values.secondary.resources | nindent 12 }}
          {{- else if ne .Values.secondary.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.secondary.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/mariadb
              {{- if .Values.secondary.persistence.subPath }}
              subPath: {{ .Values.secondary.persistence.subPath }}
              {{- end }}
            {{- if or .Values.secondary.configuration .Values.secondary.existingConfigmap }}
            - name: config
              mountPath: /opt/bitnami/mariadb/conf/my.cnf
              subPath: my.cnf
            {{- end }}
            {{- if and .Values.auth.usePasswordFiles (not .Values.auth.customPasswordFiles) }}
            - name: mariadb-credentials
              mountPath: /opt/bitnami/mariadb/secrets/
            {{- end }}
            {{- if .Values.secondary.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.secondary.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mariadb/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mariadb/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mariadb/logs
              subPath: app-logs-dir
            {{- if .Values.tls.enabled }}
            - name: cert
              mountPath: /opt/bitnami/mariadb/certs
            {{- end }}
            {{- if .Values.tde.enabled }}
            - name: tde
              mountPath: /opt/bitnami/mariadb/tde
            {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "mariadb.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          {{- if .Values.metrics.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.metrics.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            {{- if .Values.auth.usePasswordFiles }}
            - name: MARIADB_ROOT_PASSWORD_FILE
              value: {{ default "/opt/bitnami/mysqld-exporter/secrets/mariadb-root-password" .Values.auth.customPasswordFiles.root }}
            {{- else }}
            - name: MARIADB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-root-password
            {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /bin/bash
            - -ec
            - |
              password_aux="${MARIADB_ROOT_PASSWORD:-}"
              if [[ -f "${MARIADB_ROOT_PASSWORD_FILE:-}" ]]; then
                  password_aux=$(cat "$MARIADB_ROOT_PASSWORD_FILE")
              fi
              MYSQLD_EXPORTER_PASSWORD=${password_aux} /bin/mysqld_exporter --mysqld.address=localhost:{{ .Values.secondary.containerPorts.mysql }} --mysqld.username=root --web.listen-address=:{{ .Values.metrics.containerPorts.http }} {{- range .Values.metrics.extraArgs.primary }} {{ . }} {{- end }}
          {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.containerPorts.http }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.metrics.livenessProbe.enabled }}
          livenessProbe: {{- omit .Values.metrics.livenessProbe "enabled" | toYaml | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- end }}
          {{- if .Values.metrics.readinessProbe.enabled }}
          readinessProbe: {{- omit .Values.metrics.readinessProbe "enabled" | toYaml | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- end }}
          {{- end }}
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- else if ne .Values.metrics.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.metrics.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
          {{- if and .Values.auth.usePasswordFiles (not .Values.auth.customPasswordFiles) }}
            - name: mariadb-credentials
              mountPath: /opt/bitnami/mysqld-exporter/secrets/
          {{- end }}
            - name: empty-dir
              mountPath: /tmp
              subPath: app-tmp-dir
          {{- if .Values.metrics.extraVolumeMounts.secondary }}
          {{- include "common.tplvalues.render" (dict "value" .Values.metrics.extraVolumeMounts.secondary "context" $) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.secondary.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.secondary.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if or .Values.secondary.configuration .Values.secondary.existingConfigmap }}
        - name: config
          configMap:
            name: {{ include "mariadb.secondary.configmapName" . }}
        {{- end }}
        {{- if and .Values.auth.usePasswordFiles (not .Values.auth.customPasswordFiles) }}
        - name: mariadb-credentials
          secret:
            secretName: {{ template "mariadb.secretName" . }}
            items:
              - key: mariadb-root-password
                path: mariadb-root-password
              - key: mariadb-replication-password
                path: mariadb-replication-password
        {{- end }}
        {{- if .Values.tls.enabled }}
        - name: cert
          secret:
            secretName: {{ include "mariadb.tlsSecretName" . }}
            defaultMode: 256
        {{- end }}
        {{- if and .Values.tde.enabled ( not .Values.tde.secretsStoreProvider.enabled ) }}
        - name: tde
          secret:
            secretName: {{ .Values.tde.existingSecret }}
            items:
              - key: {{ .Values.tde.randomKeyFilename }}
                path: {{ .Values.tde.randomKeyFilename }}
              - key: {{ .Values.tde.encryptedKeyFilename }}
                path: {{ .Values.tde.encryptedKeyFilename }}
            defaultMode: 256
        {{- else if and .Values.tde.enabled ( .Values.tde.secretsStoreProvider.enabled ) }}
        - name: tde
          csi:
            driver: 'secrets-store.csi.k8s.io'
            readOnly: true
            volumeAttributes:
              secretProviderClass: {{ include "mariadb.tde.secretProviderClassName" . }}     
        {{- end }}
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.secondary.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.secondary.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if not .Values.secondary.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- $claimLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.secondary.persistence.labels .Values.commonLabels ) "context" . ) }}
        labels: {{- include "common.labels.matchLabels" ( dict "customLabels" $claimLabels "context" $ ) | nindent 10 }}
          app.kubernetes.io/component: secondary
        {{- if .Values.secondary.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.secondary.persistence.annotations "context" $ ) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.secondary.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.secondary.persistence.size | quote }}
        {{ include "common.storage.class" (dict "persistence" .Values.secondary.persistence "global" .Values.global) }}
        {{- if .Values.secondary.persistence.selector }}
        selector: {{- include "common.tplvalues.render" (dict "value" .Values.secondary.persistence.selector "context" $) | nindent 10 }}
        {{- end -}}
  {{- end }}
{{- end }}
