|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Service paths |
| 5 | +GLOBAL_OVERRIDES_DIR="/etc/genestack/helm-configs/global_overrides" |
| 6 | +SERVICE_CONFIG_DIR="/etc/genestack/helm-configs/clickhouse-helm-overrides.yaml" |
| 7 | +BASE_OVERRIDES="/opt/genestack/base-helm-configs/clickhouse/clickhouse-helm-overrides.yaml" |
| 8 | +KUSTOMIZE_DIR="/etc/genestack/kustomize/clickhouse/overlay" |
| 9 | +VERSIONS_FILE="/etc/genestack/helm-chart-versions.yaml" |
| 10 | + |
| 11 | +NS="clickhouse" |
| 12 | +OP_RELEASE="altinity-operator" |
| 13 | + |
| 14 | +need() { command -v "$1" >/dev/null || { echo "Missing required command: $1" >&2; exit 1; }; } |
| 15 | +need helm |
| 16 | +need kubectl |
| 17 | +need kustomize |
| 18 | +need awk |
| 19 | +need sha256sum |
| 20 | +need openssl |
| 21 | +need envsubst |
| 22 | + |
| 23 | +echo "==> Ensuring namespace '${NS}' exists" |
| 24 | +kubectl get ns "${NS}" >/dev/null 2>&1 || kubectl create ns "${NS}" |
| 25 | + |
| 26 | +# --- Create/reuse DB password secret --- |
| 27 | +echo "==> Ensuring DB password secret exists in namespace '${NS}'" |
| 28 | +if ! kubectl -n "${NS}" get secret clickhouse-db-passwords >/dev/null 2>&1; then |
| 29 | + WRITER_PLAIN="$(openssl rand -hex 16)" |
| 30 | + READER_PLAIN="$(openssl rand -hex 16)" |
| 31 | + WRITER_SHA256="$(printf "%s" "${WRITER_PLAIN}" | sha256sum | awk "{print \$1}")" |
| 32 | + READER_SHA256="$(printf "%s" "${READER_PLAIN}" | sha256sum | awk "{print \$1}")" |
| 33 | + kubectl -n "${NS}" apply -f - <<EOF |
| 34 | +apiVersion: v1 |
| 35 | +kind: Secret |
| 36 | +metadata: |
| 37 | + name: clickhouse-db-passwords |
| 38 | +type: Opaque |
| 39 | +stringData: |
| 40 | + writer_password_sha256: "${WRITER_SHA256}" |
| 41 | + reader_password_sha256: "${READER_SHA256}" |
| 42 | + writer_password_plain: "${WRITER_PLAIN}" |
| 43 | + reader_password_plain: "${READER_PLAIN}" |
| 44 | +EOF |
| 45 | +else |
| 46 | + echo " Secret 'clickhouse-db-passwords' already exists; reusing." |
| 47 | +fi |
| 48 | + |
| 49 | +# --- Read versions from YAML without yq --- |
| 50 | +# Simple awk-based extractor: get "key: value" lines. |
| 51 | +get_yaml_val() { |
| 52 | + local key="$1" |
| 53 | + awk -v k="$key" ' |
| 54 | + $1 ~ k ":" { |
| 55 | + # value may have quotes |
| 56 | + sub(/^[^:]+:[[:space:]]*/,"") |
| 57 | + gsub(/"/,"") |
| 58 | + print |
| 59 | + exit |
| 60 | + }' "${VERSIONS_FILE}" |
| 61 | +} |
| 62 | + |
| 63 | +OP_CHART="altinity/altinity-clickhouse-operator" |
| 64 | +OP_VERSION="$(get_yaml_val "clickhouse-operator")" |
| 65 | +CH_SERVER_IMAGE="altinity/clickhouse-server:$(get_yaml_val "clickhouse-server")" |
| 66 | +CH_KEEPER_IMAGE="clickhouse/clickhouse-keeper:$(get_yaml_val "clickhouse-keeper")" |
| 67 | + |
| 68 | +if [[ -z "${OP_VERSION}" || -z "${CH_SERVER_IMAGE}" || -z "${CH_KEEPER_IMAGE}" ]]; then |
| 69 | + echo "Failed to parse ${VERSIONS_FILE}. Please verify keys." >&2 |
| 70 | + exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +echo "==> Helm repo add/update for operator chart: ${OP_CHART} @ ${OP_VERSION}" |
| 74 | +helm repo add altinity https://helm.altinity.com >/dev/null |
| 75 | +helm repo update >/dev/null |
| 76 | + |
| 77 | +echo "==> Installing/Upgrading ClickHouse Operator release '${OP_RELEASE}'" |
| 78 | +HELM_CMD="helm upgrade --install ${OP_RELEASE} ${OP_CHART} \ |
| 79 | + --version ${OP_VERSION} \ |
| 80 | + -n ${NS}" |
| 81 | + |
| 82 | +HELM_CMD+=" -f ${BASE_OVERRIDES}" |
| 83 | + |
| 84 | +for dir in "$GLOBAL_OVERRIDES_DIR" "$SERVICE_CONFIG_DIR"; do |
| 85 | + if compgen -G "${dir}/*.yaml" > /dev/null; then |
| 86 | + for yaml_file in "${dir}"/*.yaml; do |
| 87 | + HELM_CMD+=" -f ${yaml_file}" |
| 88 | + done |
| 89 | + fi |
| 90 | +done |
| 91 | + |
| 92 | +HELM_CMD+=" $@" |
| 93 | + |
| 94 | +echo "==> Executing Helm command:" |
| 95 | +echo "${HELM_CMD}" |
| 96 | +eval "${HELM_CMD}" |
| 97 | + |
| 98 | +echo "==> Waiting for operator to be ready" |
| 99 | +kubectl -n "${NS}" rollout status deploy/${OP_RELEASE} --timeout=300s |
| 100 | + |
| 101 | +# --- Apply Kustomize with envsubsted images from versions file --- |
| 102 | +export CLICKHOUSE_SERVER_IMAGE="${CH_SERVER_IMAGE}" |
| 103 | +export CLICKHOUSE_KEEPER_IMAGE="${CH_KEEPER_IMAGE}" |
| 104 | + |
| 105 | +echo "==> Applying ClickHouse Keeper + Cluster (kustomize + envsubst)" |
| 106 | +# We envsubst only image placeholders present in manifests. |
| 107 | +kubectl kustomize "${KUSTOMIZE_DIR}" | envsubst '${CLICKHOUSE_SERVER_IMAGE} ${CLICKHOUSE_KEEPER_IMAGE}' | kubectl apply -n "${NS}" -f - |
| 108 | + |
| 109 | +echo "==> Waiting for ClickHouse cluster pods (CHI=ch) to be Ready" |
| 110 | +kubectl -n "${NS}" wait --for=condition=Ready pod -l clickhouse.altinity.com/chi=ch --timeout=900s |
| 111 | + |
| 112 | +echo "==> Service endpoint (HTTP 8123)" |
| 113 | +kubectl -n "${NS}" get svc clickhouse-http -o wide |
| 114 | + |
| 115 | +# Print connection hints using stored plaintext (if present) |
| 116 | +WRITER_PLAIN="$(kubectl -n "${NS}" get secret clickhouse-db-passwords -o jsonpath='{.data.writer_password_plain}' 2>/dev/null | base64 -d || true)" |
| 117 | +READER_PLAIN="$(kubectl -n "${NS}" get secret clickhouse-db-passwords -o jsonpath='{.data.reader_password_plain}' 2>/dev/null | base64 -d || true)" |
| 118 | + |
| 119 | +# Print out the in-cluster endpoint, and various service info |
| 120 | +cat <<EOF |
| 121 | +
|
| 122 | +ClickHouse installed. |
| 123 | +
|
| 124 | +In-cluster HTTP endpoint: |
| 125 | + http://clickhouse-http.${NS}.svc.cluster.local:8123 |
| 126 | +
|
| 127 | +Example queries: |
| 128 | + kubectl -n ${NS} port-forward svc/clickhouse-http 8123:8123 & |
| 129 | + curl -s "http://localhost:8123/?user=reader&password=${READER_PLAIN}&query=SELECT%201" |
| 130 | +
|
| 131 | +Users (from Secret clickhouse-db-passwords): |
| 132 | + reader / ${READER_PLAIN} |
| 133 | + writer / ${WRITER_PLAIN} |
| 134 | +
|
| 135 | +To rotate passwords: update the Secret and bump taskID in chi-cluster.yaml (or patch): |
| 136 | + kubectl -n ${NS} patch chi ch --type=merge -p '{"spec":{"taskID":"2"}}' |
| 137 | +
|
| 138 | +EOF |
0 commit comments