create plausible clickhouse database
This commit is contained in:
2
clickhouse/.gitignore
vendored
Normal file
2
clickhouse/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
charts/
|
||||
!charts/databases/
|
||||
23
clickhouse/charts/databases/.helmignore
Normal file
23
clickhouse/charts/databases/.helmignore
Normal file
@@ -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/
|
||||
24
clickhouse/charts/databases/Chart.yaml
Normal file
24
clickhouse/charts/databases/Chart.yaml
Normal file
@@ -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: "0.0.0"
|
||||
@@ -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-client:latest
|
||||
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
|
||||
@@ -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 }}
|
||||
8
clickhouse/charts/databases/values.yaml
Normal file
8
clickhouse/charts/databases/values.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
clickhouse:
|
||||
host: clickhouse.tools
|
||||
port: 9000
|
||||
adminUser: arcodange
|
||||
adminPassword: clickhousearcodange
|
||||
|
||||
databases:
|
||||
- plausible
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user