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

Change: use build-tool for generating dbaas consumers in builds #115

Merged
merged 21 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
12c6c3a
refactor: use build-tool for generating dbaas consumers in builds
shreddedbacon Sep 12, 2022
87cd397
chore: remove unused classes
shreddedbacon Sep 12, 2022
cdb7465
chore: add debug message for templating result
shreddedbacon Sep 12, 2022
7e9ebb6
chore: disable debug for ingress identification
shreddedbacon Sep 12, 2022
fbe1977
chore: disable debug for ingress identification
shreddedbacon Sep 12, 2022
0bc4b50
chore: disable debug for dbaas identification
shreddedbacon Sep 12, 2022
165bd80
chore: add dbaas command to identify
shreddedbacon Sep 12, 2022
952950e
chore: add dbaas support to exec-monitor since existing code that tra…
shreddedbacon Sep 12, 2022
768c5e9
Merge branch 'main' into dbaas-templating
shreddedbacon Sep 27, 2022
c2611e1
Merge branch 'main' into dbaas-templating
shreddedbacon Dec 2, 2022
dfa294f
chore: merge main and fix conflict
shreddedbacon Jul 24, 2023
8f67fe4
chore: merge main and fix conflicts
shreddedbacon Dec 7, 2023
9da6646
chore: merge main fix initial conflicts
shreddedbacon Jan 15, 2024
95727e9
test: fix dbaas tests with new changes
shreddedbacon Jan 15, 2024
6f77c14
chore: merge branch 'main' into dbaas-templating
shreddedbacon Jan 24, 2024
e3d27a1
chore: merge main and fix conflicts
shreddedbacon Feb 13, 2024
62e85b6
chore: remove redundant types
shreddedbacon Feb 13, 2024
658a7a4
chore: merge branch 'main' into dbaas-templating
shreddedbacon Feb 26, 2024
70b5279
chore: remove legacy dbaas charts
shreddedbacon Feb 28, 2024
998d865
refactor: support mongo-mongodb type conversion and add tests for mon…
shreddedbacon Feb 28, 2024
11d5bcb
refactor: remove dbaas images from images to pull
shreddedbacon Feb 28, 2024
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
46 changes: 46 additions & 0 deletions cmd/identify_dbaas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
)

// this is an intermediate helper command while transitioning from bash to go
// eventually this won't be required
var dbaasIdentify = &cobra.Command{
Use: "dbaas",
Aliases: []string{"db", "d"},
Short: "Identify if any dbaas consumers are created",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generator.GenerateInput(*rootCmd, false)
if err != nil {
return err
}
dbaasConsumers, err := IdentifyDBaaSConsumers(generator)
if err != nil {
return err
}
for _, dbc := range dbaasConsumers {
fmt.Println(dbc)
}
return nil
},
}

func IdentifyDBaaSConsumers(g generator.GeneratorInput) ([]string, error) {
lagoonBuild, err := generator.NewGenerator(
g,
)
if err != nil {
return nil, err
}
ret := []string{}
for _, svc := range lagoonBuild.BuildValues.Services {
if svc.IsDBaaS {
ret = append(ret, fmt.Sprintf("%s:%s", svc.Name, svc.Type))
}
}
return ret, nil
}
162 changes: 162 additions & 0 deletions cmd/identify_dbaas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package cmd

import (
"os"
"reflect"
"testing"

"github.com/uselagoon/build-deploy-tool/internal/dbaasclient"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
"github.com/uselagoon/build-deploy-tool/internal/testdata"
)

