diff --git a/cmd/template_backups_test.go b/cmd/template_backups_test.go index 8a3be857..20151092 100644 --- a/cmd/template_backups_test.go +++ b/cmd/template_backups_test.go @@ -37,6 +37,7 @@ func TestBackupTemplateGeneration(t *testing.T) { controllerDevSchedule string controllerPRSchedule string k8upVersion string + namespace string } tests := []struct { name string @@ -51,6 +52,7 @@ func TestBackupTemplateGeneration(t *testing.T) { statusPageID: "statuspageid", projectName: "example-project", environmentName: "main", + namespace: "example-project-main", environmentType: "production", buildType: "branch", lagoonVersion: "v2.7.x", @@ -70,6 +72,7 @@ func TestBackupTemplateGeneration(t *testing.T) { statusPageID: "statuspageid", projectName: "example-project", environmentName: "main", + namespace: "example-project-main", environmentType: "development", buildType: "branch", lagoonVersion: "v2.7.x", @@ -89,6 +92,7 @@ func TestBackupTemplateGeneration(t *testing.T) { statusPageID: "statuspageid", projectName: "example-project", environmentName: "main", + namespace: "example-project-main", environmentType: "production", buildType: "branch", lagoonVersion: "v2.7.x", @@ -108,6 +112,7 @@ func TestBackupTemplateGeneration(t *testing.T) { statusPageID: "statuspageid", projectName: "example-project", environmentName: "main", + namespace: "example-project-main", environmentType: "development", buildType: "pullrequest", prNumber: "123", @@ -130,6 +135,7 @@ func TestBackupTemplateGeneration(t *testing.T) { statusPageID: "statuspageid", projectName: "example-project", environmentName: "main", + namespace: "example-project-main", environmentType: "development", buildType: "pullrequest", prNumber: "123", @@ -152,6 +158,7 @@ func TestBackupTemplateGeneration(t *testing.T) { statusPageID: "statuspageid", projectName: "example-project", environmentName: "main", + namespace: "example-project-main", environmentType: "production", buildType: "branch", lagoonVersion: "v2.7.x", @@ -184,6 +191,10 @@ func TestBackupTemplateGeneration(t *testing.T) { if err != nil { t.Errorf("%v", err) } + err = os.Setenv("NAMESPACE", tt.args.namespace) + if err != nil { + t.Errorf("%v", err) + } err = os.Setenv("BRANCH", tt.args.branch) if err != nil { t.Errorf("%v", err) diff --git a/internal/generator/generator.go b/internal/generator/generator.go index 2402f26b..e793fa32 100644 --- a/internal/generator/generator.go +++ b/internal/generator/generator.go @@ -53,6 +53,7 @@ type GeneratorInput struct { IgnoreMissingEnvFiles bool Debug bool DBaaSClient *dbaasclient.Client + Namespace string } func NewGenerator( @@ -87,6 +88,15 @@ func NewGenerator( fastlyAPISecretPrefix := helpers.GetEnv("ROUTE_FASTLY_SERVICE_ID", generator.FastlyAPISecretPrefix, generator.Debug) lagoonVersion := helpers.GetEnv("LAGOON_VERSION", generator.LagoonVersion, generator.Debug) + // try source the namespace from the generator, but whatever is defined in the service account location + // should be used if one exists, falls back to whatever came in via generator + namespace := helpers.GetEnv("NAMESPACE", generator.Namespace, generator.Debug) + namespace, err := helpers.GetNamespace(namespace, "/var/run/secrets/kubernetes.io/serviceaccount/namespace") + if err != nil { + // a file was found, but there was an issue accessing it + return nil, err + } + buildValues.Backup.K8upVersion = helpers.GetEnv("K8UP_VERSION", generator.BackupConfiguration.K8upVersion, generator.Debug) // get the project and environment variables @@ -110,6 +120,7 @@ func NewGenerator( // start saving values into the build values variable buildValues.Project = projectName buildValues.Environment = environmentName + buildValues.Namespace = namespace buildValues.EnvironmentType = environmentType buildValues.BuildType = buildType buildValues.LagoonVersion = lagoonVersion @@ -227,7 +238,7 @@ func NewGenerator( // buildValues.DBaaSFallbackSingle = helpers.StrToBool(lagoonDBaaSFallbackSingle.Value) /* start backups configuration */ - err := generateBackupValues(&buildValues, lYAML, lagoonEnvVars, generator.Debug) + err = generateBackupValues(&buildValues, lYAML, lagoonEnvVars, generator.Debug) if err != nil { return nil, err } diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go index cd3b031a..0d1d16cf 100644 --- a/internal/helpers/helpers.go +++ b/internal/helpers/helpers.go @@ -7,6 +7,7 @@ import ( "encoding/base32" "encoding/gob" "encoding/hex" + "errors" "fmt" "os" "strconv" @@ -171,6 +172,18 @@ type EnvironmentVariable struct { Value string } +// Try and get the namespace name from the serviceaccount location if it exists +func GetNamespace(namespace, filename string) (string, error) { + if _, err := os.Stat(filename); !errors.Is(err, os.ErrNotExist) { + nsb, err := os.ReadFile(filename) + if err != nil { + return "", err + } + namespace = strings.Trim(string(nsb), "\n ") + } + return namespace, nil +} + func UnsetEnvVars(localVars []EnvironmentVariable) { varNames := []string{ "MONITORING_ALERTCONTACT", diff --git a/internal/helpers/helpers_test.go b/internal/helpers/helpers_test.go index a2b3a826..8001f1eb 100644 --- a/internal/helpers/helpers_test.go +++ b/internal/helpers/helpers_test.go @@ -573,3 +573,44 @@ func TestCheckLabelLength(t *testing.T) { }) } } +func TestGetNamespace(t *testing.T) { + type args struct { + namespace string + filename string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "test1 - no file", + args: args{ + namespace: "test-namespace", + filename: "test-resources/test1", + }, + want: "test-namespace", + }, + { + name: "test2 - test namespace file", + args: args{ + namespace: "a-namespace", + filename: "test-resources/test2", + }, + want: "test-namespace", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetNamespace(tt.args.namespace, tt.args.filename) + if (err != nil) != tt.wantErr { + t.Errorf("GetNamespace() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetNamespace() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/helpers/test-resources/test2 b/internal/helpers/test-resources/test2 new file mode 100644 index 00000000..d2826d00 --- /dev/null +++ b/internal/helpers/test-resources/test2 @@ -0,0 +1 @@ +test-namespace \ No newline at end of file diff --git a/test-resources/template-backups/test1-results/k8up-lagoon-backup-schedule.yaml b/test-resources/template-backups/test1-results/k8up-lagoon-backup-schedule.yaml index 6944c64e..8aa7135c 100644 --- a/test-resources/template-backups/test1-results/k8up-lagoon-backup-schedule.yaml +++ b/test-resources/template-backups/test1-results/k8up-lagoon-backup-schedule.yaml @@ -27,16 +27,16 @@ spec: bucket: baas-example-project backup: resources: {} - schedule: 5 23 * * * + schedule: 48 22 * * * check: resources: {} - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 prune: resources: {} retention: keepDaily: 7 keepMonthly: 1 keepWeekly: 6 - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 resourceRequirementsTemplate: {} status: {} diff --git a/test-resources/template-backups/test2-results/k8up-lagoon-backup-schedule.yaml b/test-resources/template-backups/test2-results/k8up-lagoon-backup-schedule.yaml index 7a3de0e9..a42bbb47 100644 --- a/test-resources/template-backups/test2-results/k8up-lagoon-backup-schedule.yaml +++ b/test-resources/template-backups/test2-results/k8up-lagoon-backup-schedule.yaml @@ -30,13 +30,13 @@ spec: schedule: 1,31 23 * * * check: resources: {} - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 prune: resources: {} retention: keepDaily: 7 keepMonthly: 1 keepWeekly: 6 - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 resourceRequirementsTemplate: {} status: {} diff --git a/test-resources/template-backups/test3-results/k8up-lagoon-backup-schedule.yaml b/test-resources/template-backups/test3-results/k8up-lagoon-backup-schedule.yaml index 35be53be..8833d020 100644 --- a/test-resources/template-backups/test3-results/k8up-lagoon-backup-schedule.yaml +++ b/test-resources/template-backups/test3-results/k8up-lagoon-backup-schedule.yaml @@ -27,10 +27,10 @@ spec: bucket: baas-example-project backup: resources: {} - schedule: 5 23 * * * + schedule: 48 22 * * * check: resources: {} - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 prune: resources: {} retention: @@ -38,6 +38,6 @@ spec: keepHourly: 10 keepMonthly: 10 keepWeekly: 10 - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 resourceRequirementsTemplate: {} status: {} diff --git a/test-resources/template-backups/test4-results/k8up-lagoon-backup-schedule.yaml b/test-resources/template-backups/test4-results/k8up-lagoon-backup-schedule.yaml index 127f7099..66f4ad23 100644 --- a/test-resources/template-backups/test4-results/k8up-lagoon-backup-schedule.yaml +++ b/test-resources/template-backups/test4-results/k8up-lagoon-backup-schedule.yaml @@ -38,14 +38,14 @@ spec: schedule: 3,33 12 * * * check: resources: {} - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 prune: resources: {} retention: keepDaily: 7 keepMonthly: 1 keepWeekly: 6 - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 resourceRequirementsTemplate: {} status: {} --- diff --git a/test-resources/template-backups/test5-results/k8up-lagoon-backup-schedule.yaml b/test-resources/template-backups/test5-results/k8up-lagoon-backup-schedule.yaml index 836f061e..72fd7259 100644 --- a/test-resources/template-backups/test5-results/k8up-lagoon-backup-schedule.yaml +++ b/test-resources/template-backups/test5-results/k8up-lagoon-backup-schedule.yaml @@ -32,14 +32,14 @@ spec: schedule: 3,33 12 * * * check: resources: {} - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 prune: resources: {} retention: keepDaily: 7 keepMonthly: 1 keepWeekly: 6 - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 resourceRequirementsTemplate: {} status: {} --- diff --git a/test-resources/template-backups/test6-results/k8up-lagoon-backup-schedule.yaml b/test-resources/template-backups/test6-results/k8up-lagoon-backup-schedule.yaml index f5b6241a..b8fdcb3c 100644 --- a/test-resources/template-backups/test6-results/k8up-lagoon-backup-schedule.yaml +++ b/test-resources/template-backups/test6-results/k8up-lagoon-backup-schedule.yaml @@ -27,10 +27,10 @@ spec: bucket: baas-example-project backup: resources: {} - schedule: 5,20,35,50 5 * * 0 + schedule: 3,18,33,48 5 * * 0 check: resources: {} - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 prune: resources: {} retention: @@ -38,6 +38,6 @@ spec: keepHourly: 10 keepMonthly: 12 keepWeekly: 16 - schedule: 5 5 * * 0 + schedule: 48 3 * * 0 resourceRequirementsTemplate: {} status: {}