Skip to content

Commit

Permalink
refactor: consolidate template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Dec 15, 2024
1 parent 3668bc0 commit 654c6be
Show file tree
Hide file tree
Showing 99 changed files with 483 additions and 249 deletions.
2 changes: 1 addition & 1 deletion cmd/identify_lagoonservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

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

type identifyServices struct {
Expand Down
12 changes: 9 additions & 3 deletions cmd/template_autogen_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
ingresstemplate "github.com/uselagoon/build-deploy-tool/internal/templating/ingress"
servicestemplates "github.com/uselagoon/build-deploy-tool/internal/templating"
"sigs.k8s.io/yaml"
)

var autogenRouteGeneration = &cobra.Command{
Expand All @@ -31,17 +32,22 @@ func AutogeneratedIngressGeneration(g generator.GeneratorInput) error {
return err
}
savedTemplates := g.SavedTemplatesPath

separator := []byte("---\n")
// generate the templates
for _, route := range lagoonBuild.AutogeneratedRoutes.Routes {
// autogenerated routes use the `servicename` as the name of the ingress resource, use `IngressName` in routev2 to handle this
if g.Debug {
fmt.Printf("Templating autogenerated ingress manifest for %s to %s\n", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.LagoonService))
}
templateYAML, err := ingresstemplate.GenerateIngressTemplate(route, *lagoonBuild.BuildValues)
ingress, err := servicestemplates.GenerateIngressTemplate(route, *lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
iBytes, err := yaml.Marshal(ingress)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
templateYAML := append(separator[:], iBytes[:]...)
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, route.LagoonService), templateYAML)
}

Expand Down
16 changes: 12 additions & 4 deletions cmd/template_backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
backuptemplate "github.com/uselagoon/build-deploy-tool/internal/templating/backups"
servicestemplates "github.com/uselagoon/build-deploy-tool/internal/templating"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -69,16 +69,24 @@ func BackupTemplateGeneration(g generator.GeneratorInput,
lagoonBuild.BuildValues.Services = repServices

// generate the backup schedule templates
templateYAML, err := backuptemplate.GenerateBackupSchedule(*lagoonBuild.BuildValues)
schedules, err := servicestemplates.GenerateBackupSchedule(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
templateYAML, err := servicestemplates.TemplateSchedules(schedules)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
if len(templateYAML) > 0 {
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, "k8up-lagoon-backup-schedule"), templateYAML)
}

