Skip to content

Commit

Permalink
Merge pull request #25 from uselagoon/custom-ingress-altnames
Browse files Browse the repository at this point in the history
Feature: add alternative names and wildcard support to lagoon.yml
  • Loading branch information
tobybellwood authored Oct 9, 2023
2 parents 8140f18 + 3a94837 commit 7c43693
Show file tree
Hide file tree
Showing 17 changed files with 1,183 additions and 299 deletions.
38 changes: 38 additions & 0 deletions cmd/template_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,44 @@ func TestTemplateRoutes(t *testing.T) {
},
want: "../test-resources/template-ingress/test20-results",
},
{
name: "test21 alternative names",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
standbyEnvironment: "main2",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "../test-resources/template-ingress/test21/lagoon.yml",
templatePath: "../test-resources/template-ingress/output",
},
want: "../test-resources/template-ingress/test21-results",
},
{
name: "test22 check wildcard",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/template-ingress/test22/lagoon.yml",
templatePath: "../test-resources/template-ingress/output",
},
want: "../test-resources/template-ingress/test22-results",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
29 changes: 22 additions & 7 deletions internal/generator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func generateRoutes(
// generate the templates for these independently of any previously generated routes,
// this WILL overwrite previously created templates ensuring that anything defined in the `production_routes`
// section are created correctly ensuring active/standby will work
*activeStanbyRoutes = generateActiveStandbyRoutes(lagoonEnvVars, lYAML, buildValues)
*activeStanbyRoutes, err = generateActiveStandbyRoutes(lagoonEnvVars, lYAML, buildValues)
if err != nil {
return "", []string{}, []string{}, fmt.Errorf("couldn't generate and merge routes: %v", err)
}
// get the first route from the list of routes, replace the previous one if necessary
if len(activeStanbyRoutes.Routes) > 0 {
// if primary != "" {
Expand Down Expand Up @@ -244,14 +247,17 @@ func generateActiveStandbyRoutes(
envVars []lagoon.EnvironmentVariable,
lagoonYAML lagoon.YAML,
buildValues BuildValues,
) lagoon.RoutesV2 {
) (lagoon.RoutesV2, error) {
activeStanbyRoutes := &lagoon.RoutesV2{}
if lagoonYAML.ProductionRoutes != nil {
if buildValues.IsActiveEnvironment == true {
if lagoonYAML.ProductionRoutes.Active != nil {
if lagoonYAML.ProductionRoutes.Active.Routes != nil {
for _, routeMap := range lagoonYAML.ProductionRoutes.Active.Routes {
lagoon.GenerateRoutesV2(activeStanbyRoutes, routeMap, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix, true)
err := lagoon.GenerateRoutesV2(activeStanbyRoutes, routeMap, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix, true)
if err != nil {
return *activeStanbyRoutes, err
}
}
}
}
Expand All @@ -260,13 +266,16 @@ func generateActiveStandbyRoutes(
if lagoonYAML.ProductionRoutes.Standby != nil {
if lagoonYAML.ProductionRoutes.Standby.Routes != nil {
for _, routeMap := range lagoonYAML.ProductionRoutes.Standby.Routes {
lagoon.GenerateRoutesV2(activeStanbyRoutes, routeMap, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix, true)
err := lagoon.GenerateRoutesV2(activeStanbyRoutes, routeMap, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix, true)
if err != nil {
return *activeStanbyRoutes, err
}
}
}
}
}
}
return *activeStanbyRoutes
return *activeStanbyRoutes, nil
}

// getRoutesFromEnvVar will collect the value of the LAGOON_ROUTES_JSON
Expand Down Expand Up @@ -305,9 +314,15 @@ func generateAndMerge(

// otherwise it just uses the default environment name
for _, routeMap := range lagoonYAML.Environments[buildValues.Branch].Routes {
lagoon.GenerateRoutesV2(n, routeMap, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix, false)
err := lagoon.GenerateRoutesV2(n, routeMap, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix, false)
if err != nil {
return *n, err
}
}
// merge routes from the API on top of the routes from the `.lagoon.yml`
mainRoutes := lagoon.MergeRoutesV2(*n, api, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix)
mainRoutes, err := lagoon.MergeRoutesV2(*n, api, envVars, buildValues.IngressClass, buildValues.FastlyAPISecretPrefix)
if err != nil {
return *n, err
}
return mainRoutes, nil
}
Loading

0 comments on commit 7c43693

Please sign in to comment.