Skip to content

Commit

Permalink
Merge pull request #67 from uselagoon/routes-json
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Jul 19, 2022
2 parents 541b877 + 0ccee57 commit cc11c64
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 24 deletions.
21 changes: 20 additions & 1 deletion cmd/template_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func TestTemplateRoutes(t *testing.T) {
want: "../test-resources/template-ingress/test10-results",
},
{
name: "test10 standby no values",
name: "test11 standby no values",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
Expand All @@ -250,6 +250,25 @@ func TestTemplateRoutes(t *testing.T) {
},
want: "../test-resources/template-ingress/test11-results",
},
{
name: "test12 check LAGOON_ROUTES_JSON generates ingress",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_ROUTES_JSON","value":"eyJyb3V0ZXMiOlt7ImRvbWFpbiI6InRlc3QxLmV4YW1wbGUuY29tIiwic2VydmljZSI6Im5naW54IiwidGxzLWFjbWUiOmZhbHNlLCJtb25pdG9yaW5nLXBhdGgiOiIvYnlwYXNzLWNhY2hlIn1dfQo=","scope":"build"},{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FASTLY_SERVICE_IDS","value":"example.com:service-id:true:annotationscom","scope":"build"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "../test-resources/template-ingress/test12/lagoon.yml",
templatePath: "../test-resources/template-ingress/output",
},
want: "../test-resources/template-ingress/test12-results",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
203 changes: 198 additions & 5 deletions internal/generator/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Test_generateAndMerge(t *testing.T) {
want lagoon.RoutesV2
wantErr bool
}{
// TODO: test just API
{
name: "test1 - generate routes from lagoon yaml and merge ones from api onto them",
args: args{
Expand Down Expand Up @@ -156,6 +157,51 @@ func Test_generateAndMerge(t *testing.T) {
},
},
},
{
name: "test2 - don't generate routes from lagoon yaml and only merge ones from api onto them",
args: args{
buildValues: BuildValues{
Branch: "main",
},
lagoonYAML: lagoon.YAML{},
api: lagoon.RoutesV2{
Routes: []lagoon.RouteV2{
{
Domain: "test1.example.com",
Service: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
{
Domain: "a.example.com",
Service: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
},
},
},
want: lagoon.RoutesV2{
Routes: []lagoon.RouteV2{
{
Domain: "test1.example.com",
Service: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
Insecure: helpers.StrPtr("Redirect"),
Annotations: map[string]string{},
},
{
Domain: "a.example.com",
Service: "nginx",
TLSAcme: helpers.BoolPtr(false),
Annotations: map[string]string{},
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/bypass-cache",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -293,24 +339,79 @@ func Test_generateAutogenRoutes(t *testing.T) {
tests := []struct {
name string
args args
want lagoon.RoutesV2
wantErr bool
}{
// TODO: Add test cases.
{
name: "test1",
args: args{
envVars: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_SYSTEM_ROUTER_PATTERN",
Value: "${service}-${project}-${environment}.example.com",
Scope: "internal_system",
},
},
lagoonYAML: &lagoon.YAML{},
buildValues: &BuildValues{
Project: "example-com",
BuildType: "branch",
Environment: "main",
EnvironmentType: "development",
Namespace: "example-com-main",
Services: map[string]ServiceValues{
"nginx": {
Name: "nginx",
Type: "nginx",
AutogeneratedRoutesEnabled: true,
AutogeneratedRoutesTLSAcme: true,
},
},
},
autogenRoutes: &lagoon.RoutesV2{},
},
want: lagoon.RoutesV2{
Routes: []lagoon.RouteV2{
{
Domain: "nginx-example-com-main.example.com",
Service: "nginx",
Autogenerated: true,
TLSAcme: helpers.BoolPtr(true),
IngressName: "nginx",
Insecure: helpers.StrPtr("Allow"),
AlternativeNames: []string{},
Labels: map[string]string{
"app.kubernetes.io/instance": "nginx",
"app.kubernetes.io/name": "autogenerated-ingress",
"helm.sh/chart": "autogenerated-ingress-0.1.0",
"lagoon.sh/autogenerated": "true",
"lagoon.sh/service": "nginx",
"lagoon.sh/service-type": "nginx",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := generateAutogenRoutes(tt.args.envVars, tt.args.lagoonYAML, tt.args.buildValues, tt.args.autogenRoutes); (err != nil) != tt.wantErr {
t.Errorf("generateAutogenRoutes() error = %v, wantErr %v", err, tt.wantErr)
}
lValues, _ := json.Marshal(tt.args.autogenRoutes)
wValues, _ := json.Marshal(tt.want)
if !reflect.DeepEqual(string(lValues), string(wValues)) {
t.Errorf("generateAutogenRoutes() = %v, want %v", string(lValues), string(wValues))
}
})
}
}

func Test_generateRoutes(t *testing.T) {
type args struct {
lagoonEnvVars []lagoon.EnvironmentVariable
envVars []lagoon.EnvironmentVariable
buildValues BuildValues
lYAML lagoon.YAML
lagoonYAML lagoon.YAML
autogenRoutes *lagoon.RoutesV2
mainRoutes *lagoon.RoutesV2
activeStanbyRoutes *lagoon.RoutesV2
Expand All @@ -324,11 +425,103 @@ func Test_generateRoutes(t *testing.T) {
want2 []string
wantErr bool
}{
// TODO: Add test cases.
{
name: "test1",
args: args{
envVars: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_SYSTEM_ROUTER_PATTERN",
Value: "${service}-${project}-${environment}.example.com",
Scope: "internal_system",
},
},
lagoonYAML: lagoon.YAML{},
buildValues: BuildValues{
Project: "example-com",
BuildType: "branch",
Environment: "main",
EnvironmentType: "development",
Namespace: "example-com-main",
Services: map[string]ServiceValues{
"nginx": {
Name: "nginx",
Type: "nginx",
AutogeneratedRoutesEnabled: true,
AutogeneratedRoutesTLSAcme: true,
},
},
},
autogenRoutes: &lagoon.RoutesV2{},
mainRoutes: &lagoon.RoutesV2{},
activeStanbyRoutes: &lagoon.RoutesV2{},
},
want: "https://nginx-example-com-main.example.com",
want1: []string{"https://nginx-example-com-main.example.com"},
want2: []string{"https://nginx-example-com-main.example.com"},
},
{
name: "test2",
args: args{
envVars: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_SYSTEM_ROUTER_PATTERN",
Value: "${service}-${project}-${environment}.example.com",
Scope: "internal_system",
},
},
lagoonYAML: lagoon.YAML{
Environments: lagoon.Environments{
"main": lagoon.Environment{
Routes: []map[string][]lagoon.Route{
{
"nginx": {
{
Ingresses: map[string]lagoon.Ingress{
"a.example.com": {
TLSAcme: helpers.BoolPtr(true),
},
},
},
{
Name: "b.example.com",
},
{
Name: "c.example.com",
},
},
},
},
},
},
},
buildValues: BuildValues{
Project: "example-com",
BuildType: "branch",
Environment: "main",
Branch: "main",
EnvironmentType: "development",
Namespace: "example-com-main",
Services: map[string]ServiceValues{
"nginx": {
Name: "nginx",
Type: "nginx",
AutogeneratedRoutesEnabled: true,
AutogeneratedRoutesTLSAcme: true,
},
},
},
autogenRoutes: &lagoon.RoutesV2{},
mainRoutes: &lagoon.RoutesV2{},
activeStanbyRoutes: &lagoon.RoutesV2{},
},
want: "https://a.example.com",
want1: []string{"https://nginx-example-com-main.example.com", "https://a.example.com", "https://b.example.com", "https://c.example.com"},
want2: []string{"https://nginx-example-com-main.example.com"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, got2, err := generateRoutes(tt.args.lagoonEnvVars, tt.args.buildValues, tt.args.lYAML, tt.args.autogenRoutes, tt.args.mainRoutes, tt.args.activeStanbyRoutes, tt.args.debug)
got, got1, got2, err := generateRoutes(tt.args.envVars, tt.args.buildValues, tt.args.lagoonYAML, tt.args.autogenRoutes, tt.args.mainRoutes, tt.args.activeStanbyRoutes, tt.args.debug)
if (err != nil) != tt.wantErr {
t.Errorf("generateRoutes() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
35 changes: 17 additions & 18 deletions internal/lagoon/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,24 @@ func MergeRoutesV2(genRoutes RoutesV2, apiRoutes RoutesV2, variables []Environme
}
// add any that exist in the api only to the final routes list
for _, aRoute := range apiRoutes.Routes {
add := RouteV2{}
add := aRoute
add.Fastly = aRoute.Fastly
if aRoute.TLSAcme != nil {
add.TLSAcme = aRoute.TLSAcme
} else {
add.TLSAcme = helpers.BoolPtr(true)
}
if aRoute.Insecure != nil {
add.Insecure = aRoute.Insecure
} else {
add.Insecure = helpers.StrPtr("Redirect")
}
if aRoute.Annotations != nil {
add.Annotations = aRoute.Annotations
} else {
add.Annotations = map[string]string{}
}
for _, route := range finalRoutes.Routes {
add = aRoute
add.Fastly = aRoute.Fastly
if aRoute.TLSAcme != nil {
add.TLSAcme = aRoute.TLSAcme
} else {
add.TLSAcme = helpers.BoolPtr(true)
}
if aRoute.Insecure != nil {
add.Insecure = aRoute.Insecure
} else {
add.Insecure = helpers.StrPtr("Redirect")
}
if aRoute.Annotations != nil {
add.Annotations = aRoute.Annotations
} else {
add.Annotations = map[string]string{}
}
if aRoute.Domain == route.Domain {
existsInAPI = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
fastly.amazee.io/watch: "false"
ingress.kubernetes.io/ssl-redirect: "true"
kubernetes.io/tls-acme: "false"
lagoon.sh/branch: main
lagoon.sh/version: v2.7.x
monitor.stakater.com/enabled: "true"
monitor.stakater.com/overridePath: /bypass-cache
nginx.ingress.kubernetes.io/ssl-redirect: "true"
uptimerobot.monitor.stakater.com/alert-contacts: alertcontact
uptimerobot.monitor.stakater.com/interval: "60"
uptimerobot.monitor.stakater.com/status-pages: statuspageid
creationTimestamp: null
labels:
app.kubernetes.io/instance: test1.example.com
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: custom-ingress
dioscuri.amazee.io/migrate: "false"
helm.sh/chart: custom-ingress-0.1.0
lagoon.sh/autogenerated: "false"
lagoon.sh/buildType: branch
lagoon.sh/environment: main
lagoon.sh/environmentType: production
lagoon.sh/project: example-project
lagoon.sh/service: test1.example.com
lagoon.sh/service-type: custom-ingress
name: test1.example.com
spec:
rules:
- host: test1.example.com
http:
paths:
- backend:
service:
name: nginx
port:
name: http
path: /
pathType: Prefix
tls:
- hosts:
- test1.example.com
secretName: test1.example.com-tls
status:
loadBalancer: {}
20 changes: 20 additions & 0 deletions test-resources/template-ingress/test12/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '2'
services:
node:
networks:
- amazeeio-network
- default
build:
context: .
dockerfile: node.dockerfile
labels:
lagoon.type: node
volumes:
- .:/app:delegated
environment:
- LAGOON_LOCALDEV_HTTP_PORT=3000
- LAGOON_ROUTE=http://node.docker.amazee.io

networks:
amazeeio-network:
external: true
Loading

0 comments on commit cc11c64

Please sign in to comment.