#
# 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.
#

{{- if .Values.components.toolset }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: "{{ template "pulsar.fullname" . }}-{{ .Values.toolset.component }}"
  namespace: {{ template "pulsar.namespace" . }}
  annotations: {{ .Values.toolset.appAnnotations | toYaml | nindent 4 }}
  labels:
    {{- include "pulsar.standardLabels" . | nindent 4 }}
    component: {{ .Values.toolset.component }}
spec:
  serviceName: "{{ template "pulsar.fullname" . }}-{{ .Values.toolset.component }}"
  replicas: {{ .Values.toolset.replicaCount }}
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: Parallel
  selector:
    matchLabels:
      {{- include "pulsar.matchLabels" . | nindent 6 }}
      component: {{ .Values.toolset.component }}
  template:
    metadata:
      labels:
        {{- include "pulsar.template.labels" . | nindent 8 }}
        component: {{ .Values.toolset.component }}
      annotations:
        {{- if .Values.toolset.restartPodsOnConfigMapChange }}
        checksum/config: {{ include (print $.Template.BasePath "/toolset-configmap.yaml") . | sha256sum }}
        {{- end }}
{{- with .Values.toolset.annotations }}
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
    {{- if .Values.toolset.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.toolset.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.toolset.tolerations }}
      tolerations:
{{ toYaml .Values.toolset.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.toolset.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml .Values.toolset.topologySpreadConstraints | nindent 8 }}
    {{- end }}
      terminationGracePeriodSeconds: {{ .Values.toolset.gracePeriod }}
      serviceAccountName: "{{ template "pulsar.fullname" . }}-{{ .Values.toolset.component }}"
      {{- if .Values.toolset.initContainers }}
      initContainers:
        {{- toYaml .Values.toolset.initContainers | nindent 6 }}
      {{- end }}
      containers:
      - name: "{{ template "pulsar.fullname" . }}-{{ .Values.toolset.component }}"
        {{- if (and .Values.images.toolset .Values.images.toolset.repository) }}
        image: "{{ template "pulsar.imageFullName" (dict "image" .Values.images.toolset "root" .) }}"
        imagePullPolicy: "{{ template "pulsar.imagePullPolicy" (dict "image" .Values.images.toolset "root" .) }}"
        {{- else }}
        image: "{{ template "pulsar.imageFullName" (dict "image" .Values.images.broker "root" .) }}"
        imagePullPolicy: "{{ template "pulsar.imagePullPolicy" (dict "image" .Values.images.broker "root" .) }}"
        {{- end }}
      {{- if .Values.toolset.resources }}
        resources:
{{ toYaml .Values.toolset.resources | indent 10 }}
      {{- end }}
        command: ["sh", "-c"]
        args:
        - |
        {{- if .Values.toolset.additionalCommand }}
          {{ .Values.toolset.additionalCommand }}
        {{- end }}
          bin/apply-config-from-env.py conf/client.conf;
          bin/apply-config-from-env.py conf/bookkeeper.conf;
          {{- include "pulsar.toolset.zookeeper.tls.settings" . | nindent 10 }}
          sleep 10000000000
        envFrom:
        - configMapRef:
            name: "{{ template "pulsar.fullname" . }}-{{ .Values.toolset.component }}"
        volumeMounts:
        {{- if .Values.auth.authentication.enabled }}
        {{- if eq .Values.auth.authentication.provider "jwt" }}
        - mountPath: "/pulsar/tokens"
          name: client-token
          readOnly: true
        {{- end }}
        {{- end }}
        {{- if and .Values.tls.enabled (or .Values.tls.broker.enabled .Values.tls.proxy.enabled) }}
        - mountPath: "/pulsar/certs/proxy-ca"
          name: proxy-ca
          readOnly: true
        {{- end}}
        {{- if .Values.toolset.extraVolumeMounts }}
{{ toYaml .Values.toolset.extraVolumeMounts | indent 8 }}
        {{- end }}
        {{- include "pulsar.toolset.certs.volumeMounts" . | nindent 8 }}
      volumes:
      {{- if .Values.auth.authentication.enabled }}
      {{- if eq .Values.auth.authentication.provider "jwt" }}
      - name: client-token
        secret:
          secretName: "{{ .Release.Name }}-token-{{ .Values.auth.superUsers.client }}"
          items:
            - key: TOKEN
              path: client/token
      {{- end}}
      {{- end}}
      {{- if and .Values.tls.enabled (or .Values.tls.broker.enabled .Values.tls.proxy.enabled) }}
      - name: proxy-ca
        secret:
          {{- if eq .Values.certs.internal_issuer.type "selfsigning" }}
          secretName: "{{ .Release.Name }}-{{ .Values.tls.ca_suffix }}"
          {{- end }}
          {{- if eq .Values.certs.internal_issuer.type "ca" }}
          secretName: "{{ .Values.certs.issuers.ca.secretName }}"
          {{- end }}
          items:
            - key: ca.crt
              path: ca.crt
      {{- end}}
      {{- if .Values.toolset.extraVolumes }}
{{ toYaml .Values.toolset.extraVolumes | indent 6 }}
      {{- end }}
      {{- include "pulsar.toolset.certs.volumes" . | nindent 6 }}
      {{- include "pulsar.imagePullSecrets" . | nindent 6}}
{{- end }}
