Skip to content

Commit edd966e

Browse files
committed
chore: backport #329
1 parent 5bd18c0 commit edd966e

File tree

11 files changed

+181
-81
lines changed

11 files changed

+181
-81
lines changed

cmd/template_backups_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ func TestBackupTemplateGeneration(t *testing.T) {
215215
templatePath: "testdata/output",
216216
emptyDir: true,
217217
want: "internal/testdata/node/backup-templates/backup-7",
218+
}, {
219+
name: "test10 - generic backup with random check prune feature flags",
220+
args: testdata.GetSeedData(
221+
testdata.TestData{
222+
ProjectName: "example-project",
223+
EnvironmentName: "main",
224+
Branch: "main",
225+
EnvironmentType: "production",
226+
LagoonYAML: "internal/testdata/node/lagoon.yml",
227+
EnvVariables: []lagoon.EnvironmentVariable{
228+
{
229+
Name: "LAGOON_FEATURE_FLAG_K8UP_WEEKLY_RANDOM_CHECK",
230+
Value: "enabled",
231+
Scope: "global",
232+
},
233+
{
234+
Name: "LAGOON_FEATURE_FLAG_K8UP_WEEKLY_RANDOM_PRUNE",
235+
Value: "enabled",
236+
Scope: "global",
237+
},
238+
},
239+
}, true),
240+
templatePath: "testdata/output",
241+
want: "internal/testdata/node/backup-templates/backup-8",
218242
},
219243
}
220244
for _, tt := range tests {

internal/generator/backups.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,44 +94,46 @@ func generateBackupValues(
9494

9595
buildValues.Backup.BackupSchedule, err = helpers.ConvertCrontab(buildValues.Namespace, newBackupSchedule)
9696
if err != nil {
97-
return fmt.Errorf("Unable to convert crontab for default backup schedule: %v", err)
97+
return fmt.Errorf("unable to convert crontab for default backup schedule: %v", err)
9898
}
9999

100100
// start: get variables from the build pod that may have been added by the controller
101101
flagCheckSchedule := helpers.GetEnv("K8UP_WEEKLY_RANDOM_FEATURE_FLAG", defaultCheckSchedule, debug)
102-
if flagCheckSchedule == "enabled" {
102+
lffCheckSchedule := CheckFeatureFlag("K8UP_WEEKLY_RANDOM_CHECK", mergedVariables, debug)
103+
if flagCheckSchedule == "enabled" || lffCheckSchedule == "enabled" {
103104
buildValues.Backup.CheckSchedule = "@weekly-random"
104105
} else {
105-
buildValues.Backup.CheckSchedule, err = helpers.ConvertCrontab(fmt.Sprintf("%s", buildValues.Namespace), defaultCheckSchedule)
106+
buildValues.Backup.CheckSchedule, err = helpers.ConvertCrontab(buildValues.Namespace, defaultCheckSchedule)
106107
if err != nil {
107-
return fmt.Errorf("Unable to convert crontab for default check schedule: %v", err)
108+
return fmt.Errorf("unable to convert crontab for default check schedule: %v", err)
108109
}
109110
}
110111
flagPruneSchedule := helpers.GetEnv("K8UP_WEEKLY_RANDOM_FEATURE_FLAG", defaultPruneSchedule, debug)
111-
if flagPruneSchedule == "enabled" {
112+
lffPruneSchedule := CheckFeatureFlag("K8UP_WEEKLY_RANDOM_PRUNE", mergedVariables, debug)
113+
if flagPruneSchedule == "enabled" || lffPruneSchedule == "enabled" {
112114
buildValues.Backup.PruneSchedule = "@weekly-random"
113115
} else {
114-
buildValues.Backup.PruneSchedule, err = helpers.ConvertCrontab(fmt.Sprintf("%s", buildValues.Namespace), defaultPruneSchedule)
116+
buildValues.Backup.PruneSchedule, err = helpers.ConvertCrontab(buildValues.Namespace, defaultPruneSchedule)
115117
if err != nil {
116-
return fmt.Errorf("Unable to convert crontab for default prune schedule: %v", err)
118+
return fmt.Errorf("unable to convert crontab for default prune schedule: %v", err)
117119
}
118120
}
119121

120122
buildValues.Backup.PruneRetention.Hourly, err = helpers.EGetEnvInt("HOURLY_BACKUP_DEFAULT_RETENTION", hourlyDefaultBackupRetention, debug)
121123
if err != nil {
122-
return fmt.Errorf("Unable to convert hourly retention provided in the environment variable to integer")
124+
return fmt.Errorf("unable to convert hourly retention provided in the environment variable to integer")
123125
}
124126
buildValues.Backup.PruneRetention.Daily, err = helpers.EGetEnvInt("DAILY_BACKUP_DEFAULT_RETENTION", dailyDefaultBackupRetention, debug)
125127
if err != nil {
126-
return fmt.Errorf("Unable to convert daily retention provided in the environment variable to integer")
128+
return fmt.Errorf("unable to convert daily retention provided in the environment variable to integer")
127129
}
128130
buildValues.Backup.PruneRetention.Weekly, err = helpers.EGetEnvInt("WEEKLY_BACKUP_DEFAULT_RETENTION", weeklyDefaultBackupRetention, debug)
129131
if err != nil {
130-
return fmt.Errorf("Unable to convert weekly retention provided in the environment variable to integer")
132+
return fmt.Errorf("unable to convert weekly retention provided in the environment variable to integer")
131133
}
132134
buildValues.Backup.PruneRetention.Monthly, err = helpers.EGetEnvInt("MONTHLY_BACKUP_DEFAULT_RETENTION", monthlyDefaultBackupRetention, debug)
133135
if err != nil {
134-
return fmt.Errorf("Unable to convert monthly retention provided in the environment variable to integer")
136+
return fmt.Errorf("unable to convert monthly retention provided in the environment variable to integer")
135137
}
136138
// :end
137139

@@ -150,7 +152,7 @@ func generateBackupValues(
150152
if buildValues.LagoonYAML.BackupSchedule.Production != "" && buildValues.EnvironmentType == "production" {
151153
buildValues.Backup.BackupSchedule, err = helpers.ConvertCrontab(buildValues.Namespace, buildValues.LagoonYAML.BackupSchedule.Production)
152154
if err != nil {
153-
return fmt.Errorf("Unable to convert crontab for default backup schedule from .lagoon.yml: %v", err)
155+
return fmt.Errorf("unable to convert crontab for default backup schedule from .lagoon.yml: %v", err)
154156
}
155157
}
156158

internal/generator/backups_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,78 @@ func Test_generateBackupValues(t *testing.T) {
703703
},
704704
},
705705
},
706+
{
707+
name: "test18 - LAGOON_FEATURE_FLAG_K8UP_WEEKLY_RANDOM_CHECK enabled",
708+
args: args{
709+
buildValues: &BuildValues{
710+
BuildType: "branch",
711+
EnvironmentType: "development",
712+
Project: "example-project",
713+
Namespace: "example-com-main",
714+
DefaultBackupSchedule: "M H(22-2) * * *",
715+
LagoonYAML: lagoon.YAML{},
716+
},
717+
mergedVariables: []lagoon.EnvironmentVariable{
718+
{Name: "LAGOON_FEATURE_FLAG_K8UP_WEEKLY_RANDOM_CHECK", Value: "enabled", Scope: "global"},
719+
},
720+
},
721+
vars: []helpers.EnvironmentVariable{},
722+
want: &BuildValues{
723+
BuildType: "branch",
724+
EnvironmentType: "development",
725+
Project: "example-project",
726+
Namespace: "example-com-main",
727+
DefaultBackupSchedule: "M H(22-2) * * *",
728+
Backup: BackupConfiguration{
729+
BackupSchedule: "31 1 * * *",
730+
CheckSchedule: "@weekly-random",
731+
PruneSchedule: "31 4 * * 0",
732+
S3BucketName: "baas-example-project",
733+
PruneRetention: PruneRetention{
734+
Hourly: 0,
735+
Daily: 7,
736+
Weekly: 6,
737+
Monthly: 1,
738+
},
739+
},
740+
},
741+
},
742+
{
743+
name: "test19 - LAGOON_FEATURE_FLAG_K8UP_WEEKLY_RANDOM_PRUNE enabled",
744+
args: args{
745+
buildValues: &BuildValues{
746+
BuildType: "branch",
747+
EnvironmentType: "development",
748+
Project: "example-project",
749+
Namespace: "example-com-main",
750+
DefaultBackupSchedule: "M H(22-2) * * *",
751+
LagoonYAML: lagoon.YAML{},
752+
},
753+
mergedVariables: []lagoon.EnvironmentVariable{
754+
{Name: "LAGOON_FEATURE_FLAG_K8UP_WEEKLY_RANDOM_PRUNE", Value: "enabled", Scope: "global"},
755+
},
756+
},
757+
vars: []helpers.EnvironmentVariable{},
758+
want: &BuildValues{
759+
BuildType: "branch",
760+
EnvironmentType: "development",
761+
Project: "example-project",
762+
Namespace: "example-com-main",
763+
DefaultBackupSchedule: "M H(22-2) * * *",
764+
Backup: BackupConfiguration{
765+
BackupSchedule: "31 1 * * *",
766+
CheckSchedule: "31 6 * * 1",
767+
PruneSchedule: "@weekly-random",
768+
S3BucketName: "baas-example-project",
769+
PruneRetention: PruneRetention{
770+
Hourly: 0,
771+
Daily: 7,
772+
Weekly: 6,
773+
Monthly: 1,
774+
},
775+
},
776+
},
777+
},
706778
}
707779
for _, tt := range tests {
708780
t.Run(tt.name, func(t *testing.T) {

internal/generator/container_registries.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,8 @@ func configureContainerRegistries(buildValues *BuildValues) error {
7171
}
7272
eru := cr.URL
7373
u, _ := url.Parse(eru)
74-
if u.Host == "" {
75-
eru = fmt.Sprintf("%s", eru)
76-
} else {
77-
eru = fmt.Sprintf("%s", u.Host)
74+
if u.Host != "" {
75+
eru = u.Host
7876
}
7977
// truncate the secret name to fit within the DNS1123subdomain spec before creating it
8078
secretName := fmt.Sprintf("lagoon-private-registry-%s", machinerynamespace.MakeSafe(n))

internal/generator/generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ func NewGenerator(
189189

190190
// break out of the generator if these requirements are missing
191191
if projectName == "" || environmentName == "" || environmentType == "" || buildType == "" {
192-
return nil, fmt.Errorf("Missing arguments: project-name, environment-name, environment-type, or build-type not defined")
192+
return nil, fmt.Errorf("missing arguments: project-name, environment-name, environment-type, or build-type not defined")
193193
}
194194
switch buildType {
195195
case "branch", "promote":
196196
if branch == "" {
197-
return nil, fmt.Errorf("Missing arguments: branch not defined")
197+
return nil, fmt.Errorf("missing arguments: branch not defined")
198198
}
199199
case "pullrequest":
200200
if prNumber == "" || prHeadBranch == "" || prBaseBranch == "" {
201-
return nil, fmt.Errorf("Missing arguments: pullrequest-number, pullrequest-head-branch, or pullrequest-base-branch not defined")
201+
return nil, fmt.Errorf("missing arguments: pullrequest-number, pullrequest-head-branch, or pullrequest-base-branch not defined")
202202
}
203203
}
204204

internal/generator/helpers_generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,23 @@ func CheckFeatureFlag(key string, envVariables []lagoon.EnvironmentVariable, deb
148148
// check for force value
149149
if value, ok := os.LookupEnv(fmt.Sprintf("LAGOON_FEATURE_FLAG_FORCE_%s", key)); ok {
150150
if debug {
151-
fmt.Println(fmt.Sprintf("Using forced flag value from build variable %s", fmt.Sprintf("LAGOON_FEATURE_FLAG_FORCE_%s", key)))
151+
fmt.Printf("Using forced flag value from build variable %s\n", fmt.Sprintf("LAGOON_FEATURE_FLAG_FORCE_%s", key))
152152
}
153153
return value
154154
}
155155
// check lagoon environment variables
156156
for _, lVar := range envVariables {
157157
if strings.Contains(lVar.Name, fmt.Sprintf("LAGOON_FEATURE_FLAG_%s", key)) {
158158
if debug {
159-
fmt.Println(fmt.Sprintf("Using flag value from Lagoon environment variable %s", fmt.Sprintf("LAGOON_FEATURE_FLAG_%s", key)))
159+
fmt.Printf("Using flag value from Lagoon environment variable %s\n", fmt.Sprintf("LAGOON_FEATURE_FLAG_%s", key))
160160
}
161161
return lVar.Value
162162
}
163163
}
164164
// return default
165165
if value, ok := os.LookupEnv(fmt.Sprintf("LAGOON_FEATURE_FLAG_DEFAULT_%s", key)); ok {
166166
if debug {
167-
fmt.Println(fmt.Sprintf("Using default flag value from build variable %s", fmt.Sprintf("LAGOON_FEATURE_FLAG_DEFAULT_%s", key)))
167+
fmt.Printf("Using default flag value from build variable %s\n", fmt.Sprintf("LAGOON_FEATURE_FLAG_DEFAULT_%s", key))
168168
}
169169
return value
170170
}
@@ -175,7 +175,7 @@ func CheckFeatureFlag(key string, envVariables []lagoon.EnvironmentVariable, deb
175175
func CheckAdminFeatureFlag(key string, debug bool) string {
176176
if value, ok := os.LookupEnv(fmt.Sprintf("ADMIN_LAGOON_FEATURE_FLAG_%s", key)); ok {
177177
if debug {
178-
fmt.Println(fmt.Sprintf("Using admin feature flag value from build variable %s", fmt.Sprintf("ADMIN_LAGOON_FEATURE_FLAG_%s", key)))
178+
fmt.Printf("Using admin feature flag value from build variable %s\n", fmt.Sprintf("ADMIN_LAGOON_FEATURE_FLAG_%s", key))
179179
}
180180
return value
181181
}

internal/generator/ingress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func generateActiveStandbyRoutes(
247247
) (lagoon.RoutesV2, error) {
248248
activeStanbyRoutes := &lagoon.RoutesV2{}
249249
if buildValues.LagoonYAML.ProductionRoutes != nil {
250-
if buildValues.IsActiveEnvironment == true {
250+
if buildValues.IsActiveEnvironment {
251251
if buildValues.LagoonYAML.ProductionRoutes.Active != nil {
252252
if buildValues.LagoonYAML.ProductionRoutes.Active.Routes != nil {
253253
for _, routeMap := range buildValues.LagoonYAML.ProductionRoutes.Active.Routes {
@@ -259,7 +259,7 @@ func generateActiveStandbyRoutes(
259259
}
260260
}
261261
}
262-
if buildValues.IsStandbyEnvironment == true {
262+
if buildValues.IsStandbyEnvironment {
263263
if buildValues.LagoonYAML.ProductionRoutes.Standby != nil {
264264
if buildValues.LagoonYAML.ProductionRoutes.Standby.Routes != nil {
265265
for _, routeMap := range buildValues.LagoonYAML.ProductionRoutes.Standby.Routes {

internal/generator/lagoon_yaml.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ func LoadAndUnmarshalLagoonYml(lagoonYml string, lagoonYmlOverride string, lagoo
3434
//Decode it
3535
envLagoonYamlString, err := base64.StdEncoding.DecodeString(envLagoonYamlStringBase64)
3636
if err != nil {
37-
return fmt.Errorf("Unable to decode %v - is it base64 encoded?", lagoonYmlOverrideEnvVarName)
37+
return fmt.Errorf("unable to decode %v - is it base64 encoded?", lagoonYmlOverrideEnvVarName)
3838
}
3939
envLagoonYaml := &lagoon.YAML{}
4040
lEnvLagoonPolysite := make(map[string]interface{})
4141

4242
err = yaml.Unmarshal(envLagoonYamlString, envLagoonYaml)
4343
if err != nil {
44-
return fmt.Errorf("Unable to unmarshal env var %v: %v", lagoonYmlOverrideEnvVarName, err)
44+
return fmt.Errorf("unable to unmarshal env var %v: %v", lagoonYmlOverrideEnvVarName, err)
4545
}
4646
err = yaml.Unmarshal(envLagoonYamlString, lEnvLagoonPolysite)
4747
if err != nil {
48-
return fmt.Errorf("Unable to unmarshal env var %v: %v", lagoonYmlOverrideEnvVarName, err)
48+
return fmt.Errorf("unable to unmarshal env var %v: %v", lagoonYmlOverrideEnvVarName, err)
4949
}
5050

5151
if _, ok := lEnvLagoonPolysite[projectName]; ok {

0 commit comments

Comments
 (0)