# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0

file:
  /opt/bitnami/clickhouse/etc/config.d/01-listen.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - /listen_host>::</
      - /listen_host>0.0.0.0</
  /opt/bitnami/clickhouse/etc/config.d/02-logger.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - /level>{{ .Vars.logLevel }}</
  /opt/bitnami/clickhouse/etc/config.d/03-macros.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - /shard from_env="CLICKHOUSE_SHARD_ID">/
      - /replica from_env="CLICKHOUSE_REPLICA_ID">/
  /opt/bitnami/clickhouse/etc/config.d/04-cluster.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - "remote_servers"
      {{ if .Vars.tls.enabled }}
      - /port from_env="CLICKHOUSE_TCP_SECURE_PORT">/
      - /secure>1</
      {{ else }}
      - /port from_env="CLICKHOUSE_TCP_PORT">/
      {{ end }}
  /opt/bitnami/clickhouse/etc/config.d/05-zookeeper.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - "zookeeper"
      {{ if .Vars.tls.enabled }}
      - /port>{{ .Vars.keeper.containerPorts.tcpSecure }}</
      - /secure>1</
      {{ else }}
      - /port>{{ .Vars.keeper.containerPorts.tcp }}</
      {{ end }}
  {{ if .Vars.tls.enabled }}
  /opt/bitnami/clickhouse/etc/config.d/06-tls.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - "openSSL"
      - /certificateFile>/opt/bitnami/clickhouse/certs/server/tls.crt</
      - /privateKeyFile>/opt/bitnami/clickhouse/certs/server/tls.key</
      - /caConfig>/opt/bitnami/clickhouse/certs/ca/tls.crt</
  {{ end }}
  {{ if .Vars.metrics.enabled }}
  /opt/bitnami/clickhouse/etc/config.d/07-prometheus.xml:
    mode: "0644"
    filetype: file
    exists: true
    contents:
      - "prometheus"
      - /port from_env="CLICKHOUSE_METRICS_PORT">/
  {{ end }}
  /bitnami/clickhouse/data:
    mode: "2750"
    filetype: directory
    exists: true
http:
  {{ if .Vars.tls.enabled }}
  https://clickhouse:{{ .Vars.service.ports.https }}/replicas_status:
    status: 200
    allow-insecure: true
    body:
      - Ok
  {{ else }}
  http://clickhouse:{{ .Vars.service.ports.http }}/replicas_status:
    status: 200
    body:
      - Ok
  {{ end }}
  {{ if .Vars.metrics.enabled }}
  http://clickhouse:{{ .Vars.service.ports.metrics }}/metrics:
    status: 200
  {{ end }}
command:
  {{- $uid := .Vars.containerSecurityContext.runAsUser }}
  {{- $gid := .Vars.podSecurityContext.fsGroup }}
  check-user-info:
    # The UID and GID should always be either the one specified as vars (always a bigger number that the default)
    # or the one randomly defined by openshift (larger values). Otherwise, the chart is still using the default value.
    exec: if [ $(id -u) -lt {{ $uid }} ] || [ $(id -G | awk '{print $2}') -lt {{ $gid }} ]; then exit 1; fi
    exit-status: 0
  check-cluster-status:
    exec: clickhouse-client {{ if .Vars.tls.enabled }} --config /opt/bitnami/clickhouse/etc/config.d/06-tls.xml --secure --port {{ .Vars.containerPorts.tcpSecure }} {{ else }} --port {{ .Vars.containerPorts.tcp }} {{ end }} --user '{{ .Vars.auth.username }}' --password '{{ .Vars.auth.password }}' --query='SELECT COUNT(*) FROM system.clusters;'
    exit-status: 0
    {{- $expectedCount := mul .Vars.shards .Vars.replicaCount }}
    stdout:
      - "{{ $expectedCount }}"
  create-database-in-cluster:
    {{- $database := printf "test_goss_%s" (randAlpha 5) }}
    {{- $table := printf "%s.table_%s" $database (randAlpha 5) }}
    {{- $insertValue := 23 }}
    exec: clickhouse-client --host clickhouse {{ if .Vars.tls.enabled }} --config /opt/bitnami/clickhouse/etc/config.d/06-tls.xml --secure --port {{ .Vars.service.ports.tcpSecure }} {{ else }} --port {{ .Vars.service.ports.tcp }} {{ end }} --user '{{ .Vars.auth.username }}' --password '{{ .Vars.auth.password }}' --multiquery --query='CREATE DATABASE {{ $database }} ON CLUSTER default ENGINE = Memory; CREATE TABLE {{ $table }} (a UInt8) ENGINE = Memory;  INSERT INTO {{ $table }} VALUES({{ $insertValue }}); SELECT * FROM {{ $table }};'
    exit-status: 0
    stdout:
      - "{{ $insertValue }}"
  {{ if .Vars.automountServiceAccountToken }}
  check-sa:
    exec: cat /var/run/secrets/kubernetes.io/serviceaccount/token | cut -d '.' -f 2 | xargs -I '{}' echo '{}====' | fold -w 4 | sed '$ d' | tr -d '\n' | base64 -d
    exit-status: 0
    stdout:
    - /serviceaccount.*name.*{{.Env.BITNAMI_APP_NAME }}/
  {{ end }}