// these tests uses the same files as the dbaas templates
func TestIdentifyDBaaSConsumers(t *testing.T) {
type args struct {
name string
alertContact string
statusPageID string
projectName string
environmentName string
branch string
prNumber string
prHeadBranch string
prBaseBranch string
environmentType string
buildType string
activeEnvironment string
standbyEnvironment string
cacheNoCache string
serviceID string
secretPrefix string
projectVars string
envVars string
lagoonVersion string
lagoonYAML string
valuesFilePath string
templatePath string
}
tests := []struct {
name string
args testdata.TestData
vars []helpers.EnvironmentVariable
templatePath string
want []string
wantErr bool
}{
{
name: "test1 - mariadb to mariadb-dbaas only",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.yml",
}, true),
templatePath: "testdata/output",
want: []string{
"mariadb:mariadb-dbaas",
},
},
{
name: "test2 - mariadb to mariadb-shared which converts to mariadb-dbaas",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{Name: "LAGOON_SERVICE_TYPES", Value: "mariadb:mariadb-shared", Scope: "build"},
},
}, true),
templatePath: "testdata/output",
want: []string{
"mariadb:mariadb-dbaas",
},
},
{
name: "test3 - override provider to non-existent should result in failing dbaas check and a single pod no dbaas found",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{Name: "LAGOON_DBAAS_ENVIRONMENT_TYPES", Value: "mariadb:development2", Scope: "build"},
},
}, true),
templatePath: "testdata/output",
want: []string{},
},
{
name: "test4 - mariadb-single to mariadb-dbaas (using mariadb-shared to mariadb-dbaas conversion)",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{Name: "LAGOON_SERVICE_TYPES", Value: "mariadb:mariadb-shared", Scope: "build"},
},
}, true),
templatePath: "testdata/output",
want: []string{
"mariadb:mariadb-dbaas",
},
},
{
name: "test5 - multiple mariadb",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.multidb.yml",
}, true),
templatePath: "testdata/output",
want: []string{
"mariadb:mariadb-dbaas",
"mariadb2:mariadb-dbaas",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
savedTemplates := tt.templatePath
generator, err := testdata.SetupEnvironment(*rootCmd, savedTemplates, tt.args)
if err != nil {
t.Errorf("%v", err)
}
for _, envVar := range tt.vars {
err = os.Setenv(envVar.Name, envVar.Value)
if err != nil {
t.Errorf("%v", err)
}
}

// setup the fake dbaas server
ts := dbaasclient.TestDBaaSHTTPServer()
defer ts.Close()
err = os.Setenv("DBAAS_OPERATOR_HTTP", ts.URL)
if err != nil {
t.Errorf("%v", err)
}

got, err := IdentifyDBaaSConsumers(generator)
if (err != nil) != tt.wantErr {
t.Errorf("IdentifyDBaaSConsumers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("IdentifyDBaaSConsumers() = %v, want %v", got, tt.want)
}
t.Cleanup(func() {
helpers.UnsetEnvVars(tt.vars)
})
})
}
}
1 change: 1 addition & 0 deletions cmd/identify_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,6 @@ func CreatedIngressIdentification(g generator.GeneratorInput) ([]string, []strin
func init() {
identifyCmd.AddCommand(primaryIngressIdentify)
identifyCmd.AddCommand(ingressIdentify)
identifyCmd.AddCommand(dbaasIdentify)
identifyCmd.AddCommand(autogenIngressIdentify)
}
3 changes: 3 additions & 0 deletions cmd/template_dbaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func DBaaSTemplateGeneration(g generator.GeneratorInput,
return fmt.Errorf("couldn't generate template: %v", err)
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, "dbaas"), templateYAML)
if g.Debug {
fmt.Println(fmt.Sprintf("Templating dbaas consumers to %s", fmt.Sprintf("%s/%s.yaml", savedTemplates, "dbaas")))
}
return nil
}

Expand Down
28 changes: 28 additions & 0 deletions cmd/template_dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/uselagoon/build-deploy-tool/internal/dbaasclient"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
"github.com/uselagoon/build-deploy-tool/internal/testdata"
)

Expand All @@ -32,6 +33,33 @@ func TestDBaaSTemplateGeneration(t *testing.T) {
templatePath: "testdata/output",
want: "../internal/testdata/complex/dbaas-templates/dbaas-1",
},
{
name: "test2 - mariadb-single to mariadb-dbaas (using mariadb-shared to mariadb-dbaas conversion)",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{Name: "LAGOON_SERVICE_TYPES", Value: "mariadb:mariadb-shared", Scope: "build"},
},
}, true),
templatePath: "testdata/output",
want: "../internal/testdata/complex/dbaas-templates/dbaas-2",
},
{
name: "test3 - multiple mariadb",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.multidb.yml",
}, true),
templatePath: "testdata/output",
want: "../internal/testdata/complex/dbaas-templates/dbaas-3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion internal/generator/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type ServiceValues struct {
AutogeneratedRoutesTLSAcme bool `json:"autogeneratedRoutesTLSAcme"`
AutogeneratedRouteDomain string `json:"autogeneratedRouteDomain"`
ShortAutogeneratedRouteDomain string `json:"shortAutogeneratedRouteDomain"`
DBaaSEnvironment string `json:"dbaasEnvironment"`
NativeCronjobs map[string]CronjobValues `json:"nativeCronjobs"`
InPodCronjobs string `json:"inPodCronjobs"`
ImageName string `json:"imageName"`
Expand All @@ -92,6 +91,8 @@ type ServiceValues struct {
CronjobTolerations *[]corev1.Toleration `json:"cronjobTolerations"`
CronjobAffinity *corev1.Affinity `json:"cronjobAffinity"`
DBaasReadReplica bool `json:"dBaasReadReplica"`
DBaaSEnvironment string `json:"dbaasEnvironment"`
IsDBaaS bool `json:"isDBaaS"`
BackupsEnabled bool `json:"backupsEnabled"`
}

