Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cmd/template_autogen_ingress_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/base64"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -551,6 +552,44 @@ func TestAutogeneratedIngressGeneration(t *testing.T) {
templatePath: "testdata/output",
want: "internal/testdata/basic/autogen-templates/test29-autogenerated-pathroutes",
},
{
name: "autogenerated-routes-api-config-against-existng-reuslt",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_API_AUTOGENERATED_CONFIG",
Value: base64.URLEncoding.EncodeToString([]byte(`{"autogenerate":{"enabled":true,"allowPullRequests":true,"insecure":"Redirect"},"environment":{"autogenerateRoutes":true}}`)),
Scope: "internal_system",
},
},
}, true),
templatePath: "testoutput",
want: "internal/testdata/node/autogen-templates/ingress-1",
},
{
name: "autogenerated-routes-api-config",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_API_AUTOGENERATED_CONFIG",
Value: base64.URLEncoding.EncodeToString([]byte(`{"autogenerate":{"enabled":true,"allowPullRequests":true,"prefixes":["ie","de","abc"],"pathRoutes":[{"toService":"node","fromService":"node","path":"/"}],"insecure":"Redirect"},"environment":{"autogenerateRoutes":true,"autogeneratePathRoutes":[{"toService":"node","fromService":"node","path":"/"}]}}`)),
Scope: "internal_system",
},
},
}, true),
templatePath: "testoutput",
want: "internal/testdata/node/autogen-templates/autogenerated-routes-api-config",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
72 changes: 72 additions & 0 deletions cmd/template_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,78 @@ func TestTemplateRoutes(t *testing.T) {
templatePath: "testoutput",
want: "internal/testdata/node/ingress-templates/ingress-24",
},
{
name: "active-standby-api-routes",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "prod-left",
Branch: "prod-left",
LagoonYAML: "internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_API_ROUTES",
Value: base64.URLEncoding.EncodeToString([]byte(`{"routes":[
{"type":"ACTIVE", "domain":"active.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":false,"source":"API"},
{"domain":"other.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":false,"source":"API"},
{"domain":"test.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":true,"source":"API"}
]}`)),
Scope: "internal_system",
},
},
}, true),
templatePath: "testoutput",
want: "internal/testdata/node/ingress-templates/active-standby-api-routes",
},
{
name: "api-defined-routes-with-lagoon-yml",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_API_ROUTES",
Value: base64.URLEncoding.EncodeToString([]byte(`{"routes":[
{"domain":"other.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":false,"source":"API"},
{"domain":"test.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":true,"source":"API"}
]}`)),
Scope: "internal_system",
},
},
}, true),
templatePath: "testoutput",
want: "internal/testdata/node/ingress-templates/api-defined-routes-with-lagoon-yml",
},
{
name: "api-defined-routes-with-lagoon-yml-fastly",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FASTLY_SERVICE_ID",
Value: "service-id:true",
Scope: "build",
},
{
Name: "LAGOON_API_ROUTES",
Value: base64.URLEncoding.EncodeToString([]byte(`{"routes":[
{"domain":"other.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":false,"source":"API"},
{"domain":"test.example.com","service":"node","alternativeNames":[],"annotations":{},"pathRoutes":[],"tlsAcme":true,"insecure":"Redirect","hstsEnabled":false,"hstsIncludeSubdomains":false,"hstsPreload":false,"hstsMaxAge":3153600,"primary":true,"source":"API"}
]}`)),
Scope: "internal_system",
},
},
}, true),
templatePath: "testoutput",
want: "internal/testdata/node/ingress-templates/api-defined-routes-with-lagoon-yml-fastly",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions internal/collector/testdata/json-result/result-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@
"lagoon.sh/project": "example-project",
"lagoon.sh/service": "example.com",
"lagoon.sh/service-type": "custom-ingress",
"lagoon.sh/template": "custom-ingress-0.1.0"
"lagoon.sh/template": "custom-ingress-0.1.0",
"route.lagoon.sh/source": "yaml"
},
"annotations": {
"fastly.amazee.io/service-id": "service-id",
Expand Down Expand Up @@ -386,7 +387,8 @@
"lagoon.sh/project": "example-project",
"lagoon.sh/service": "node",
"lagoon.sh/service-type": "node",
"lagoon.sh/template": "autogenerated-ingress-0.1.0"
"lagoon.sh/template": "autogenerated-ingress-0.1.0",
"route.lagoon.sh/source": "autogenerated"
},
"annotations": {
"fastly.amazee.io/watch": "false",
Expand Down
6 changes: 4 additions & 2 deletions internal/collector/testdata/json-result/result-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@
"lagoon.sh/project": "example-project",
"lagoon.sh/service": "example.com",
"lagoon.sh/service-type": "custom-ingress",
"lagoon.sh/template": "custom-ingress-0.1.0"
"lagoon.sh/template": "custom-ingress-0.1.0",
"route.lagoon.sh/source": "yaml"
},
"annotations": {
"fastly.amazee.io/service-id": "service-id",
Expand Down Expand Up @@ -261,7 +262,8 @@
"lagoon.sh/project": "example-project",
"lagoon.sh/service": "node",
"lagoon.sh/service-type": "node",
"lagoon.sh/template": "autogenerated-ingress-0.1.0"
"lagoon.sh/template": "autogenerated-ingress-0.1.0",
"route.lagoon.sh/source": "autogenerated"
},
"annotations": {
"fastly.amazee.io/watch": "false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ items:
lagoon.sh/service: example.com
lagoon.sh/service-type: custom-ingress
lagoon.sh/template: custom-ingress-0.1.0
route.lagoon.sh/source: yaml
name: example.com
namespace: example-project-main
resourceVersion: "1"
Expand Down Expand Up @@ -79,6 +80,7 @@ items:
lagoon.sh/service: node
lagoon.sh/service-type: node
lagoon.sh/template: autogenerated-ingress-0.1.0
route.lagoon.sh/source: autogenerated
name: node
namespace: example-project-main
resourceVersion: "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ items:
lagoon.sh/service: example.com
lagoon.sh/service-type: custom-ingress
lagoon.sh/template: custom-ingress-0.1.0
route.lagoon.sh/source: yaml
name: example.com
namespace: example-project-main
resourceVersion: "1"
Expand Down Expand Up @@ -79,6 +80,7 @@ items:
lagoon.sh/service: node
lagoon.sh/service-type: node
lagoon.sh/template: autogenerated-ingress-0.1.0
route.lagoon.sh/source: autogenerated
name: node
namespace: example-project-main
resourceVersion: "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ metadata:
lagoon.sh/service: example.com
lagoon.sh/service-type: custom-ingress
lagoon.sh/template: custom-ingress-0.1.0
route.lagoon.sh/source: yaml
name: example.com
spec:
rules:
Expand Down
1 change: 1 addition & 0 deletions internal/collector/testdata/seed/seed-1/ingress-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ metadata:
lagoon.sh/service: node
lagoon.sh/service-type: node
lagoon.sh/template: autogenerated-ingress-0.1.0
route.lagoon.sh/source: autogenerated
name: node
spec:
rules:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ metadata:
lagoon.sh/service: example.com
lagoon.sh/service-type: custom-ingress
lagoon.sh/template: custom-ingress-0.1.0
route.lagoon.sh/source: yaml
name: example.com
spec:
rules:
Expand Down
1 change: 1 addition & 0 deletions internal/collector/testdata/seed/seed-2/ingress-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ metadata:
lagoon.sh/service: node
lagoon.sh/service-type: node
lagoon.sh/template: autogenerated-ingress-0.1.0
route.lagoon.sh/source: autogenerated
name: node
spec:
rules:
Expand Down
10 changes: 10 additions & 0 deletions internal/generator/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,13 @@ type PruneRetention struct {
Weekly int `json:"weekly"`
Monthly int `json:"monthly"`
}

type AutogeneratedRouteConfig struct {
Environment *EnvironmentAutogeneratedRouteConfig `json:"environment,omitempty"`
Autogenerate *lagoon.Autogenerate `json:"autogenerate"`
}

type EnvironmentAutogeneratedRouteConfig struct {
AutogenerateRoutes *bool `json:"autogenerateRoutes"`
AutogeneratedPathRoutes *[]lagoon.AutogeneratePathRoute `json:"autogeneratedPathRoutes,omitempty"`
}
45 changes: 45 additions & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
Expand Down Expand Up @@ -283,6 +284,50 @@ func NewGenerator(
buildValues.LagoonVersion = lagoonCoreVersion.Value
}

lagoonAutogeneratedRouteConfig, _ := lagoon.GetLagoonVariable("LAGOON_API_AUTOGENERATED_CONFIG", []string{"internal_system"}, buildValues.EnvironmentVariables)
if lagoonAutogeneratedRouteConfig != nil {
autogenConfig := &AutogeneratedRouteConfig{}
rawJSONStr, _ := base64.StdEncoding.DecodeString(lagoonAutogeneratedRouteConfig.Value)
rawJSON := []byte(rawJSONStr)
err := json.Unmarshal(rawJSON, autogenConfig)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal autogenerated route config from Lagoon API, is it actually JSON that has been base64 encoded?: %v", err)
}
environment := lagoon.Environment{}
if buildValues.LagoonYAML.Environments == nil {
buildValues.LagoonYAML.Environments = make(lagoon.Environments)
} else {
environment = buildValues.LagoonYAML.Environments[buildValues.Branch]
}
if autogenConfig.Autogenerate != nil {
// replace the project autogenerated routes config with one from the api
// this replaces the entire configuration that would be defined in the .lagoon.yml file
// thus, the .lagoon.yml is ignored for the `routes.autogenerate` stanza
// if some options that were defined in the .lagoon.yml file differ from what is in the api
// then those changes will need to be made in the api to remain in effect,
// otherwise the `.lagoon.yml` defaults will apply
// for those values not set in the api
buildValues.LagoonYAML.Routes.Autogenerate = *autogenConfig.Autogenerate
// overwrite the environment with the project values, autogenerated route config
environment.AutogeneratePathRoutes = autogenConfig.Autogenerate.PathRoutes
environment.AutogenerateRoutes = autogenConfig.Autogenerate.Enabled
buildValues.LagoonYAML.Environments[buildValues.Branch] = environment
}
if autogenConfig.Environment != nil {
// if the environment section is provided, then this will override anything that may have been provided in the
// environments.${name} stanza related to autogenerated routes
if autogenConfig.Environment.AutogeneratedPathRoutes != nil {
// replace environment autogenerated path routes with any from the api
environment.AutogeneratePathRoutes = *autogenConfig.Environment.AutogeneratedPathRoutes
}
if autogenConfig.Environment.AutogenerateRoutes != nil {
// replace environment autogenerated routes enabled with the value from the api if defined
environment.AutogenerateRoutes = autogenConfig.Environment.AutogenerateRoutes
}
buildValues.LagoonYAML.Environments[buildValues.Branch] = environment
}
}

// handle generating the container registry login generation here, extract from the `.lagoon.yml` firstly
if err := configureContainerRegistries(&buildValues); err != nil {
return nil, err
Expand Down
Loading
Loading