{{- $fullname := include "common.names.fullname" .  }}
{{- $releaseNamespace := .Release.Namespace }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $kafkaProtocol := include "schema-registry.listenerType" ( dict "protocol" (ternary .Values.kafka.auth.clientProtocol .Values.externalKafka.auth.protocol .Values.kafka.enabled) ) -}}
{{- $kafkaReplicaCount := int .Values.kafka.replicaCount }}
{{- $kafkaFullname := include "schema-registry.kafka.fullname" . }}
{{- $kafkaPort := int .Values.kafka.service.ports.client }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" $ }}
kind: StatefulSet
metadata:
  name: {{ $fullname | quote }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  podManagementPolicy: {{ .Values.podManagementPolicy | quote }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  serviceName: {{ printf "%s-headless" $fullname }}
  {{- if .Values.updateStrategy }}
  updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      {{- if or (include "schema-registry.createConfigMap" .) (include "schema-registry.log4j.createConfigMap" .) .Values.podAnnotations }}
      annotations:
        {{- if (include "schema-registry.createConfigMap" .) }}
        checksum/configuration: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if (include "schema-registry.log4j.createConfigMap" .) }}
        checksum/log4j-configuration: {{ include (print $.Template.BasePath "/log4j-configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "schema-registry.imagePullSecrets" . | indent 6 }}
      serviceAccountName: {{ include "schema-registry.serviceAccountName" . }}
      {{- 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" "" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "" "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.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
       {{- if .Values.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: schema-registry
          image: {{ include "schema-registry.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | 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 }}
          {{- else }}
          command:
            - /bin/bash
            - -ec
            - |
              {{- if contains "SSL" $kafkaProtocol }}
              ID="${MY_POD_NAME#"{{ $fullname }}-"}"
              if [[ -f "/certs/schema-registry.truststore.jks" ]] && [[ -f "/certs/schema-registry-${ID}.keystore.jks" ]]; then
                  mkdir -p /opt/bitnami/schema-registry/certs
                  cp "/certs/schema-registry.truststore.jks" "/opt/bitnami/schema-registry/certs/schema-registry.truststore.jks"
                  cp "/certs/schema-registry-${ID}.keystore.jks" "/opt/bitnami/schema-registry/certs/schema-registry.keystore.jks"
              else
                  echo "Couldn't find the expected Java Key Stores (JKS) files! They are mandatory when encryption via TLS is enabled with Kafka brokers."
                  exit 1
              fi
              {{- end }}
              exec /opt/bitnami/scripts/schema-registry/entrypoint.sh /opt/bitnami/scripts/schema-registry/run.sh
          {{- 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: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: SCHEMA_REGISTRY_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: SCHEMA_REGISTRY_LISTENERS
              value: {{ .Values.listeners | quote }}
            {{- if .Values.auth.tls.enabled }}
            {{- if .Values.auth.tls.keystorePassword }}
            - name: SCHEMA_REGISTRY_SSL_KEYSTORE_PASSWORD
              value: {{ .Values.auth.tls.keystorePassword | quote }}
            {{- end }}
            {{- if .Values.auth.tls.truststorePassword }}
            - name: SCHEMA_REGISTRY_SSL_TRUSTSTORE_PASSWORD
              value: {{ .Values.auth.tls.truststorePassword | quote }}
            {{- end }}
            - name: SCHEMA_REGISTRY_CLIENT_AUTHENTICATION
              value: {{ .Values.auth.tls.clientAuthentication | quote }}
            {{- end }}
            - name: SCHEMA_REGISTRY_AVRO_COMPATIBILY_LEVEL
              value: {{ .Values.avroCompatibilityLevel | quote }}
            - name: SCHEMA_REGISTRY_ADVERTISED_HOSTNAME
              value: "$(MY_POD_NAME).{{ $fullname }}-headless.{{ $releaseNamespace }}.svc.{{ $clusterDomain }}"
            - name: SCHEMA_REGISTRY_KAFKA_BROKERS
              {{- if .Values.kafka.enabled }}
              {{- $brokers := list }}
              {{- range $e, $i := until $kafkaReplicaCount }}
              {{- $brokers = append $brokers (printf "%s://%s-%d.%s-headless.%s.svc.%s:%d" $kafkaProtocol $kafkaFullname $i $kafkaFullname $releaseNamespace $clusterDomain $kafkaPort) }}
              {{- end }}
              value: {{ join "," $brokers | quote }}
              {{- else }}
              value: {{ join "," .Values.externalKafka.brokers | quote }}
              {{- end }}
            {{- if contains "SASL" $kafkaProtocol }}
            - name : SCHEMA_REGISTRY_KAFKA_SASL_MECHANISM
              value: {{ .Values.auth.kafka.saslMechanism }}
            - name: SCHEMA_REGISTRY_KAFKA_SASL_USERS
              {{- if .Values.kafka.enabled }}
              value: {{ join "," .Values.kafka.auth.sasl.jaas.clientUsers | quote }}
              {{- else }}
              value: {{ .Values.externalKafka.auth.jaas.user | quote }}
              {{- end }}
            - name: SCHEMA_REGISTRY_KAFKA_SASL_PASSWORDS
              valueFrom:
                secretKeyRef:
                  {{- if .Values.kafka.enabled }}
                  name: {{ printf "%s-jaas" (include "schema-registry.kafka.fullname" .) }}
                  {{- else }}
                  name: {{ include "common.names.fullname" . }}-external-kafka
                  {{- end }}
                  key: client-passwords
            {{- end }}
            {{- if contains "SSL" $kafkaProtocol }}
            - name: SCHEMA_REGISTRY_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM
              value: {{ .Values.auth.kafka.tlsEndpointIdentificationAlgorithm | quote }}
            {{- if .Values.auth.kafka.keystorePassword }}
            - name: SCHEMA_REGISTRY_KAFKA_KEYSTORE_PASSWORD
              value: {{ .Values.auth.kafka.keystorePassword | quote }}
            {{- end }}
            {{- if .Values.auth.kafka.truststorePassword }}
            - name: SCHEMA_REGISTRY_KAFKA_TRUSTSTORE_PASSWORD
              value: {{ .Values.auth.kafka.truststorePassword | quote }}
            {{- end }}
            {{- 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: 8081
              protocol: TCP
          {{- 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 }}
            tcpSocket:
              port: http
          {{- 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 }}
            tcpSocket:
              port: http
          {{- 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 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if or .Values.configuration .Values.existingConfigmap  .Values.log4j .Values.existingLog4jConfigMap (contains "SSL" $kafkaProtocol) .Values.extraVolumes }}
          volumeMounts:
            {{- if or .Values.configuration .Values.existingConfigmap }}
            - name: configuration
              mountPath: /bitnami/schema-registry/etc/schema-registry/schema-registry.properties
              subPath: schema-registry.properties
            {{- end }}
            {{- if or .Values.log4j .Values.existingLog4jConfigMap }}
            - name: log4j-configuration
              mountPath: /bitnami/schema-registry/etc/schema-registry/log4j.properties
              subPath: log4j.properties
            {{- end }}
            {{- if contains "SSL" $kafkaProtocol }}
            - name: certificates
              mountPath: /certs
              readOnly: true
            {{- end }}
          {{- if .Values.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if or .Values.configuration .Values.existingConfigmap  .Values.log4j .Values.existingLog4jConfigMap (contains "SSL" $kafkaProtocol) .Values.extraVolumes }}
      volumes:
        {{- if or .Values.configuration .Values.existingConfigmap }}
        - name: configuration
          configMap:
            name: {{ include "schema-registry.configmapName" . }}
        {{- end }}
        {{- if or .Values.log4j .Values.existingLog4jConfigMap }}
        - name: log4j-configuration
          configMap:
            name: {{ include "schema-registry.log4j.configmapName" . }}
        {{ end }}
        {{- if contains "SSL" $kafkaProtocol }}
        - name: certificates
          secret:
            secretName: {{ printf "%s" (tpl .Values.auth.kafka.jksSecret $) }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
