Skip to content

Commit

Permalink
fix: backupcommand for mariadb-dbaas
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Oct 3, 2023
1 parent f3c6aa5 commit 3198d1a
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 109 deletions.
30 changes: 30 additions & 0 deletions cmd/template_backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
backuptemplate "github.com/uselagoon/build-deploy-tool/internal/templating/backups"
"sigs.k8s.io/yaml"
)

type readReplicaValues struct {
ReadReplicaHosts string `json:"readReplicaHosts"`
}

var backupGeneration = &cobra.Command{
Use: "backup-schedule",
Aliases: []string{"schedule", "bs"},
Expand Down Expand Up @@ -38,6 +44,30 @@ func BackupTemplateGeneration(g generator.GeneratorInput,
}
savedTemplates := g.SavedTemplatesPath

// TODO: the dbaas consumers aren't known when the generator runs currently
// so this is a small helper function to collect this from the build stage
// this will eventually need to be collected directly by the generator or some other component
// of the generator in the future, but since backups are the only thing that need to know this at this stage
repServices := []generator.ServiceValues{}
for _, s := range lagoonBuild.BuildValues.Services {
rawYAML, err := os.ReadFile(fmt.Sprintf("/kubectl-build-deploy/%s-values.yaml", s.Name))
if err != nil {
repServices = append(repServices, s)
// skip this one
continue
}
dbaasValues := &readReplicaValues{}
err = yaml.Unmarshal(rawYAML, dbaasValues)
if err != nil {
return fmt.Errorf("couldn't read %v: %v", fmt.Sprintf("/kubectl-build-deploy/%s-values.yaml", s.Name), err)
}
if dbaasValues.ReadReplicaHosts != "" {
s.DBaasReadReplica = true
}
repServices = append(repServices, s)
}
lagoonBuild.BuildValues.Services = repServices

// generate the backup schedule templates
templateYAML, err := backuptemplate.GenerateBackupSchedule(*lagoonBuild.BuildValues)
if err != nil {
Expand Down
47 changes: 33 additions & 14 deletions internal/templating/backups/template_prebackuppod.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func GeneratePreBackupPod(

prebackuppod.Spec = k8upPBPSpec

if prebackuppod.Spec.Pod.Spec.Containers[0].EnvFrom == nil {
if prebackuppod.Spec.Pod.Spec.Containers[0].EnvFrom == nil && serviceValues.DBaasReadReplica {
prebackuppod.Spec.Pod.Spec.Containers[0].Env = append(prebackuppod.Spec.Pod.Spec.Containers[0].Env, v1.EnvVar{
Name: "BACKUP_DB_READREPLICAS",
ValueFrom: &v1.EnvVarSource{
Expand Down Expand Up @@ -173,7 +173,7 @@ func GeneratePreBackupPod(
Name: "BACKUP_DB_READREPLICAS",
ValueFrom: &v1.EnvVarSource{
ConfigMapKeyRef: &v1.ConfigMapKeySelector{
Key: fmt.Sprintf("%s_READREPLICAS", serviceValues.OverrideName),
Key: fmt.Sprintf("%s_READREPLICAS", varFix(serviceValues.OverrideName)),
LocalObjectReference: v1.LocalObjectReference{
Name: "lagoon-env",
},
Expand Down Expand Up @@ -250,18 +250,29 @@ type PreBackupPods map[string]string

// this is just the first run at doing this, once the service template generator is introduced, this will need to be re-evaluated
var preBackupPodSpecs = PreBackupPods{
"mariadb-dbaas": `backupCommand: >-
"mariadb-dbaas": `backupCommand: >
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1)
fi && dump=$(mktemp) && mysqldump --max-allowed-packet=500M --events
--routines --quick --add-locks --no-autocommit --single-transaction
--no-create-db --no-data -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME
-p$BACKUP_DB_PASSWORD $BACKUP_DB_DATABASE > $dump && mysqldump
--max-allowed-packet=500M --events --routines --quick --add-locks
--no-autocommit --single-transaction --no-create-db
--ignore-table=$BACKUP_DB_DATABASE.watchdog --no-create-info -h
$BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD
$BACKUP_DB_DATABASE >> $dump && cat $dump && rm $dump"
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1);
fi &&
dump=$(mktemp)
&& mysqldump --max-allowed-packet=500M --events --routines --quick
--add-locks --no-autocommit --single-transaction --no-create-db
--no-data --no-tablespaces
-h $BACKUP_DB_HOST
-u $BACKUP_DB_USERNAME
-p$BACKUP_DB_PASSWORD
$BACKUP_DB_DATABASE
> $dump
&& mysqldump --max-allowed-packet=500M --events --routines --quick
--add-locks --no-autocommit --single-transaction --no-create-db
--ignore-table=$BACKUP_DB_DATABASE.watchdog
--no-create-info --no-tablespaces --skip-triggers
-h $BACKUP_DB_HOST
-u $BACKUP_DB_USERNAME
-p$BACKUP_DB_PASSWORD
$BACKUP_DB_DATABASE
>> $dump
&& cat $dump && rm $dump"
fileExtension: .{{ .Name }}.sql
pod:
spec:
Expand Down Expand Up @@ -293,7 +304,15 @@ pod:
image: imagecache.amazeeio.cloud/amazeeio/alpine-mysql-client
imagePullPolicy: Always
name: {{ .Name }}-prebackuppod`,
"postgres-dbaas": `backupCommand: /bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1); fi && PGPASSWORD=$BACKUP_DB_PASSWORD pg_dump --host=$BACKUP_DB_HOST --port=$BACKUP_DB_PORT --dbname=$BACKUP_DB_NAME --username=$BACKUP_DB_USERNAME --format=t -w"
"postgres-dbaas": `backupCommand: >
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1);
fi && PGPASSWORD=$BACKUP_DB_PASSWORD pg_dump
--host=$BACKUP_DB_HOST
--port=$BACKUP_DB_PORT
--dbname=$BACKUP_DB_NAME
--username=$BACKUP_DB_USERNAME
--format=t -w"
fileExtension: .{{ .Name }}.tar
pod:
spec:
Expand Down
28 changes: 28 additions & 0 deletions internal/templating/backups/template_prebackuppod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ func TestGeneratePreBackupPod(t *testing.T) {
},
want: "test-resources/result-prebackuppod6.yaml",
},
{
name: "test7 - k8up/v1",
args: args{
lValues: generator.BuildValues{
Project: "example-project",
Environment: "environment-with-really-really-reall-3fdb",
EnvironmentType: "production",
Namespace: "myexample-project-environment-with-really-really-reall-3fdb",
BuildType: "branch",
LagoonVersion: "v2.x.x",
Kubernetes: "generator.local",
Branch: "environment-with-really-really-reall-3fdb",
Services: []generator.ServiceValues{
{
Name: "mariadb-database",
OverrideName: "mariadb-database",
Type: "mariadb-dbaas",
DBaaSEnvironment: "development",
DBaasReadReplica: true,
},
},
Backup: generator.BackupConfiguration{
K8upVersion: "v2",
},
},
},
want: "test-resources/result-prebackuppod7.yaml",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@ metadata:
prebackuppod: mariadb-database
name: mariadb-database-prebackuppod
spec:
backupCommand: /bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then BACKUP_DB_HOST=$(echo
$BACKUP_DB_READREPLICAS | cut -d ',' -f1) fi && dump=$(mktemp) && mysqldump --max-allowed-packet=500M
--events --routines --quick --add-locks --no-autocommit --single-transaction --no-create-db
--no-data -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD $BACKUP_DB_DATABASE
> $dump && mysqldump --max-allowed-packet=500M --events --routines --quick --add-locks
--no-autocommit --single-transaction --no-create-db --ignore-table=$BACKUP_DB_DATABASE.watchdog
--no-create-info -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD
$BACKUP_DB_DATABASE >> $dump && cat $dump && rm $dump"
backupCommand: |
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1); fi && dump=$(mktemp) && mysqldump --max-allowed-packet=500M --events --routines --quick --add-locks --no-autocommit --single-transaction --no-create-db --no-data --no-tablespaces -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD $BACKUP_DB_DATABASE > $dump && mysqldump --max-allowed-packet=500M --events --routines --quick --add-locks --no-autocommit --single-transaction --no-create-db --ignore-table=$BACKUP_DB_DATABASE.watchdog --no-create-info --no-tablespaces --skip-triggers -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD $BACKUP_DB_DATABASE >> $dump && cat $dump && rm $dump"
fileExtension: .mariadb-database.sql
pod:
metadata: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ metadata:
prebackuppod: postgres-database
name: postgres-database-prebackuppod
spec:
backupCommand: /bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then BACKUP_DB_HOST=$(echo
$BACKUP_DB_READREPLICAS | cut -d ',' -f1); fi && PGPASSWORD=$BACKUP_DB_PASSWORD
pg_dump --host=$BACKUP_DB_HOST --port=$BACKUP_DB_PORT --dbname=$BACKUP_DB_NAME
--username=$BACKUP_DB_USERNAME --format=t -w"
backupCommand: |
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1); fi && PGPASSWORD=$BACKUP_DB_PASSWORD pg_dump --host=$BACKUP_DB_HOST --port=$BACKUP_DB_PORT --dbname=$BACKUP_DB_NAME --username=$BACKUP_DB_USERNAME --format=t -w"
fileExtension: .postgres-database.tar
pod:
metadata: {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
apiVersion: k8up.io/v1
kind: PreBackupPod
metadata:
annotations:
lagoon.sh/branch: environment-with-really-really-reall-3fdb
lagoon.sh/version: v2.x.x
creationTimestamp: null
labels:
app.kubernetes.io/managed-by: build-deploy-tool
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
prebackuppod: mariadb-database
name: mariadb-database-prebackuppod
spec:
backupCommand: |
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1); fi && dump=$(mktemp) && mysqldump --max-allowed-packet=500M --events --routines --quick --add-locks --no-autocommit --single-transaction --no-create-db --no-data --no-tablespaces -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD $BACKUP_DB_DATABASE > $dump && mysqldump --max-allowed-packet=500M --events --routines --quick --add-locks --no-autocommit --single-transaction --no-create-db --ignore-table=$BACKUP_DB_DATABASE.watchdog --no-create-info --no-tablespaces --skip-triggers -h $BACKUP_DB_HOST -u $BACKUP_DB_USERNAME -p$BACKUP_DB_PASSWORD $BACKUP_DB_DATABASE >> $dump && cat $dump && rm $dump"
fileExtension: .mariadb-database.sql
pod:
metadata: {}
spec:
containers:
- args:
- sleep
- infinity
env:
- name: BACKUP_DB_HOST
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_HOST
name: lagoon-env
- name: BACKUP_DB_USERNAME
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_USERNAME
name: lagoon-env
- name: BACKUP_DB_PASSWORD
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_PASSWORD
name: lagoon-env
- name: BACKUP_DB_DATABASE
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_DATABASE
name: lagoon-env
- name: BACKUP_DB_READREPLICAS
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_READREPLICAS
name: lagoon-env
image: imagecache.amazeeio.cloud/amazeeio/alpine-mysql-client
imagePullPolicy: Always
name: mariadb-database-prebackuppod
resources: {}
Loading

0 comments on commit 3198d1a

Please sign in to comment.