apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "clickhouse.fullname" . }}
  labels:
    {{- include "clickhouse.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "clickhouse.fullname" . }}
  selector:
    matchLabels:
      {{- include "clickhouse.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "clickhouse.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "clickhouse.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}-backup
          env:
          - name: CLICKHOUSE_USER
            value: "{{ .Values.username }}"
          - name: CLICKHOUSE_PASSWORD
            value: "{{ .Values.password }}"
          {{- range $key, $value := .Values.backupEnv }}
          - name: "{{ $key }}"
            value: "{{ $value }}"
          {{- end }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.backupImage.repository }}:{{ .Values.backupImage.tag }}"
          imagePullPolicy: {{ .Values.backupImage.pullPolicy }}
          args:
            - server
          ports:
          - containerPort: 7171
            name: backup-web
          volumeMounts:
          - name: default-chi-openreplay-clickhouse-replicated-0-0-0 
            mountPath: /var/lib/clickhouse
        - name: {{ .Chart.Name }}
          env:
          - name: CLICKHOUSE_USER
            value: "{{ .Values.username }}"
          - name: CLICKHOUSE_PASSWORD
            value: "{{ .Values.password }}"
          - name: CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT
            value: "1"
          {{- range $key, $value := .Values.env }}
          - name: "{{ $key }}"
            value: "{{ $value }}"
          {{- end }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
          - containerPort: 9000
            name: web
          - containerPort: 8123
            name: data
          volumeMounts:
          - name: default-chi-openreplay-clickhouse-replicated-0-0-0 
            mountPath: /var/lib/clickhouse
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  volumeClaimTemplates:
  - metadata:
      name: default-chi-openreplay-clickhouse-replicated-0-0-0
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: {{ .Values.storageSize }}
