-
Notifications
You must be signed in to change notification settings - Fork 559
feat(clickhouse): db cleanup #1876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Mokto
merged 1 commit into
sentry-kubernetes:develop
from
TartanLeGrand:feat/clickhouse/add-cleanup
Sep 15, 2025
+314
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
242 changes: 242 additions & 0 deletions
242
charts/sentry/templates/snuba/cleanup/cronjob-clickhouse-cleanup.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
{{- if .Values.snuba.cleanup.enabled }} | ||
{{- $batchApiIsStable := eq (include "sentry.batch.isStable" .) "true" -}} | ||
apiVersion: {{ include "sentry.batch.apiVersion" . }} | ||
kind: CronJob | ||
metadata: | ||
name: {{ template "sentry.fullname" . }}-clickhouse-cleanup | ||
labels: | ||
app: {{ template "sentry.fullname" . }} | ||
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" | ||
release: "{{ .Release.Name }}" | ||
heritage: "{{ .Release.Service }}" | ||
spec: | ||
schedule: "{{ .Values.snuba.cleanup.schedule }}" | ||
successfulJobsHistoryLimit: {{ .Values.snuba.cleanup.successfulJobsHistoryLimit }} | ||
failedJobsHistoryLimit: {{ .Values.snuba.cleanup.failedJobsHistoryLimit }} | ||
concurrencyPolicy: "{{ .Values.snuba.cleanup.concurrencyPolicy }}" | ||
jobTemplate: | ||
spec: | ||
{{- if .Values.snuba.cleanup.activeDeadlineSeconds }} | ||
activeDeadlineSeconds: {{ .Values.snuba.cleanup.activeDeadlineSeconds }} | ||
{{- end}} | ||
template: | ||
metadata: | ||
annotations: | ||
checksum/configYml: {{ .Values.config.configYml | toYaml | toString | sha256sum }} | ||
checksum/config.yaml: {{ include "snuba.config" . | sha256sum }} | ||
{{- if .Values.snuba.cleanup.annotations }} | ||
{{ toYaml .Values.snuba.cleanup.annotations | indent 12 }} | ||
{{- end }} | ||
labels: | ||
app: {{ template "sentry.fullname" . }} | ||
release: "{{ .Release.Name }}" | ||
{{- if .Values.snuba.cleanup.podLabels }} | ||
{{ toYaml .Values.snuba.cleanup.podLabels | indent 12 }} | ||
{{- end }} | ||
spec: | ||
{{- if .Values.snuba.cleanup.affinity }} | ||
affinity: | ||
{{ toYaml .Values.snuba.cleanup.affinity | indent 12 }} | ||
{{- end }} | ||
{{- if .Values.snuba.cleanup.nodeSelector }} | ||
nodeSelector: | ||
{{ toYaml .Values.snuba.cleanup.nodeSelector | indent 12 }} | ||
{{- else if .Values.global.nodeSelector }} | ||
nodeSelector: | ||
{{ toYaml .Values.global.nodeSelector | indent 12 }} | ||
{{- end }} | ||
{{- if .Values.snuba.cleanup.tolerations }} | ||
tolerations: | ||
{{ toYaml .Values.snuba.cleanup.tolerations | indent 12 }} | ||
{{- else if .Values.global.tolerations }} | ||
tolerations: | ||
{{ toYaml .Values.global.tolerations | indent 12 }} | ||
{{- end }} | ||
{{- if .Values.dnsPolicy }} | ||
dnsPolicy: {{ .Values.dnsPolicy | quote }} | ||
{{- end }} | ||
{{- if .Values.dnsConfig }} | ||
dnsConfig: | ||
{{ toYaml .Values.dnsConfig | indent 12 }} | ||
{{- end }} | ||
{{- if .Values.images.snuba.imagePullSecrets }} | ||
imagePullSecrets: | ||
{{ toYaml .Values.images.snuba.imagePullSecrets | indent 12 }} | ||
{{- end }} | ||
{{- if .Values.snuba.cleanup.securityContext }} | ||
securityContext: | ||
{{ toYaml .Values.snuba.cleanup.securityContext | indent 12 }} | ||
{{- end }} | ||
containers: | ||
- name: {{ .Chart.Name }}-clickhouse-cleanup | ||
image: clickhouse/clickhouse-client:latest | ||
imagePullPolicy: IfNotPresent | ||
command: ["/bin/bash"] | ||
args: | ||
- "-c" | ||
- | | ||
set -e | ||
echo "Starting ClickHouse cleanup with retention period of {{ .Values.snuba.cleanup.retentionDays }} days" | ||
echo "Connecting to ClickHouse at $CLICKHOUSE_HOST:$CLICKHOUSE_PORT" | ||
# Function to discover tables | ||
discover_tables() { | ||
echo "Auto-detecting tables in ClickHouse database..." | ||
# Get all tables from the database | ||
ALL_TABLES=$(clickhouse-client \ | ||
--host="$CLICKHOUSE_HOST" \ | ||
--port="$CLICKHOUSE_PORT" \ | ||
--user="$CLICKHOUSE_USER" \ | ||
--database="$CLICKHOUSE_DATABASE" \ | ||
--password="$CLICKHOUSE_PASSWORD" \ | ||
--query="SHOW TABLES" 2>/dev/null || echo "") | ||
if [ -z "$ALL_TABLES" ]; then | ||
echo "Warning: Could not retrieve table list, falling back to hardcoded list" | ||
return 1 | ||
fi | ||
echo "Found tables: $ALL_TABLES" | ||
# Filter tables based on include/exclude patterns | ||
DISCOVERED_TABLES=() | ||
{{- range .Values.snuba.cleanup.tables.includePatterns }} | ||
INCLUDE_PATTERN="{{ . }}" | ||
{{- end }} | ||
{{- range .Values.snuba.cleanup.tables.excludePatterns }} | ||
EXCLUDE_PATTERN="{{ . }}" | ||
{{- end }} | ||
for table in $ALL_TABLES; do | ||
# Check include patterns | ||
INCLUDE_MATCH=false | ||
{{- range .Values.snuba.cleanup.tables.includePatterns }} | ||
if echo "$table" | grep -E "{{ . }}" >/dev/null 2>&1; then | ||
INCLUDE_MATCH=true | ||
fi | ||
{{- end }} | ||
# Check exclude patterns | ||
EXCLUDE_MATCH=false | ||
{{- range .Values.snuba.cleanup.tables.excludePatterns }} | ||
if echo "$table" | grep -E "{{ . }}" >/dev/null 2>&1; then | ||
EXCLUDE_MATCH=true | ||
fi | ||
{{- end }} | ||
# Add to discovered tables if it matches include and doesn't match exclude | ||
if [ "$INCLUDE_MATCH" = true ] && [ "$EXCLUDE_MATCH" = false ]; then | ||
DISCOVERED_TABLES+=("$table") | ||
echo "Including table: $table" | ||
else | ||
echo "Excluding table: $table (include=$INCLUDE_MATCH, exclude=$EXCLUDE_MATCH)" | ||
fi | ||
done | ||
if [ ${#DISCOVERED_TABLES[@]} -eq 0 ]; then | ||
echo "Warning: No tables matched the include/exclude patterns, falling back to hardcoded list" | ||
return 1 | ||
fi | ||
# Export discovered tables for cleanup | ||
printf '%s\n' "${DISCOVERED_TABLES[@]}" | ||
return 0 | ||
} | ||
# Determine which tables to clean up | ||
{{- if .Values.snuba.cleanup.tables.autoDetect }} | ||
echo "Table auto-detection enabled" | ||
if TABLES_TO_CLEANUP=$(discover_tables); then | ||
readarray -t TABLES <<< "$TABLES_TO_CLEANUP" | ||
echo "Using auto-detected tables: ${TABLES[*]}" | ||
else | ||
echo "Auto-detection failed, using fallback list" | ||
TABLES=( | ||
{{- range .Values.snuba.cleanup.tables.fallbackList }} | ||
"{{ . }}" | ||
{{- end }} | ||
) | ||
fi | ||
{{- else }} | ||
echo "Table auto-detection disabled, using configured fallback list" | ||
TABLES=( | ||
{{- range .Values.snuba.cleanup.tables.fallbackList }} | ||
"{{ . }}" | ||
{{- end }} | ||
) | ||
{{- end }} | ||
echo "Tables to cleanup: ${TABLES[*]}" | ||
# Execute cleanup for each table | ||
CLEANED_COUNT=0 | ||
FAILED_COUNT=0 | ||
for table in "${TABLES[@]}"; do | ||
if [ -n "$table" ]; then | ||
echo "Cleaning up table: $table" | ||
if clickhouse-client \ | ||
--host="$CLICKHOUSE_HOST" \ | ||
--port="$CLICKHOUSE_PORT" \ | ||
--user="$CLICKHOUSE_USER" \ | ||
--database="$CLICKHOUSE_DATABASE" \ | ||
--password="$CLICKHOUSE_PASSWORD" \ | ||
--query="DELETE FROM $table WHERE timestamp < now() - INTERVAL {{ .Values.snuba.cleanup.retentionDays }} DAY"; then | ||
echo "Successfully cleaned up table: $table" | ||
CLEANED_COUNT=$((CLEANED_COUNT + 1)) | ||
else | ||
echo "Warning: Failed to cleanup table $table (table may not exist or have timestamp column)" | ||
FAILED_COUNT=$((FAILED_COUNT + 1)) | ||
fi | ||
fi | ||
done | ||
echo "ClickHouse cleanup completed: $CLEANED_COUNT tables cleaned, $FAILED_COUNT failed" | ||
env: | ||
- name: CLICKHOUSE_HOST | ||
value: {{ include "sentry.clickhouse.host" . | quote }} | ||
- name: CLICKHOUSE_PORT | ||
value: {{ include "sentry.clickhouse.port" . | quote }} | ||
- name: CLICKHOUSE_USER | ||
value: "default" | ||
- name: CLICKHOUSE_DATABASE | ||
value: "default" | ||
- name: CLICKHOUSE_PASSWORD | ||
valueFrom: | ||
secretKeyRef: | ||
name: {{ template "sentry.fullname" . }}-clickhouse | ||
key: clickhouse-password | ||
optional: true | ||
{{- if .Values.snuba.cleanup.env }} | ||
{{ toYaml .Values.snuba.cleanup.env | indent 12 }} | ||
{{- end }} | ||
resources: | ||
{{ toYaml .Values.snuba.cleanup.resources | indent 14 }} | ||
{{- if .Values.snuba.cleanup.containerSecurityContext }} | ||
securityContext: | ||
{{ toYaml .Values.snuba.cleanup.containerSecurityContext | indent 14 }} | ||
{{- end }} | ||
{{- if .Values.snuba.cleanup.sidecars }} | ||
{{ toYaml .Values.snuba.cleanup.sidecars | indent 10 }} | ||
{{- end }} | ||
{{- if .Values.global.sidecars }} | ||
{{ toYaml .Values.global.sidecars | indent 10 }} | ||
{{- end }} | ||
restartPolicy: Never | ||
{{- if or .Values.snuba.cleanup.volumes .Values.global.volumes }} | ||
volumes: | ||
{{- if .Values.snuba.cleanup.volumes }} | ||
{{ toYaml .Values.snuba.cleanup.volumes | indent 10 }} | ||
{{- end }} | ||
{{- if .Values.global.volumes }} | ||
{{ toYaml .Values.global.volumes | indent 10 }} | ||
{{- end }} | ||
{{- end }} | ||
{{- if .Values.snuba.cleanup.priorityClassName }} | ||
priorityClassName: "{{ .Values.snuba.cleanup.priorityClassName }}" | ||
{{- end }} | ||
{{- if .Values.serviceAccount.enabled }} | ||
serviceAccountName: {{ .Values.serviceAccount.name }}-clickhouse-cleanup | ||
{{- end }} | ||
{{- end }} |
10 changes: 10 additions & 0 deletions
10
charts/sentry/templates/snuba/cleanup/serviceaccount-clickhouse-cleanup.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{{- if .Values.serviceAccount.enabled }} | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: {{ .Values.serviceAccount.name }}-clickhouse-cleanup | ||
{{- if .Values.serviceAccount.annotations }} | ||
annotations: {{ toYaml .Values.serviceAccount.annotations | nindent 4 }} | ||
{{- end }} | ||
automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }} | ||
{{- end }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mokto @TartanLeGrand This should be "sentry.snuba.config"