#
# 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.oxia }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "pulsar.fullname" . }}-{{ .Values.oxia.component }}-server
  namespace: {{ template "pulsar.namespace" . }}
  labels:
    {{- include "pulsar.standardLabels" . | nindent 4 }}
    component: {{ .Values.oxia.component }}-server
  annotations: {{ .Values.oxia.server.appAnnotations | toYaml | nindent 4 }}
spec:
  replicas: {{ .Values.oxia.server.replicas }}
  selector:
    matchLabels:
      {{- include "pulsar.matchLabels" . | nindent 6 }}
      component: {{ .Values.oxia.component }}-server
  serviceName: {{ template "pulsar.fullname" . }}-{{ .Values.oxia.component }}-svc
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        {{- include "pulsar.template.labels" . | nindent 8 }}
        component: {{ .Values.oxia.component }}-server
      annotations:
        {{- if not .Values.oxia.server.podMonitor.enabled }}
        prometheus.io/scrape: "true"
        prometheus.io/port: "{{ .Values.oxia.server.ports.metrics }}"
        {{- end }}
    spec:
    {{- if .Values.oxia.server.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.oxia.server.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.oxia.server.tolerations }}
      tolerations:
{{ toYaml .Values.oxia.server.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.oxia.server.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml .Values.oxia.server.topologySpreadConstraints | nindent 8 }}
    {{- end }}
      affinity:
        {{- if and .Values.affinity.anti_affinity .Values.oxia.server.affinity.anti_affinity}}
        podAntiAffinity:
          {{ if eq .Values.oxia.server.affinity.type "requiredDuringSchedulingIgnoredDuringExecution"}}
          {{ .Values.oxia.server.affinity.type }}:
          - labelSelector:
              matchExpressions:
              - key: "app"
                operator: In
                values:
                - "{{ template "pulsar.name" . }}"
              - key: "release"
                operator: In
                values:
                - {{ .Release.Name }}
              - key: "component"
                operator: In
                values:
                - {{ .Values.oxia.component }}-server
            topologyKey: {{ .Values.oxia.server.affinity.anti_affinity_topology_key }}
        {{ else }}
          {{ .Values.oxia.server.affinity.type }}:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: "app"
                      operator: In
                      values:
                      - "{{ template "pulsar.name" . }}"
                    - key: "release"
                      operator: In
                      values:
                      - {{ .Release.Name }}
                    - key: "component" 
                      operator: In
                      values:
                      - {{ .Values.oxia.component }}-server
                topologyKey: {{ .Values.oxia.server.affinity.anti_affinity_topology_key }}
        {{ end }}
        {{- end }}    
      serviceAccountName: {{ template "pulsar.fullname" . }}-{{ .Values.oxia.component }}
      {{- if .Values.oxia.server.securityContext }}
      securityContext:
{{ toYaml .Values.oxia.server.securityContext | indent 8 }}
      {{- end }}
      containers:
        - command:
            - "oxia"
            - "server"
            - "--log-json"
            - "--data-dir=/data/db"
            - "--wal-dir=/data/wal"
            - "--db-cache-size-mb={{ .Values.oxia.server.dbCacheSizeMb }}"
            {{- if .Values.oxia.pprofEnabled }}
            - "--profile"
            {{- end}}
          image: "{{ .Values.images.oxia.repository }}:{{ .Values.images.oxia.tag }}"
          imagePullPolicy: "{{ template "pulsar.imagePullPolicy" (dict "image" .Values.images.oxia "root" .) }}"
          name: server
          ports:
            {{- range $key, $value := .Values.oxia.server.ports }}
            - containerPort: {{ $value | int }}
              name: {{ $key }}
            {{- end}}
          resources:
            limits:
              cpu: {{ .Values.oxia.server.cpuLimit }}
              memory: {{ .Values.oxia.server.memoryLimit }}
          volumeMounts:
            - name: {{ template "pulsar.fullname" . }}-{{ .Values.oxia.component }}-data
              mountPath: /data
          livenessProbe:
            {{- include "oxia-cluster.probe" .Values.oxia.server.ports.internal | nindent 12 }}
          readinessProbe:
            {{- include "oxia-cluster.readiness-probe" .Values.oxia.server.ports.internal | nindent 12 }}
          startupProbe:
            {{- include "oxia-cluster.startup-probe" .Values.oxia.server.ports.internal | nindent 12 }}
  volumeClaimTemplates:
    - metadata:
        name: {{ template "pulsar.fullname" . }}-{{ .Values.oxia.component }}-data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        {{- if .Values.oxia.server.storageClassName }}
        storageClassName: {{ .Values.oxia.server.storageClassName }}
        {{- end}}
        resources:
          requests:
            storage: {{ .Values.oxia.server.storageSize }}
{{- end}}