From ed6a1bbf788408b3fafc3c05d3ff1e3a21a608b1 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Wed, 18 May 2022 11:07:13 +1000 Subject: [PATCH] refactor: move all the autogenerated route checks into the compose2services function --- cmd/helpers_values.go | 56 +++++++++++++++++++++------- cmd/template_autogen_ingress.go | 35 ++--------------- cmd/template_autogen_ingress_test.go | 2 +- internal/lagoon/buildvalues.go | 2 +- internal/lagoon/variables.go | 2 +- 5 files changed, 49 insertions(+), 48 deletions(-) diff --git a/cmd/helpers_values.go b/cmd/helpers_values.go index 9dc23566..1f4d045c 100644 --- a/cmd/helpers_values.go +++ b/cmd/helpers_values.go @@ -113,7 +113,7 @@ func collectBuildValues(debug bool, activeEnv, standbyEnv *bool, lagoonServiceTypes, _ := lagoon.GetLagoonVariable("LAGOON_SERVICE_TYPES", []string{"build"}, *lagoonEnvVars) // convert docker-compose services to servicevalues for csName, csValues := range lCompose.Services { - cService, err := composeToServiceValues(lYAML, lagoonServiceTypes, csName, csValues) + cService, err := composeToServiceValues(lYAML, lagoonValues, lagoonServiceTypes, csName, csValues) if err != nil { return err } @@ -122,8 +122,31 @@ func collectBuildValues(debug bool, activeEnv, standbyEnv *bool, return nil } -func composeToServiceValues(lYAML *lagoon.YAML, lagoonServiceTypes *lagoon.EnvironmentVariable, csName string, csValues lagoon.Service) (lagoon.ServiceValues, error) { +func composeToServiceValues(lYAML *lagoon.YAML, lagoonValues *lagoon.BuildValues, lagoonServiceTypes *lagoon.EnvironmentVariable, csName string, csValues lagoon.Service) (lagoon.ServiceValues, error) { lagoonType := lagoon.CheckServiceLagoonLabel(csValues.Labels, "lagoon.type") + autogenEnabled := true + // check if autogenerated routes are disabled + if lYAML.Routes.Autogenerate.Enabled != nil { + if *lYAML.Routes.Autogenerate.Enabled == false { + autogenEnabled = false + } + } + // check if pullrequests autogenerated routes are disabled + if lagoonValues.BuildType == "pullrequest" && lYAML.Routes.Autogenerate.AllowPullRequests != nil { + if *lYAML.Routes.Autogenerate.AllowPullRequests == false { + autogenEnabled = false + } else { + autogenEnabled = true + } + } + // check if this environment has autogenerated routes disabled + if lYAML.Environments[lagoonValues.Branch].AutogenerateRoutes != nil { + if *lYAML.Environments[lagoonValues.Branch].AutogenerateRoutes == false { + autogenEnabled = false + } else { + autogenEnabled = true + } + } if lagoonType != "" { if value, ok := lYAML.Environments[environmentName].Types[csName]; ok { lagoonType = value @@ -137,22 +160,27 @@ func composeToServiceValues(lYAML *lagoon.YAML, lagoonServiceTypes *lagoon.Envir } } } - cService := lagoon.ServiceValues{ - Name: csName, - Type: lagoonType, - } serviceAutogenerated := lagoon.CheckServiceLagoonLabel(csValues.Labels, "lagoon.autogeneratedroute") - if reflect.TypeOf(serviceAutogenerated).Kind() == reflect.String { - vBool, err := strconv.ParseBool(serviceAutogenerated) - if err == nil { - cService.AutogeneratedRoutes = &vBool + if serviceAutogenerated != "" { + if reflect.TypeOf(serviceAutogenerated).Kind() == reflect.String { + vBool, err := strconv.ParseBool(serviceAutogenerated) + if err == nil { + autogenEnabled = vBool + } } } lagoonTypeName := lagoon.CheckServiceLagoonLabel(csValues.Labels, "lagoon.name") - if lagoonTypeName != "" && lagoonTypeName != csName { - // if there is a lagoon.name label on a service, then this service does not need an autogenerated route - // as it is likely an ancillary pod for another service (the one in the `lagoon.name` label :)) - cService.AutogeneratedRoutes = helpers.BoolPtr(false) + if lagoonTypeName != "" { + if lagoonTypeName != csName { + // if there is a lagoon.name label on a service, then this service does not need an autogenerated route + // as it is likely an ancillary pod for another service (the one in the `lagoon.name` label :)) + autogenEnabled = false + } + } + cService := lagoon.ServiceValues{ + Name: csName, + Type: lagoonType, + AutogeneratedRoutes: autogenEnabled, } return cService, nil } diff --git a/cmd/template_autogen_ingress.go b/cmd/template_autogen_ingress.go index c92a935c..d118cce9 100644 --- a/cmd/template_autogen_ingress.go +++ b/cmd/template_autogen_ingress.go @@ -72,29 +72,7 @@ func generateAutogenRoutes( lagoonValues lagoon.BuildValues, // lagoonCompose lagoon.Compose, ) (*lagoon.RoutesV2, error) { - autogenEnabled := true - // check if autogenerated routes are disabled - if lagoonYAML.Routes.Autogenerate.Enabled != nil { - if *lagoonYAML.Routes.Autogenerate.Enabled == false { - autogenEnabled = false - } - } - // check if pullrequests autogenerated routes are disabled - if lagoonValues.BuildType == "pullrequest" && lagoonYAML.Routes.Autogenerate.AllowPullRequests != nil { - if *lagoonYAML.Routes.Autogenerate.AllowPullRequests == false { - autogenEnabled = false - } else { - autogenEnabled = true - } - } - // check if this environment has autogenerated routes disabled - if lagoonYAML.Environments[lagoonValues.Branch].AutogenerateRoutes != nil { - if *lagoonYAML.Environments[lagoonValues.Branch].AutogenerateRoutes == false { - autogenEnabled = false - } else { - autogenEnabled = true - } - } + // generate autogenerated routes for the services autogenRoutes := &lagoon.RoutesV2{} @@ -104,14 +82,8 @@ func generateAutogenRoutes( // if the `LAGOON_SYSTEM_ROUTER_PATTERN` exists, generate the routes for serviceName, service := range lagoonValues.Services { // get the service type - - // check if this service has autogenerated routes disabled - if service.AutogeneratedRoutes != nil { - autogenEnabled = *service.AutogeneratedRoutes - } - // if autogenerated routes are enabled, generate them :) - if autogenEnabled { + if service.AutogeneratedRoutes { if helpers.Contains(supportedAutogeneratedTypes, service.Type) { domain, shortDomain := AutogeneratedDomainFromPattern(lagoonRouterPattern.Value, serviceName) serviceValues := lagoon.ServiceValues{ @@ -160,7 +132,8 @@ func generateAutogenRoutes( } return autogenRoutes, nil } - return autogenRoutes, nil + // if there is no LAGOON_SYSTEM_ROUTER_PATTERN found, abort + return autogenRoutes, err } // AutogeneratedDomainFromPattern generates the domain name and the shortened domain name for an autogenerated ingress diff --git a/cmd/template_autogen_ingress_test.go b/cmd/template_autogen_ingress_test.go index 4f90109f..5d0bb5b9 100644 --- a/cmd/template_autogen_ingress_test.go +++ b/cmd/template_autogen_ingress_test.go @@ -325,7 +325,7 @@ func TestAutogeneratedIngressGeneration(t *testing.T) { buildType: "branch", lagoonVersion: "v2.7.x", branch: "main", - projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},]`, + projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`, envVars: `[]`, secretPrefix: "fastly-api-", lagoonYAML: "test-resources/template-autogenerated/single-lagoon-no-type.yml", diff --git a/internal/lagoon/buildvalues.go b/internal/lagoon/buildvalues.go index 6d1e51ad..0d8ec351 100644 --- a/internal/lagoon/buildvalues.go +++ b/internal/lagoon/buildvalues.go @@ -42,7 +42,7 @@ type BuildValues struct { type ServiceValues struct { Name string `json:"name"` Type string `json:"type"` - AutogeneratedRoutes *bool `json:"autogeneratedRoutes"` + AutogeneratedRoutes bool `json:"autogeneratedRoutes"` AutogeneratedRouteDomain string `json:"autogeneratedRouteDomain"` ShortAutogeneratedRouteDomain string `json:"shortAutogeneratedRouteDomain"` DBaaSEnvironment string `json:"dbaasEnvironment"` diff --git a/internal/lagoon/variables.go b/internal/lagoon/variables.go index d7f1d6e7..b3f3fc96 100644 --- a/internal/lagoon/variables.go +++ b/internal/lagoon/variables.go @@ -65,7 +65,7 @@ func GetLagoonVariable(name string, scope []string, variables []EnvironmentVaria return &v, nil } } - return nil, fmt.Errorf("variable not found") + return nil, fmt.Errorf("variable %s not found", name) } // VariableExists checks if a variable exists in a slice of environment variables