Skip to content

Commit

Permalink
fix: additional prebackuppod changes (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Oct 26, 2023
1 parent f367c37 commit 7ac98ae
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 32 deletions.
42 changes: 22 additions & 20 deletions internal/templating/backups/template_prebackuppod.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ func GeneratePreBackupPod(
"lagoon.sh/version": lValues.LagoonVersion,
}

// add any additional labels
additionalLabels := map[string]string{}
additionalAnnotations := map[string]string{}
if lValues.BuildType == "branch" {
additionalAnnotations["lagoon.sh/branch"] = lValues.Branch
} else if lValues.BuildType == "pullrequest" {
additionalAnnotations["lagoon.sh/prNumber"] = lValues.PRNumber
additionalAnnotations["lagoon.sh/prHeadBranch"] = lValues.PRHeadBranch
additionalAnnotations["lagoon.sh/prBaseBranch"] = lValues.PRBaseBranch

}

// create the prebackuppods
for _, serviceValues := range lValues.Services {
// add any additional labels
additionalLabels := map[string]string{}
additionalAnnotations := map[string]string{}
if lValues.BuildType == "branch" {
additionalAnnotations["lagoon.sh/branch"] = lValues.Branch
} else if lValues.BuildType == "pullrequest" {
additionalAnnotations["lagoon.sh/prNumber"] = lValues.PRNumber
additionalAnnotations["lagoon.sh/prHeadBranch"] = lValues.PRHeadBranch
additionalAnnotations["lagoon.sh/prBaseBranch"] = lValues.PRBaseBranch
}
additionalLabels["app.kubernetes.io/name"] = serviceValues.Type
additionalLabels["app.kubernetes.io/instance"] = serviceValues.Name
additionalLabels["lagoon.sh/service"] = serviceValues.Name
additionalLabels["lagoon.sh/service-type"] = serviceValues.Type
if _, ok := preBackupPodSpecs[serviceValues.Type]; ok {
switch lValues.Backup.K8upVersion {
case "v1":
Expand Down Expand Up @@ -94,10 +96,10 @@ func GeneratePreBackupPod(

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",
Name: "BACKUP_DB_READREPLICA_HOSTS",
ValueFrom: &v1.EnvVarSource{
ConfigMapKeyRef: &v1.ConfigMapKeySelector{
Key: fmt.Sprintf("%s_READREPLICAS", varFix(serviceValues.OverrideName)),
Key: fmt.Sprintf("%s_READREPLICA_HOSTS", varFix(serviceValues.OverrideName)),
LocalObjectReference: v1.LocalObjectReference{
Name: "lagoon-env",
},
Expand Down Expand Up @@ -180,10 +182,10 @@ func GeneratePreBackupPod(

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",
Name: "BACKUP_DB_READREPLICA_HOSTS",
ValueFrom: &v1.EnvVarSource{
ConfigMapKeyRef: &v1.ConfigMapKeySelector{
Key: fmt.Sprintf("%s_READREPLICAS", varFix(serviceValues.OverrideName)),
Key: fmt.Sprintf("%s_READREPLICA_HOSTS", varFix(serviceValues.OverrideName)),
LocalObjectReference: v1.LocalObjectReference{
Name: "lagoon-env",
},
Expand Down Expand Up @@ -261,8 +263,8 @@ 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: >
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICAS ]; then
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICAS | cut -d ',' -f1);
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | cut -d ',' -f1);
fi &&
dump=$(mktemp)
&& mysqldump --max-allowed-packet=500M --events --routines --quick
Expand Down Expand Up @@ -315,8 +317,8 @@ pod:
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);
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | cut -d ',' -f1);
fi && PGPASSWORD=$BACKUP_DB_PASSWORD pg_dump
--host=$BACKUP_DB_HOST
--port=$BACKUP_DB_PORT
Expand Down
35 changes: 35 additions & 0 deletions internal/templating/backups/template_prebackuppod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ func TestGeneratePreBackupPod(t *testing.T) {
},
want: "test-resources/result-prebackuppod4.yaml",
},
{
name: "test5 - multiple 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,
},
{
Name: "mariadb",
OverrideName: "mariadb",
Type: "mariadb-dbaas",
DBaaSEnvironment: "development",
DBaasReadReplica: true,
},
},
Backup: generator.BackupConfiguration{
K8upVersion: "v2",
},
},
},
want: "test-resources/result-prebackuppod5.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 @@ -7,16 +7,20 @@ metadata:
lagoon.sh/version: v2.x.x
creationTimestamp: null
labels:
app.kubernetes.io/instance: mariadb-database
app.kubernetes.io/managed-by: build-deploy-tool
app.kubernetes.io/name: mariadb-dbaas
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: mariadb-database
lagoon.sh/service-type: mariadb-dbaas
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"
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | 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 Expand Up @@ -46,10 +50,10 @@ spec:
configMapKeyRef:
key: MARIADB_DATABASE_DATABASE
name: lagoon-env
- name: BACKUP_DB_READREPLICAS
- name: BACKUP_DB_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_READREPLICAS
key: MARIADB_DATABASE_READREPLICA_HOSTS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ metadata:
lagoon.sh/version: v2.x.x
creationTimestamp: null
labels:
app.kubernetes.io/instance: postgres-database
app.kubernetes.io/managed-by: build-deploy-tool
app.kubernetes.io/name: postgres-dbaas
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: postgres-database
lagoon.sh/service-type: postgres-dbaas
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"
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | 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 Expand Up @@ -46,10 +50,10 @@ spec:
configMapKeyRef:
key: POSTGRES_DATABASE_DATABASE
name: lagoon-env
- name: BACKUP_DB_READREPLICAS
- name: BACKUP_DB_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
key: POSTGRES_DATABASE_READREPLICAS
key: POSTGRES_DATABASE_READREPLICA_HOSTS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ metadata:
lagoon.sh/version: v2.x.x
creationTimestamp: null
labels:
app.kubernetes.io/instance: mongodb-database
app.kubernetes.io/managed-by: build-deploy-tool
app.kubernetes.io/name: mongodb-dbaas
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: mongodb-database
lagoon.sh/service-type: mongodb-dbaas
prebackuppod: mongodb-database
name: mongodb-database-prebackuppod
spec:
Expand Down Expand Up @@ -46,10 +50,10 @@ spec:
configMapKeyRef:
key: MONGODB_DATABASE_DATABASE
name: lagoon-env
- name: BACKUP_DB_READREPLICAS
- name: BACKUP_DB_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
key: MONGODB_DATABASE_READREPLICAS
key: MONGODB_DATABASE_READREPLICA_HOSTS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ metadata:
lagoon.sh/version: v2.x.x
creationTimestamp: null
labels:
app.kubernetes.io/instance: mariadb-database
app.kubernetes.io/managed-by: build-deploy-tool
app.kubernetes.io/name: mariadb-dbaas
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: mariadb-database
lagoon.sh/service-type: mariadb-dbaas
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"
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | 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 Expand Up @@ -46,10 +50,10 @@ spec:
configMapKeyRef:
key: MARIADB_DATABASE_DATABASE
name: lagoon-env
- name: BACKUP_DB_READREPLICAS
- name: BACKUP_DB_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_READREPLICAS
key: MARIADB_DATABASE_READREPLICA_HOSTS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
Expand Down
122 changes: 122 additions & 0 deletions internal/templating/backups/test-resources/result-prebackuppod5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
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/instance: mariadb-database
app.kubernetes.io/managed-by: build-deploy-tool
app.kubernetes.io/name: mariadb-dbaas
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: mariadb-database
lagoon.sh/service-type: mariadb-dbaas
prebackuppod: mariadb-database
name: mariadb-database-prebackuppod
spec:
backupCommand: |
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | 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_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE_READREPLICA_HOSTS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
name: mariadb-database-prebackuppod
resources: {}
---
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/instance: mariadb
app.kubernetes.io/managed-by: build-deploy-tool
app.kubernetes.io/name: mariadb-dbaas
lagoon.sh/buildType: branch
lagoon.sh/environment: environment-with-really-really-reall-3fdb
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: mariadb
lagoon.sh/service-type: mariadb-dbaas
prebackuppod: mariadb
name: mariadb-prebackuppod
spec:
backupCommand: |
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | 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.sql
pod:
metadata: {}
spec:
containers:
- args:
- sleep
- infinity
env:
- name: BACKUP_DB_HOST
valueFrom:
configMapKeyRef:
key: MARIADB_HOST
name: lagoon-env
- name: BACKUP_DB_USERNAME
valueFrom:
configMapKeyRef:
key: MARIADB_USERNAME
name: lagoon-env
- name: BACKUP_DB_PASSWORD
valueFrom:
configMapKeyRef:
key: MARIADB_PASSWORD
name: lagoon-env
- name: BACKUP_DB_DATABASE
valueFrom:
configMapKeyRef:
key: MARIADB_DATABASE
name: lagoon-env
- name: BACKUP_DB_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
key: MARIADB_READREPLICA_HOSTS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
name: mariadb-prebackuppod
resources: {}
Loading

0 comments on commit 7ac98ae

Please sign in to comment.