generated from cloudoperators/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat(deployment): Regular DB maintenance #401
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
This file contains 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,25 @@ | ||
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
FROM alpine:3.20 | ||
|
||
# Install mysql-client and bash (for scripts) | ||
RUN apk add --no-cache mysql-client bash | ||
|
||
# Create non-root user | ||
RUN addgroup -g 1000 dbuser && \ | ||
adduser -u 1000 -G dbuser -s /bin/bash -D dbuser | ||
|
||
# Create directory for scripts | ||
WORKDIR /scripts | ||
COPY scripts/* /scripts/ | ||
|
||
# Set permissions | ||
RUN chown -R dbuser:dbuser /scripts && \ | ||
chmod -R 755 /scripts | ||
|
||
# Switch to non-root user | ||
USER dbuser | ||
|
||
# Entry point script | ||
ENTRYPOINT ["/scripts/entrypoint.sh"] |
This file contains 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,15 @@ | ||
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
services: | ||
maintenance: | ||
build: . | ||
image: db-maintenance:latest | ||
container_name: heureka_maintenance | ||
environment: | ||
DB_HOST: host.docker.internal | ||
DB_NAME: ${DB_NAME} | ||
DB_USER: ${DB_USER} | ||
DB_PASSWORD: ${DB_PASSWORD} | ||
volumes: | ||
- ./scripts:/scripts |
This file contains 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,17 @@ | ||
#!/bin/bash | ||
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
set -e | ||
|
||
# Validate environment variables | ||
if [ -z "$DB_HOST" ] || [ -z "$DB_NAME" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ]; then | ||
echo "Error: Required environment variables are not set" | ||
exit 1 | ||
fi | ||
|
||
# Execute the maintenance script | ||
echo "Starting database maintenance at $(date)" | ||
mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" < /scripts/maintenance.sql | ||
|
||
echo "Database maintenance completed at $(date)" |
This file contains 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,47 @@ | ||
-- SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
|
||
-- Maintenance script for database truncation | ||
-- Enable logging | ||
SET @start_time = NOW(); | ||
SELECT CONCAT('Starting maintenance at ', @start_time) AS log_message; | ||
|
||
-- Start transaction | ||
START TRANSACTION; | ||
|
||
-- Disable foreign key checks temporarily | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
-- Truncate junction join tables first | ||
TRUNCATE TABLE IssueMatchEvidence; | ||
TRUNCATE TABLE ComponentVersionIssue; | ||
TRUNCATE TABLE IssueRepositoryService; | ||
TRUNCATE TABLE IssueMatchChange; | ||
TRUNCATE TABLE SupportGroupService; | ||
TRUNCATE TABLE SupportGroupUser; | ||
TRUNCATE TABLE Owner; | ||
|
||
-- Truncate dependent entity tables | ||
TRUNCATE TABLE IssueMatch; | ||
TRUNCATE TABLE Evidence; | ||
TRUNCATE TABLE ComponentInstance; | ||
-- TRUNCATE TABLE IssueVariant; | ||
TRUNCATE TABLE ComponentVersion; | ||
TRUNCATE TABLE Activity; | ||
|
||
-- Truncate main entity tables | ||
TRUNCATE TABLE Component; | ||
TRUNCATE TABLE Service; | ||
TRUNCATE TABLE SupportGroup; | ||
-- TRUNCATE TABLE Issue; | ||
-- TRUNCATE TABLE IssueRepository; | ||
|
||
-- Re-enable foreign key checks | ||
SET FOREIGN_KEY_CHECKS = 1; | ||
|
||
-- Log completion | ||
SELECT CONCAT('Maintenance completed at ', NOW(), '. Duration: ', | ||
TIMESTAMPDIFF(SECOND, @start_time, NOW()), ' seconds') AS log_message; | ||
|
||
-- Commit transaction | ||
COMMIT; |
This file contains 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,39 @@ | ||
{{- if .Values.dbMaintenance.enabled }} | ||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: {{ include "heureka.fullname" . }}-db-maintenance | ||
labels: | ||
{{- include "heureka.labels" . | nindent 4 }} | ||
app.kubernetes.io/component: db-maintenance | ||
spec: | ||
schedule: {{ .Values.dbMaintenance.schedule | quote }} | ||
concurrencyPolicy: Forbid | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
securityContext: | ||
runAsNonRoot: true | ||
runAsUser: 1000 | ||
runAsGroup: 1000 | ||
containers: | ||
- name: db-maintenance | ||
image: {{ .Values.dbMaintenance.image.repository }}:{{ .Values.dbMaintenance.image.tag }} | ||
env: | ||
- name: DB_HOST | ||
value: {{ .Values.mariadb.fullnameOverride | default (printf "%s-mariadb" (include "heureka.fullname" .)) }} | ||
- name: DB_NAME | ||
value: {{ .Values.mariadb.auth.database }} | ||
- name: DB_USER | ||
valueFrom: | ||
secretKeyRef: | ||
name: heureka | ||
key: mariadb-username | ||
- name: DB_PASSWORD | ||
valueFrom: | ||
secretKeyRef: | ||
name: heureka | ||
key: mariadb-password | ||
restartPolicy: OnFailure | ||
{{- end }} |
This file contains 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
This file contains 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
Oops, something went wrong.
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.
Need to discuss schedule interval.
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.
Thats fine from my perspective once a minute is a bit often though....
Maybe you rather say once at 1 : 30 at night per default:
schedule: "30 1 * * *"