Skip to content

Commit

Permalink
fix: template creation for dbaas and other fmt fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jun 21, 2024
1 parent 194aabd commit e03b200
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 194 deletions.
4 changes: 0 additions & 4 deletions cmd/config_fastly.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
)

var (
domainName string
)

var fastlyConfigGeneration = &cobra.Command{
Use: "fastly",
Aliases: []string{"f"},
Expand Down
24 changes: 0 additions & 24 deletions cmd/identify_dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@ import (

// 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
Expand Down
24 changes: 0 additions & 24 deletions cmd/identify_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@ import (
)

func TestIdentifyFeatureFlag(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
Expand Down
23 changes: 0 additions & 23 deletions cmd/identify_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,29 +388,6 @@ func TestIdentifyRoute(t *testing.T) {
}

func TestCreatedIngressIdentification(t *testing.T) {
type args struct {
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
Expand Down
30 changes: 14 additions & 16 deletions cmd/identify_lagoonservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,23 @@ func LagoonServiceTemplateIdentification(g generator.GeneratorInput) ([]identify
if err != nil {
return nil, fmt.Errorf("couldn't generate template: %v", err)
}
if deployments != nil {
for _, d := range deployments {
dcs := []containers{}
for _, dc := range d.Spec.Template.Spec.Containers {
dcp := []ports{}
for _, p := range dc.Ports {
dcp = append(dcp, ports{Port: p.ContainerPort})
}
dcs = append(dcs, containers{
Name: dc.Name,
Ports: dcp,
})
for _, d := range deployments {
dcs := []containers{}
for _, dc := range d.Spec.Template.Spec.Containers {
dcp := []ports{}
for _, p := range dc.Ports {
dcp = append(dcp, ports{Port: p.ContainerPort})
}
lServices = append(lServices, identifyServices{
Name: d.Name,
Type: d.ObjectMeta.Labels["lagoon.sh/service-type"],
Containers: dcs,
dcs = append(dcs, containers{
Name: dc.Name,
Ports: dcp,
})
}
lServices = append(lServices, identifyServices{
Name: d.Name,
Type: d.ObjectMeta.Labels["lagoon.sh/service-type"],
Containers: dcs,
})
}
return lServices, nil
}
Expand Down
26 changes: 0 additions & 26 deletions cmd/identify_native_cronjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@ import (
)

func TestIdentifyNativeCronjobs(t *testing.T) {
type args struct {
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
ingressClass string
rootlessWorkloads string
projectVars string
envVars string
lagoonVersion string
lagoonYAML string
valuesFilePath string
templatePath string
imageReferences map[string]string
}
tests := []struct {
name string
args testdata.TestData
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var versionCmd = &cobra.Command{
}

func displayVersionInfo() {
fmt.Println(fmt.Sprintf("%s %s (built: %s / go %s)", bdtName, bdtVersion, bdtBuild, goVersion))
fmt.Printf("%s %s (built: %s / go %s)\n", bdtName, bdtVersion, bdtBuild, goVersion)
}

func init() {
Expand Down
8 changes: 3 additions & 5 deletions cmd/tasks_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/uselagoon/build-deploy-tool/internal/tasklib"
)

var runPreRollout, runPostRollout, outOfClusterConfig bool

const (
preRolloutTasks = iota
postRolloutTasks
Expand All @@ -38,11 +36,11 @@ func unidleThenRun(namespace string, prePost string, incoming lagoon.Task) error
if !incoming.RequiresEnvironment { // we don't have to kill this build if we can't bring the services up, so we just note the issue and continue
fmt.Println("Namespace unidling is taking longer than expected - this might affect pre-rollout tasks that rely on multiple services")
} else {
return fmt.Errorf("Unable to unidle the environment for pre-rollout tasks in time (waited %v seconds, retried %v times) - exiting as the task is defined as requiring the environment to be up.",
return fmt.Errorf("unable to unidle the environment for pre-rollout tasks in time (waited %v seconds, retried %v times) - exiting as the task is defined as requiring the environment to be up",
incoming.ScaleWaitTime, incoming.ScaleMaxIterations)
}
default:
return fmt.Errorf("There was a problem when unidling the environment for pre-rollout tasks: %v", err.Error())
return fmt.Errorf("there was a problem when unidling the environment for pre-rollout tasks: %v", err.Error())
}
}
return runCleanTaskInEnvironment(namespace, prePost, incoming)
Expand Down Expand Up @@ -218,7 +216,7 @@ func evaluateWhenConditionsForTaskInEnvironment(environment tasklib.TaskEnvironm
}
retBool, okay := ret.(bool)
if !okay {
err := fmt.Errorf("Expression doesn't evaluate to a boolean")
err := fmt.Errorf("expression doesn't evaluate to a boolean")
if debug {
fmt.Println(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/template_autogen_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func AutogeneratedIngressGeneration(g generator.GeneratorInput) error {
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.Println(fmt.Sprintf("Templating autogenerated ingress manifest for %s to %s", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.LagoonService)))
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)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/template_autogen_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func TestAutogeneratedIngressGeneration(t *testing.T) {
if err != nil {
t.Errorf("couldn't read file %v: %v", savedTemplates, err)
}
fmt.Println(fmt.Sprintf("%s/%s", savedTemplates, f.Name()))
fmt.Printf("%s/%s\n", savedTemplates, f.Name())
fmt.Println(string(f1))
}
t.Errorf("resulting templates do not match")
Expand Down
8 changes: 5 additions & 3 deletions cmd/template_dbaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ func DBaaSTemplateGeneration(g generator.GeneratorInput,
if err != nil {
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")))
if len(templateYAML) > 0 {
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, "dbaas"), templateYAML)
if g.Debug {
fmt.Printf("Templating dbaas consumers to %s\n", fmt.Sprintf("%s/%s.yaml", savedTemplates, "dbaas"))
}
}
return nil
}
Expand Down
29 changes: 25 additions & 4 deletions cmd/template_dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io/fs"
"os"
"reflect"
"testing"
Expand All @@ -21,6 +22,7 @@ func TestDBaaSTemplateGeneration(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 @@ -104,6 +106,19 @@ func TestDBaaSTemplateGeneration(t *testing.T) {
templatePath: "testdata/output",
want: "internal/testdata/complex/dbaas-templates/dbaas-4",
},
{
name: "test7 - basic - no dbaas",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "internal/testdata/basic/lagoon.yml",
}, true),
templatePath: "testdata/output",
emptyDir: true,
want: "internal/testdata/basic/dbaas-templates/dbaas-1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -135,11 +150,17 @@ func TestDBaaSTemplateGeneration(t *testing.T) {
if err != nil {
t.Errorf("couldn't read directory %v: %v", savedTemplates, err)
}
results, err := os.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
resultSize := 0
results := []fs.DirEntry{}
if !tt.emptyDir {
results, err = os.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) != 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
4 changes: 2 additions & 2 deletions cmd/template_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func IngressTemplateGeneration(g generator.GeneratorInput) error {
// generate the templates
for _, route := range lagoonBuild.MainRoutes.Routes {
if g.Debug {
fmt.Println(fmt.Sprintf("Templating ingress manifest for %s to %s", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain)))
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)
if err != nil {
Expand All @@ -51,7 +51,7 @@ func IngressTemplateGeneration(g generator.GeneratorInput) error {
// generate the templates for active/standby routes separately to normal routes
for _, route := range lagoonBuild.ActiveStandbyRoutes.Routes {
if g.Debug {
fmt.Println(fmt.Sprintf("Templating active/standby ingress manifest for %s to %s", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain)))
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)
if err != nil {
Expand Down
Loading

0 comments on commit e03b200

Please sign in to comment.