Skip to content

Commit

Permalink
fix: backport 275 and 278 to 251
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jan 3, 2024
1 parent 6bd767a commit 8d059cf
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 16 deletions.
11 changes: 11 additions & 0 deletions cmd/template_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestBackupTemplateGeneration(t *testing.T) {
controllerDevSchedule string
controllerPRSchedule string
k8upVersion string
namespace string
}
tests := []struct {
name string
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type GeneratorInput struct {
IgnoreMissingEnvFiles bool
Debug bool
DBaaSClient *dbaasclient.Client
Namespace string
}

func NewGenerator(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/base32"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -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",
Expand Down
41 changes: 41 additions & 0 deletions internal/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions internal/helpers/test-resources/test2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test-namespace
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ 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: 10
keepHourly: 10
keepMonthly: 10
keepWeekly: 10
schedule: 5 5 * * 0
schedule: 48 3 * * 0
resourceRequirementsTemplate: {}
status: {}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ 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:
keepDaily: 10
keepHourly: 10
keepMonthly: 12
keepWeekly: 16
schedule: 5 5 * * 0
schedule: 48 3 * * 0
resourceRequirementsTemplate: {}
status: {}

0 comments on commit 8d059cf

Please sign in to comment.