apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "bedrock.fullname" . }}
  labels:
    {{- include "bedrock.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "bedrock.fullname" . }}
  selector:
    matchLabels:
      {{- include "bedrock.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "bedrock.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "bedrock.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
            - configMapRef:
                name: bedrock-settings
          ports:
            - name: bedrock
              containerPort: 19132
              protocol: UDP
          volumeMounts:
          - name: data
            mountPath: /data
          - name: bedrock-server
            mountPath: /data/whitelist.json
            subPath: whitelist.json
          - name: bedrock-server
            mountPath: /data/permissions.json
            subPath: permissions.json
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
      - name: bedrock-server
        secret:
          defaultMode: 420
          secretName: bedrock-server
      {{- if and .Values.config.persistence.enabled .Values.config.persistence.existingClaim }}
      - name: data
        persistentVolumeClaim:
        {{- with .Values.config.persistence.existingClaim }}
           claimName: {{ tpl . $ }}
        {{- end }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
{{- if and .Values.config.persistence.enabled (not .Values.config.persistence.existingClaim) }}
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: {{ .Values.config.storageClassName | quote }}
      resources:
        requests:
          storage: {{ .Values.config.storageSize }}
{{- end -}}