Expand Down
33 changes: 24 additions & 9 deletions internal/generator/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ var supportedAutogeneratedTypes = []string{
"python",
}

// these are lagoon types that support autogenerated routes
var supportedDBTypes = []string{
"mariadb",
"mariadb-dbaas",
"postgres",
"postgres-dbaas",
"mongodb",
"mongodb-dbaas",
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
}

// these are lagoon types that come with resources requiring backups
var typesWithBackups = []string{
"basic-persistent",
Expand All @@ -57,35 +67,35 @@ var typesWithBackups = []string{

// just some default values for services
var defaultServiceValues = map[string]map[string]string{
"elasticsearch": map[string]string{
"elasticsearch": {
"persistentPath": "/usr/share/elasticsearch/data",
"persistentSize": "5Gi",
},
"opensearch": map[string]string{
"opensearch": {
"persistentPath": "/usr/share/opensearch/data",
"persistentSize": "5Gi",
},
"mariadb-single": map[string]string{
"mariadb-single": {
"persistentPath": "/var/lib/mysql",
"persistentSize": "5Gi",
},
"postgres-single": map[string]string{
"postgres-single": {
"persistentPath": "/var/lib/postgresql/data",
"persistentSize": "5Gi",
},
"mongodb-single": map[string]string{
"mongodb-single": {
"persistentPath": "/data/db",
"persistentSize": "5Gi",
},
"varnish-persistent": map[string]string{
"varnish-persistent": {
"persistentPath": "/var/cache/varnish",
"persistentSize": "5Gi",
},
"rabbitmq": map[string]string{
"rabbitmq": {
"persistentPath": "/var/lib/rabbitmq",
"persistentSize": "5Gi",
},
"redis-persistent": map[string]string{
"redis-persistent": {
"persistentPath": "/data",
"persistentSize": "5Gi",
},
Expand Down Expand Up @@ -274,7 +284,10 @@ func composeToServiceValues(

// handle dbaas operator checks here
dbaasEnvironment := buildValues.EnvironmentType
if lagoonType == "mariadb" || lagoonType == "postgres" || lagoonType == "mongodb" {
svcIsDBaaS := false
if helpers.Contains(supportedDBTypes, lagoonType) {
// strip the dbaas off the supplied type for checking against providers, it gets added again later
lagoonType = strings.Split(lagoonType, "-dbaas")[0]
err := buildValues.DBaaSClient.CheckHealth(buildValues.DBaaSOperatorEndpoint)
if err != nil {
// @TODO eventually this error should be handled and fail a build, with a flag to override https://github.com/uselagoon/build-deploy-tool/issues/56
Expand Down Expand Up @@ -321,6 +334,7 @@ func composeToServiceValues(
// if the requested dbaas environment exists, then set the type to be the requested type with `-dbaas`
if exists {
lagoonType = fmt.Sprintf("%s-dbaas", lagoonType)
svcIsDBaaS = true
} else {
// otherwise fallback to -single (if DBaaSFallbackSingle is enabled, otherwise it will error out prior)
lagoonType = fmt.Sprintf("%s-single", lagoonType)
Expand Down Expand Up @@ -352,6 +366,7 @@ func composeToServiceValues(
PersistentVolumePath: servicePersistentPath,
PersistentVolumeName: servicePersistentName,
PersistentVolumeSize: servicePersistentSize,
IsDBaaS: svcIsDBaaS,
BackupsEnabled: backupsEnabled,
}
// check if the service has a service port override (this only applies to basic(-persistent))
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func Test_composeToServiceValues(t *testing.T) {
AutogeneratedRoutesEnabled: false,
AutogeneratedRoutesTLSAcme: false,
DBaaSEnvironment: "development",
IsDBaaS: true,
BackupsEnabled: true,
},
},
Expand Down Expand Up @@ -433,6 +434,7 @@ func Test_composeToServiceValues(t *testing.T) {
AutogeneratedRoutesEnabled: false,
AutogeneratedRoutesTLSAcme: false,
DBaaSEnvironment: "development",
IsDBaaS: true,
BackupsEnabled: true,
},
},
Expand Down
Loading
Loading