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

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "hertzbeat.database" . }}
  labels:
    {{- include "hertzbeat.labels" . | nindent 4 }}
    component: database
spec:
  replicas: 1
  serviceName: {{ include "hertzbeat.database" . }}
  selector:
    matchLabels:
      {{- include "hertzbeat.selectorLabels" . | nindent 6 }}
      component: database
  template:
    metadata:
      {{- with .Values.database.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "hertzbeat.selectorLabels" . | nindent 8 }}
        component: database
    spec:
      securityContext:
        {{- toYaml .Values.database.podSecurityContext | nindent 8 }}
      containers:
      - name: database
        image: {{ .Values.database.image.repository }}:{{ .Values.database.image.tag }}
        imagePullPolicy: {{ .Values.database.image.pullPolicy }}
        livenessProbe:
          tcpSocket:
            port: 5432
          initialDelaySeconds: 300
          periodSeconds: 10
        readinessProbe:
          tcpSocket:
            port: 5432
          initialDelaySeconds: 1
          periodSeconds: 10
        resources:
          {{- toYaml .Values.database.resources | nindent 12 }}
        envFrom:
          - configMapRef:
              name: "{{ include "hertzbeat.database" . }}"
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/data
          subPath: {{ .Values.database.persistence.subPath }}
        - mountPath: /docker-entrypoint-initdb.d/schema.sql
          subPath: schema.sql
          name: schema
      volumes:
        - name: schema
          configMap:
            name: {{ include "hertzbeat.database" . }}
        {{- if not .Values.database.persistence.enabled }}
        - name: data
          emptyDir: {}
        {{- else }}
        - name: data
          persistentVolumeClaim: 
            claimName: {{ .Values.database.persistence.existingClaim | default (include "hertzbeat.database" .) }}
        {{- end }}
      {{- with .Values.database.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.database.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.database.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{/*  {{- if and .Values.database.persistence.enabled (not .Values.database.persistence.existingClaim) }}*/}}
{{/*  volumeClaimTemplates:*/}}
{{/*    - metadata:*/}}
{{/*        name: data*/}}
{{/*        labels:*/}}
{{/*          {{- include "hertzbeat.labels" . | indent 10 }}*/}}
{{/*        annotations:*/}}
{{/*        {{- range $key, $value := .Values.database.persistence.annotations }}*/}}
{{/*          {{ $key }}: {{ $value | quote }}*/}}
{{/*        {{- end }}*/}}
{{/*      spec:*/}}
{{/*        accessModes: [{{ .Values.database.persistence.accessMode | quote }}]*/}}
{{/*        {{- if .Values.database.persistence.storageClass }}*/}}
{{/*        {{- if (eq "-" .Values.database.persistence.storageClass) }}*/}}
{{/*        storageClassName: ""*/}}
{{/*        {{- else }}*/}}
{{/*        storageClassName: "{{ .Values.database.persistence.storageClass }}"*/}}
{{/*        {{- end }}*/}}
{{/*        {{- end }}*/}}
{{/*        resources:*/}}
{{/*          requests:*/}}
{{/*            storage: {{ .Values.database.persistence.size | quote }}*/}}
{{/*  {{- end -}}*/}}