templateYAML = nil
// generate any prebackuppod templates
templateYAML, err = backuptemplate.GeneratePreBackupPod(*lagoonBuild.BuildValues)
pbps, err := servicestemplates.GeneratePreBackupPod(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
templateYAML, err = servicestemplates.TemplatePreBackupPods(pbps)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/template_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"testing"

"github.com/andreyvit/diff"
"github.com/uselagoon/build-deploy-tool/internal/dbaasclient"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
Expand Down Expand Up @@ -328,8 +329,7 @@ func TestBackupTemplateGeneration(t *testing.T) {
t.Errorf("couldn't read file %v: %v", tt.want, err)
}
if !reflect.DeepEqual(f1, r1) {
fmt.Println(string(f1))
t.Errorf("resulting templates do not match")
t.Errorf("BackupTemplateGeneration() = \n%v", diff.LineDiff(string(r1), string(f1)))
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/template_dbaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
dbaasTemplater "github.com/uselagoon/build-deploy-tool/internal/templating/dbaas"
servicestemplates "github.com/uselagoon/build-deploy-tool/internal/templating"
)

var dbaasGeneration = &cobra.Command{
Expand All @@ -33,7 +33,11 @@ func DBaaSTemplateGeneration(g generator.GeneratorInput,
}
savedTemplates := g.SavedTemplatesPath

templateYAML, err := dbaasTemplater.GenerateDBaaSTemplate(*lagoonBuild.BuildValues)
dbaas, err := servicestemplates.GenerateDBaaSTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
templateYAML, err := servicestemplates.TemplateConsumers(dbaas)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/template_dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"testing"

"github.com/andreyvit/diff"
"github.com/uselagoon/build-deploy-tool/internal/dbaasclient"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
Expand Down Expand Up @@ -184,8 +185,7 @@ func TestDBaaSTemplateGeneration(t *testing.T) {
t.Errorf("couldn't read file %v: %v", tt.want, err)
}
if !reflect.DeepEqual(f1, r1) {
fmt.Println(string(f1))
t.Errorf("resulting templates do not match")
t.Errorf("DBaaSTemplateGeneration() = \n%v", diff.LineDiff(string(r1), string(f1)))
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions cmd/template_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
ingresstemplate "github.com/uselagoon/build-deploy-tool/internal/templating/ingress"
servicestemplates "github.com/uselagoon/build-deploy-tool/internal/templating"
)

var routeGeneration = &cobra.Command{
Expand All @@ -31,13 +31,16 @@ func IngressTemplateGeneration(g generator.GeneratorInput) error {
return err
}
savedTemplates := g.SavedTemplatesPath

// generate the templates
for _, route := range lagoonBuild.MainRoutes.Routes {
if g.Debug {
fmt.Printf("Templating ingress manifest for %s to %s\n", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain))
}
templateYAML, err := ingresstemplate.GenerateIngressTemplate(route, *lagoonBuild.BuildValues)
ingress, err := servicestemplates.GenerateIngressTemplate(route, *lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
templateYAML, err := servicestemplates.TemplateIngress(ingress)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
Expand All @@ -53,7 +56,11 @@ func IngressTemplateGeneration(g generator.GeneratorInput) error {
if g.Debug {
fmt.Printf("Templating active/standby ingress manifest for %s to %s\n", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain))
}
templateYAML, err := ingresstemplate.GenerateIngressTemplate(route, *lagoonBuild.BuildValues)
ingress, err := servicestemplates.GenerateIngressTemplate(route, *lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
templateYAML, err := servicestemplates.TemplateIngress(ingress)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
Expand Down
44 changes: 15 additions & 29 deletions cmd/template_lagoonservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/templating/networkpolicy"
"github.com/uselagoon/build-deploy-tool/internal/templating/registrysecret"
servicestemplates "github.com/uselagoon/build-deploy-tool/internal/templating/services"
servicestemplates "github.com/uselagoon/build-deploy-tool/internal/templating"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -62,102 +60,90 @@ func LagoonServiceTemplateGeneration(g generator.GeneratorInput) error {
savedTemplates := g.SavedTemplatesPath

// generate the templates
secrets, err := registrysecret.GenerateRegistrySecretTemplate(*lagoonBuild.BuildValues)
secrets, err := servicestemplates.GenerateRegistrySecretTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
for _, secret := range secrets {
serviceBytes, err := yaml.Marshal(secret)
templateBytes, err := servicestemplates.TemplateSecret(secret)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
separator := []byte("---\n")
restoreResult := append(separator[:], serviceBytes[:]...)
if g.Debug {
fmt.Printf("Templating registry secret manifests %s\n", fmt.Sprintf("%s/%s.yaml", savedTemplates, secret.Name))
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, secret.Name), restoreResult)
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, secret.Name), templateBytes)
}
services, err := servicestemplates.GenerateServiceTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
for _, d := range services {
serviceBytes, err := yaml.Marshal(d)
templateBytes, err := servicestemplates.TemplateService(d)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
separator := []byte("---\n")
restoreResult := append(separator[:], serviceBytes[:]...)
if g.Debug {
fmt.Printf("Templating service manifests %s\n", fmt.Sprintf("%s/service-%s.yaml", savedTemplates, d.Name))
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/service-%s.yaml", savedTemplates, d.Name), restoreResult)
helpers.WriteTemplateFile(fmt.Sprintf("%s/service-%s.yaml", savedTemplates, d.Name), templateBytes)
}
pvcs, err := servicestemplates.GeneratePVCTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
for _, d := range pvcs {
serviceBytes, err := yaml.Marshal(d)
templateBytes, err := servicestemplates.TemplatePVC(d)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
separator := []byte("---\n")
restoreResult := append(separator[:], serviceBytes[:]...)
if g.Debug {
fmt.Printf("Templating pvc manifests %s\n", fmt.Sprintf("%s/pvc-%s.yaml", savedTemplates, d.Name))
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/pvc-%s.yaml", savedTemplates, d.Name), restoreResult)
helpers.WriteTemplateFile(fmt.Sprintf("%s/pvc-%s.yaml", savedTemplates, d.Name), templateBytes)
}
deployments, err := servicestemplates.GenerateDeploymentTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
for _, d := range deployments {
deploymentBytes, err := yaml.Marshal(d)
templateBytes, err := servicestemplates.TemplateDeployment(d)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
separator := []byte("---\n")
restoreResult := append(separator[:], deploymentBytes[:]...)
if g.Debug {
fmt.Printf("Templating deployment manifests %s\n", fmt.Sprintf("%s/deployment-%s.yaml", savedTemplates, d.Name))
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/deployment-%s.yaml", savedTemplates, d.Name), restoreResult)
helpers.WriteTemplateFile(fmt.Sprintf("%s/deployment-%s.yaml", savedTemplates, d.Name), templateBytes)
}
cronjobs, err := servicestemplates.GenerateCronjobTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
for _, d := range cronjobs {
deploymentBytes, err := yaml.Marshal(d)
templateBytes, err := servicestemplates.TemplateCronjobs(d)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
separator := []byte("---\n")
restoreResult := append(separator[:], deploymentBytes[:]...)
if g.Debug {
fmt.Printf("Templating cronjob manifests %s\n", fmt.Sprintf("%s/cronjob-%s.yaml", savedTemplates, d.Name))
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/cronjob-%s.yaml", savedTemplates, d.Name), restoreResult)
helpers.WriteTemplateFile(fmt.Sprintf("%s/cronjob-%s.yaml", savedTemplates, d.Name), templateBytes)
}
if lagoonBuild.BuildValues.IsolationNetworkPolicy {
// if isolation network policies are enabled, template that here
np, err := networkpolicy.GenerateNetworkPolicy(*lagoonBuild.BuildValues)
np, err := servicestemplates.GenerateNetworkPolicy(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
npBytes, err := yaml.Marshal(np)
templateBytes, err := servicestemplates.TemplateNetworkPolicy(np)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
separator := []byte("---\n")
restoreResult := append(separator[:], npBytes[:]...)
if g.Debug {
fmt.Printf("Templating networkpolicy manifest %s\n", fmt.Sprintf("%s/isolation-network-policy.yaml", savedTemplates))
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/isolation-network-policy.yaml", savedTemplates), restoreResult)
helpers.WriteTemplateFile(fmt.Sprintf("%s/isolation-network-policy.yaml", savedTemplates), templateBytes)
}
return nil
}
Expand Down
Loading

0 comments on commit 654c6be

Please sign in to comment.