diff --git a/clickhouse/.gitignore b/clickhouse/.gitignore new file mode 100644 index 0000000..402a194 --- /dev/null +++ b/clickhouse/.gitignore @@ -0,0 +1,2 @@ +charts/ +!charts/databases/ \ No newline at end of file diff --git a/clickhouse/charts/databases/.helmignore b/clickhouse/charts/databases/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/clickhouse/charts/databases/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/clickhouse/charts/databases/Chart.yaml b/clickhouse/charts/databases/Chart.yaml new file mode 100644 index 0000000..0e03292 --- /dev/null +++ b/clickhouse/charts/databases/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: clickhouse databases +description: declare clickhouse databases + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "24.12.6.70-alpine" diff --git a/clickhouse/charts/databases/templates/init-databases-job.yaml b/clickhouse/charts/databases/templates/init-databases-job.yaml new file mode 100644 index 0000000..9536bc0 --- /dev/null +++ b/clickhouse/charts/databases/templates/init-databases-job.yaml @@ -0,0 +1,83 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: clickhouse-db-init + labels: + app.kubernetes.io/name: clickhouse-db-init + app.kubernetes.io/instance: {{ .Release.Name }} +spec: + template: + spec: + restartPolicy: OnFailure + containers: + - name: clickhouse-init + image: clickhouse/clickhouse-server:{{ .Chart.AppVersion }} + command: ["bash", "-c"] + args: + - | + echo "⏳ Waiting for ClickHouse..." + until clickhouse-client \ + --host {{ .Values.clickhouse.host }} \ + --port {{ .Values.clickhouse.port }} \ + --user {{ .Values.clickhouse.adminUser }} \ + --password "{{ .Values.clickhouse.adminPassword }}" \ + -q "SELECT 1" >/dev/null 2>&1; do + sleep 2 + done + echo "✅ ClickHouse ready" + + {{- if .Values.databases }} + echo "➡️ Creating declared databases & users..." + clickhouse-client \ + --host {{ .Values.clickhouse.host }} \ + --port {{ .Values.clickhouse.port }} \ + --user {{ .Values.clickhouse.adminUser }} \ + --password "{{ .Values.clickhouse.adminPassword }}" \ + --multiquery < /config/init.sql + {{- end }} + + echo "➡️ Generating list of databases to drop..." + clickhouse-client \ + --host {{ .Values.clickhouse.host }} \ + --port {{ .Values.clickhouse.port }} \ + --user {{ .Values.clickhouse.adminUser }} \ + --password "{{ .Values.clickhouse.adminPassword }}" \ + -q " + SELECT concat('DROP DATABASE IF EXISTS ', name, ';') + FROM system.databases + WHERE name NOT IN ( + 'system', + 'information_schema', + 'INFORMATION_SCHEMA', + 'default' + {{- if .Values.databases }} + {{- range $db := .Values.databases }} + , '{{ $db }}' + {{- end }} + {{- end }} + ); + " > /tmp/to_drop.sql + + if [ -s /tmp/to_drop.sql ]; then + echo "➡️ Dropping leftover databases:" + cat /tmp/to_drop.sql + + clickhouse-client \ + --host {{ .Values.clickhouse.host }} \ + --port {{ .Values.clickhouse.port }} \ + --user {{ .Values.clickhouse.adminUser }} \ + --password "{{ .Values.clickhouse.adminPassword }}" \ + --multiquery < /tmp/to_drop.sql + + else + echo "✔️ No databases to drop." + fi + + echo "🎉 Initialization completed" + volumeMounts: + - name: init-sql + mountPath: /config + volumes: + - name: init-sql + configMap: + name: clickhouse-init-sql diff --git a/clickhouse/charts/databases/templates/init-sql-configmap.yaml b/clickhouse/charts/databases/templates/init-sql-configmap.yaml new file mode 100644 index 0000000..a647f47 --- /dev/null +++ b/clickhouse/charts/databases/templates/init-sql-configmap.yaml @@ -0,0 +1,24 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: clickhouse-init-sql +data: + init.sql: | + -- This file is auto-generated by Helm + -- Databases and users initialization + + {{- range $db := .Values.databases }} + + -- Database: {{ $db }} + CREATE DATABASE IF NOT EXISTS {{ $db }}; + + -- User: {{ $db }} + CREATE USER IF NOT EXISTS {{ $db }} + IDENTIFIED BY '{{ $db }}arcodange'; + + -- Privileges + GRANT CREATE, SELECT, INSERT, ALTER, DROP + ON {{ $db }}.* + TO {{ $db }}; + + {{- end }} diff --git a/clickhouse/charts/databases/values.yaml b/clickhouse/charts/databases/values.yaml new file mode 100644 index 0000000..4de6643 --- /dev/null +++ b/clickhouse/charts/databases/values.yaml @@ -0,0 +1,8 @@ +clickhouse: + host: clickhouse.tools + port: 9000 + adminUser: arcodange + adminPassword: clickhousearcodange + +databases: + - plausible \ No newline at end of file diff --git a/clickhouse/kustomization.yaml b/clickhouse/kustomization.yaml index 8aa7938..6559713 100644 --- a/clickhouse/kustomization.yaml +++ b/clickhouse/kustomization.yaml @@ -1,12 +1,17 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization +helmGlobals: + chartHome: charts + helmCharts: - name: clickhouse repo: https://charts.pascaliske.dev version: 0.4.0 releaseName: clickhouse valuesFile: clickhouseValues.yaml +- name: databases + releaseName: clickhouse-databases patches: - target: