Skip to content

Commit

Permalink
Merge branch 'main' into container-registry-username-override-key
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Nov 27, 2023
2 parents 4ab78f3 + 2d62373 commit 11ca33e
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 56 deletions.
8 changes: 4 additions & 4 deletions internal/generator/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

const (
defaultCheckSchedule = "M H(3-6) * * 0"
defaultPruneSchedule = "M H(3-6) * * 0"
defaultCheckSchedule = "M H(5-8) * * 1"
defaultPruneSchedule = "M H(3-5) * * 0"
defaultBackupSchedule = "M H(22-2) * * *"
hourlyDefaultBackupRetention = 0
dailyDefaultBackupRetention = 7
Expand Down Expand Up @@ -104,7 +104,7 @@ func generateBackupValues(
if flagCheckSchedule == "enabled" {
buildValues.Backup.CheckSchedule = "@weekly-random"
} else {
buildValues.Backup.CheckSchedule, err = helpers.ConvertCrontab(buildValues.Namespace, defaultCheckSchedule)
buildValues.Backup.CheckSchedule, err = helpers.ConvertCrontab(fmt.Sprintf("%s", buildValues.Namespace), defaultCheckSchedule)
if err != nil {
return fmt.Errorf("Unable to convert crontab for default check schedule: %v", err)
}
Expand All @@ -113,7 +113,7 @@ func generateBackupValues(
if flagPruneSchedule == "enabled" {
buildValues.Backup.PruneSchedule = "@weekly-random"
} else {
buildValues.Backup.PruneSchedule, err = helpers.ConvertCrontab(buildValues.Namespace, defaultPruneSchedule)
buildValues.Backup.PruneSchedule, err = helpers.ConvertCrontab(fmt.Sprintf("%s", buildValues.Namespace), defaultPruneSchedule)
if err != nil {
return fmt.Errorf("Unable to convert crontab for default prune schedule: %v", err)
}
Expand Down
32 changes: 16 additions & 16 deletions internal/generator/backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -77,7 +77,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 23 * * 0-5",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -113,7 +113,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 23 * * 0-5",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -147,7 +147,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 23 * * 0-5",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -183,7 +183,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 23 * * 0-5",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -217,7 +217,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 23 * * 0-5",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -253,7 +253,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 23 * * 0-5",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -296,7 +296,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "1,16,31,46 0-23 1-31 1-12 0-6",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -336,7 +336,7 @@ func Test_generateBackupValues(t *testing.T) {
BackupLocationSecretKey: "a1b2c3d4e5f6g7h8i9",
},
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
PruneRetention: PruneRetention{
Hourly: 0,
Expand Down Expand Up @@ -378,7 +378,7 @@ func Test_generateBackupValues(t *testing.T) {
BackupLocationSecretKey: "a1b2c3d4e5f6g7h8i9",
},
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
PruneRetention: PruneRetention{
Hourly: 0,
Expand Down Expand Up @@ -415,7 +415,7 @@ func Test_generateBackupValues(t *testing.T) {
RestoreLocationSecretKey: "a1b2c3d4e5f6g7h8i9",
},
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -462,7 +462,7 @@ func Test_generateBackupValues(t *testing.T) {
RestoreLocationSecretKey: "a1b2c3d4e5f6g7h8i9",
},
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
PruneRetention: PruneRetention{
Hourly: 0,
Expand Down Expand Up @@ -529,7 +529,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-example-project",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -566,7 +566,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "mybucket",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -608,7 +608,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "mybucket",
PruneRetention: PruneRetention{
Expand Down Expand Up @@ -645,7 +645,7 @@ func Test_generateBackupValues(t *testing.T) {
Namespace: "example-com-main",
Backup: BackupConfiguration{
BackupSchedule: "31 1 * * *",
CheckSchedule: "31 4 * * 0",
CheckSchedule: "31 6 * * 1",
PruneSchedule: "31 4 * * 0",
S3BucketName: "baas-cluster-bucket/baas-example-project",
PruneRetention: PruneRetention{
Expand Down
75 changes: 54 additions & 21 deletions internal/templating/backups/template_prebackuppod.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"sigs.k8s.io/yaml"
)

type PreBackupPodTmpl struct {
Service generator.ServiceValues
Namespace string
}

func GeneratePreBackupPod(
lValues generator.BuildValues,
) ([]byte, error) {
Expand Down Expand Up @@ -77,7 +82,11 @@ func GeneratePreBackupPod(

var pbp bytes.Buffer
tmpl, _ := template.New("").Funcs(funcMap).Parse(preBackupPodSpecs[serviceValues.Type])
err := tmpl.Execute(&pbp, serviceValues)
tmplVals := PreBackupPodTmpl{
Service: serviceValues,
Namespace: lValues.Namespace,
}
err := tmpl.Execute(&pbp, tmplVals)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -163,7 +172,11 @@ func GeneratePreBackupPod(

var pbp bytes.Buffer
tmpl, _ := template.New("").Funcs(funcMap).Parse(preBackupPodSpecs[serviceValues.Type])
err := tmpl.Execute(&pbp, serviceValues)
tmplVals := PreBackupPodTmpl{
Service: serviceValues,
Namespace: lValues.Namespace,
}
err := tmpl.Execute(&pbp, tmplVals)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -285,7 +298,7 @@ var preBackupPodSpecs = PreBackupPods{
$BACKUP_DB_DATABASE
>> $dump
&& cat $dump && rm $dump"
fileExtension: .{{ .Name }}.sql
fileExtension: .{{ .Service.Name }}.sql
pod:
spec:
containers:
Expand All @@ -296,26 +309,26 @@ pod:
- name: BACKUP_DB_HOST
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_HOST
key: {{ .Service.Name | VarFix }}_HOST
name: lagoon-env
- name: BACKUP_DB_USERNAME
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_USERNAME
key: {{ .Service.Name | VarFix }}_USERNAME
name: lagoon-env
- name: BACKUP_DB_PASSWORD
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_PASSWORD
key: {{ .Service.Name | VarFix }}_PASSWORD
name: lagoon-env
- name: BACKUP_DB_DATABASE
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_DATABASE
key: {{ .Service.Name | VarFix }}_DATABASE
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
name: {{ .Name }}-prebackuppod`,
name: {{ .Service.Name }}-prebackuppod`,
"postgres-dbaas": `backupCommand: >
/bin/sh -c "if [ ! -z $BACKUP_DB_READREPLICA_HOSTS ]; then
BACKUP_DB_HOST=$(echo $BACKUP_DB_READREPLICA_HOSTS | cut -d ',' -f1);
Expand All @@ -325,7 +338,7 @@ pod:
--dbname=$BACKUP_DB_DATABASE
--username=$BACKUP_DB_USERNAME
--format=t -w"
fileExtension: .{{ .Name }}.tar
fileExtension: .{{ .Service.Name }}.tar
pod:
spec:
containers:
Expand All @@ -336,28 +349,28 @@ pod:
- name: BACKUP_DB_HOST
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_HOST
key: {{ .Service.Name | VarFix }}_HOST
name: lagoon-env
- name: BACKUP_DB_USERNAME
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_USERNAME
key: {{ .Service.Name | VarFix }}_USERNAME
name: lagoon-env
- name: BACKUP_DB_PASSWORD
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_PASSWORD
key: {{ .Service.Name | VarFix }}_PASSWORD
name: lagoon-env
- name: BACKUP_DB_DATABASE
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_DATABASE
key: {{ .Service.Name | VarFix }}_DATABASE
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
name: {{ .Name }}-prebackuppod`,
"mongodb-dbaas": `backupCommand: /bin/sh -c "mongodump --uri=mongodb://${BACKUP_DB_USERNAME}:${BACKUP_DB_PASSWORD}@${BACKUP_DB_HOST}:${BACKUP_DB_PORT}/${BACKUP_DB_DATABASE}?ssl=true&sslInsecure=true&tls=true&tlsInsecure=true --archive"
fileExtension: .{{ .Name }}.bson
name: {{ .Service.Name }}-prebackuppod`,
"mongodb-dbaas": `backupCommand: /bin/sh -c "dump=$(mktemp) && mongodump --quiet --ssl --tlsInsecure --username=${BACKUP_DB_USERNAME} --password=${BACKUP_DB_PASSWORD} --host=${BACKUP_DB_HOST}:${BACKUP_DB_PORT} --db=${BACKUP_DB_DATABASE} --authenticationDatabase=${BACKUP_DB_AUTHSOURCE} --authenticationMechanism=${BACKUP_DB_AUTHMECHANISM} --archive=$dump && cat $dump && rm $dump"
fileExtension: .{{ .Service.Name }}.bson
pod:
spec:
containers:
Expand All @@ -368,24 +381,44 @@ pod:
- name: BACKUP_DB_HOST
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_HOST
key: {{ .Service.Name | VarFix }}_HOST
name: lagoon-env
- name: BACKUP_DB_USERNAME
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_USERNAME
key: {{ .Service.Name | VarFix }}_USERNAME
name: lagoon-env
- name: BACKUP_DB_PASSWORD
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_PASSWORD
key: {{ .Service.Name | VarFix }}_PASSWORD
name: lagoon-env
- name: BACKUP_DB_DATABASE
valueFrom:
configMapKeyRef:
key: {{ .Name | VarFix }}_DATABASE
key: {{ .Service.Name | VarFix }}_DATABASE
name: lagoon-env
- name: BACKUP_DB_PORT
valueFrom:
configMapKeyRef:
key: {{ .Service.Name | VarFix }}_PORT
name: lagoon-env
- name: BACKUP_DB_AUTHSOURCE
valueFrom:
configMapKeyRef:
key: {{ .Service.Name | VarFix }}_AUTHSOURCE
name: lagoon-env
- name: BACKUP_DB_AUTHMECHANISM
valueFrom:
configMapKeyRef:
key: {{ .Service.Name | VarFix }}_AUTHMECHANISM
name: lagoon-env
- name: BACKUP_DB_AUTHTLS
valueFrom:
configMapKeyRef:
key: {{ .Service.Name | VarFix }}_AUTHTLS
name: lagoon-env
image: uselagoon/database-tools:latest
imagePullPolicy: Always
name: {{ .Name }}-prebackuppod`,
name: {{ .Service.Name }}-prebackuppod`,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ metadata:
prebackuppod: mongodb-database
name: mongodb-database-prebackuppod
spec:
backupCommand: /bin/sh -c "mongodump --uri=mongodb://${BACKUP_DB_USERNAME}:${BACKUP_DB_PASSWORD}@${BACKUP_DB_HOST}:${BACKUP_DB_PORT}/${BACKUP_DB_DATABASE}?ssl=true&sslInsecure=true&tls=true&tlsInsecure=true
--archive"
backupCommand: /bin/sh -c "dump=$(mktemp) && mongodump --quiet --ssl --tlsInsecure
--username=${BACKUP_DB_USERNAME} --password=${BACKUP_DB_PASSWORD} --host=${BACKUP_DB_HOST}:${BACKUP_DB_PORT}
--db=${BACKUP_DB_DATABASE} --authenticationDatabase=${BACKUP_DB_AUTHSOURCE} --authenticationMechanism=${BACKUP_DB_AUTHMECHANISM}
--archive=$dump && cat $dump && rm $dump"
fileExtension: .mongodb-database.bson
pod:
metadata: {}
Expand Down Expand Up @@ -50,6 +52,26 @@ spec:
configMapKeyRef:
key: MONGODB_DATABASE_DATABASE
name: lagoon-env
- name: BACKUP_DB_PORT
valueFrom:
configMapKeyRef:
key: MONGODB_DATABASE_PORT
name: lagoon-env
- name: BACKUP_DB_AUTHSOURCE
valueFrom:
configMapKeyRef:
key: MONGODB_DATABASE_AUTHSOURCE
name: lagoon-env
- name: BACKUP_DB_AUTHMECHANISM
valueFrom:
configMapKeyRef:
key: MONGODB_DATABASE_AUTHMECHANISM
name: lagoon-env
- name: BACKUP_DB_AUTHTLS
valueFrom:
configMapKeyRef:
key: MONGODB_DATABASE_AUTHTLS
name: lagoon-env
- name: BACKUP_DB_READREPLICA_HOSTS
valueFrom:
configMapKeyRef:
Expand Down
Loading

0 comments on commit 11ca33e

Please sign in to comment.