apiVersion: apps/v1
kind: Deployment
metadata:
  name: clickhouse
  labels:
    app: clickhouse
    {{- include "common.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      app: clickhouse
      {{- include "common.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        app: clickhouse
        {{- include "common.selectorLabels" . | nindent 8 }}
    spec:
      securityContext:
        fsGroup: 65532
      initContainers:
        - name: restore-clickhouse-backup
          image: {{ .Values.images.clickhouse }}
          resources:
            requests:
              cpu: "0.1"
              memory: "512Mi"
            limits:
              cpu: "0.4"
              memory: "1024Mi"
          env:
            - name: RESTORE_DB
              value: "true"
            - name: DISABLE_PROGRESS
              value: "true"
          volumeMounts: &clickhouseVolumeMounts
            - name: clickhouse-data
              mountPath: /var/lib/clickhouse
            - name: s3
              mountPath: /etc/s3
              readOnly: true
      containers:
        - name: clickhouse
          image: {{ .Values.images.clickhouse }}
          resources:
            requests:
              cpu: "3"
              memory: "16Gi"
            limits:
              cpu: "4"
              memory: "32Gi"
          volumeMounts: *clickhouseVolumeMounts
        # backup requires access to volume, so, it cannot be done using k8s cron job
        - name: clickhouse-backup
          image: {{ .Values.images.clickhouseBackup }}
          resources:
            requests:
              cpu: "0.1"
              memory: 256Mi
              ephemeral-storage: "4Gi"
            limits:
              # should be at least 512 because MinIO uploads in chunks (128MB by default) plus some internal buffers
              cpu: "0.2"
              memory: 1024Mi
              ephemeral-storage: "4Gi"
          env:
            - name: CLICKHOUSE
              value: "127.0.0.1:9000"
          volumeMounts: *clickhouseVolumeMounts
      imagePullSecrets:
        - name: ghcr
      volumes:
        - name: clickhouse-data
          ephemeral:
            volumeClaimTemplate:
              spec:
                accessModes:
                  - ReadWriteOnce
                resources:
                  requests:
                    storage: 100Gi  # Set this to match your ephemeral-storage requirement
        - name: s3
          secret:
            secretName: ij-perf-data-s3-rw
            items:
              - key: AWS_ACCESS_KEY_ID
                path: accessKey
              - key: AWS_SECRET_ACCESS_KEY
                path: secretKey
              - key: AWS_SECRET_ACCESS_KEY
                path: secretKey
              - key: bucket-name
                path: bucket
---
apiVersion: v1
kind: Service
metadata:
  name: clickhouse
  labels:
    app: clickhouse
spec:
  ports:
    - name: tcp
      port: 9000
      protocol: TCP
    - name: http
      port: 8123
      protocol: TCP
  selector:
    app: clickhouse