Skip to content

Commit

Permalink
refactor: move all the autogenerated route checks into the compose2se…
Browse files Browse the repository at this point in the history
…rvices function
  • Loading branch information
shreddedbacon committed May 18, 2022
1 parent 84f06e7 commit ed6a1bb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
56 changes: 42 additions & 14 deletions cmd/helpers_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
35 changes: 4 additions & 31 deletions cmd/template_autogen_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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{
Expand Down Expand Up @@ -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
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 @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion internal/lagoon/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion internal/lagoon/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed6a1bb

Please sign in to comment.