Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: only generate schedules if resource types have backups #271

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion cmd/template_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestBackupTemplateGeneration(t *testing.T) {
args testdata.TestData
templatePath string
want string
emptyDir bool // if no templates are generated, then there will be a .gitkeep file in there
wantErr bool
}{
{
Expand Down Expand Up @@ -188,6 +189,27 @@ func TestBackupTemplateGeneration(t *testing.T) {
templatePath: "testdata/output",
want: "../internal/testdata/complex/backup-templates/backup-2",
},
{
name: "test9 - nothing to backup so no schedule",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
K8UPVersion: "v2",
LagoonYAML: "../internal/testdata/node/lagoon.nostorage.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_IMAGECACHE_REGISTRY",
Value: "imagecache.example.com",
Scope: "global",
},
},
}, true),
templatePath: "testdata/output",
emptyDir: true,
want: "../internal/testdata/node/backup-templates/backup-7",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -221,7 +243,16 @@ func TestBackupTemplateGeneration(t *testing.T) {
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
if len(files) != len(results) {
resultSize := 0
if !tt.emptyDir {
results, err = ioutil.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
// .gitkeep file needs to be subtracted to equal 0
resultSize = len(results)
}
if len(files) != resultSize {
for _, f := range files {
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name()))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type BuildValues struct {
TaskScaleMaxIterations int `json:"taskScaleMaxIterations"`
TaskScaleWaitTime int `json:"taskScaleWaitTime"`
ImageCache string `json:"imageCache"`
BackupsEnabled bool `json:"backupsEnabled"`
DefaultBackupSchedule string `json:"defaultBackupSchedule"`
DBaaSClient *dbaasclient.Client `json:"-"`
}
Expand Down Expand Up @@ -91,6 +92,7 @@ type ServiceValues struct {
CronjobTolerations *[]corev1.Toleration `json:"cronjobTolerations"`
CronjobAffinity *corev1.Affinity `json:"cronjobAffinity"`
DBaasReadReplica bool `json:"dBaasReadReplica"`
BackupsEnabled bool `json:"backupsEnabled"`
}

// CronjobValues is the values for cronjobs
Expand Down
31 changes: 31 additions & 0 deletions internal/generator/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ var supportedAutogeneratedTypes = []string{
"python",
}

// these are lagoon types that come with resources requiring backups
var typesWithBackups = []string{
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
"basic-persistent",
"node-persistent",
"nginx-php-persistent",
"python-persistent",
"varnish-persistent",
"redis-persistent",
"solr",
"elasticsearch",
"opensearch",
"rabbitmq",
"mongodb-dbaas",
"mariadb-dbaas",
"postgres-dbaas",
"mariadb-single",
"postgres-single",
"mongodb-single",
}

// just some default values for services
var defaultServiceValues = map[string]map[string]string{
"elasticsearch": map[string]string{
Expand Down Expand Up @@ -102,6 +122,9 @@ func generateServicesFromDockerCompose(
if err != nil {
return err
}
if cService.BackupsEnabled {
buildValues.BackupsEnabled = true
}
buildValues.Services = append(buildValues.Services, cService)
}
}
Expand Down Expand Up @@ -311,6 +334,13 @@ func composeToServiceValues(
autogenTLSAcmeEnabled = false
}

// check if this service is one that supports backups
backupsEnabled := false
if helpers.Contains(typesWithBackups, lagoonType) {
backupsEnabled = true

}

// create the service values
cService := ServiceValues{
Name: composeService,
Expand All @@ -322,6 +352,7 @@ func composeToServiceValues(
PersistentVolumePath: servicePersistentPath,
PersistentVolumeName: servicePersistentName,
PersistentVolumeSize: servicePersistentSize,
BackupsEnabled: backupsEnabled,
}
// check if the service has a service port override (this only applies to basic(-persistent))
servicePortOverride := lagoon.CheckServiceLagoonLabel(composeServiceValues.Labels, "lagoon.service.port")
Expand Down
5 changes: 5 additions & 0 deletions internal/generator/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func Test_composeToServiceValues(t *testing.T) {
Type: "nginx-php-persistent",
AutogeneratedRoutesEnabled: true,
AutogeneratedRoutesTLSAcme: true,
BackupsEnabled: true,
},
},
{
Expand Down Expand Up @@ -147,6 +148,7 @@ func Test_composeToServiceValues(t *testing.T) {
Type: "nginx-php-persistent",
AutogeneratedRoutesEnabled: true,
AutogeneratedRoutesTLSAcme: true,
BackupsEnabled: true,
},
},
{
Expand Down Expand Up @@ -348,6 +350,7 @@ func Test_composeToServiceValues(t *testing.T) {
AutogeneratedRoutesEnabled: false,
AutogeneratedRoutesTLSAcme: false,
DBaaSEnvironment: "development",
BackupsEnabled: true,
},
},
{
Expand Down Expand Up @@ -391,6 +394,7 @@ func Test_composeToServiceValues(t *testing.T) {
AutogeneratedRoutesEnabled: false,
AutogeneratedRoutesTLSAcme: false,
DBaaSEnvironment: "development2",
BackupsEnabled: true,
},
},
{
Expand Down Expand Up @@ -429,6 +433,7 @@ func Test_composeToServiceValues(t *testing.T) {
AutogeneratedRoutesEnabled: false,
AutogeneratedRoutesTLSAcme: false,
DBaaSEnvironment: "development",
BackupsEnabled: true,
},
},
{
Expand Down
Loading
Loading