Skip to content

Commit b9cd1a2

Browse files
committed
refactor: add apex domain for wildcard ingress
1 parent dff4d45 commit b9cd1a2

File tree

9 files changed

+192
-0
lines changed

9 files changed

+192
-0
lines changed

internal/generator/ingress_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ func Test_generateAndMerge(t *testing.T) {
458458
IngressClass: "nginx",
459459
AlternativeNames: []string{},
460460
Wildcard: helpers.BoolPtr(true),
461+
WildcardApex: helpers.BoolPtr(true),
461462
IngressName: "wildcard-a.example.com",
462463
RequestVerification: helpers.BoolPtr(false),
463464
},
@@ -804,6 +805,7 @@ func Test_generateActiveStandbyRoutes(t *testing.T) {
804805
IngressClass: "nginx",
805806
AlternativeNames: []string{},
806807
Wildcard: helpers.BoolPtr(true),
808+
WildcardApex: helpers.BoolPtr(true),
807809
IngressName: "wildcard-active.example.com",
808810
RequestVerification: helpers.BoolPtr(false),
809811
},

internal/lagoon/routes.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type RouteV2 struct {
3737
HSTSPreload *bool `json:"hstsPreload,omitempty"`
3838
Autogenerated bool `json:"-"`
3939
Wildcard *bool `json:"wildcard,omitempty"`
40+
WildcardApex *bool `json:"wildcardApex,omitempty"`
4041
RequestVerification *bool `json:"disableRequestVerification,omitempty"`
4142
PathRoutes []PathRoute `json:"pathRoutes,omitempty"`
4243
}
@@ -56,6 +57,7 @@ type Ingress struct {
5657
HSTSPreload *bool `json:"hstsPreload,omitempty"`
5758
AlternativeNames []string `json:"alternativenames,omitempty"`
5859
Wildcard *bool `json:"wildcard,omitempty"`
60+
WildcardApex *bool `json:"wildcardApex,omitempty"`
5961
RequestVerification *bool `json:"disableRequestVerification,omitempty"`
6062
PathRoutes []PathRoute `json:"pathRoutes,omitempty"`
6163
}
@@ -195,6 +197,13 @@ func GenerateRoutesV2(yamlRoutes *RoutesV2, routeMap map[string][]Route, variabl
195197
if ingress.AlternativeNames != nil && *newRoute.Wildcard {
196198
return fmt.Errorf("Route %s has wildcard: true and alternativenames defined, this is not supported", newRoute.Domain)
197199
}
200+
newRoute.WildcardApex = helpers.BoolPtr(true)
201+
if ingress.WildcardApex != nil {
202+
if !*ingress.WildcardApex {
203+
// allow false pass through if required
204+
newRoute.WildcardApex = ingress.WildcardApex
205+
}
206+
}
198207
newRoute.IngressName = fmt.Sprintf("wildcard-%s", newRoute.Domain)
199208
if err := validation.IsDNS1123Subdomain(strings.ToLower(newRoute.IngressName)); err != nil {
200209
newRoute.IngressName = fmt.Sprintf("%s-%s", newRoute.IngressName[:len(newRoute.IngressName)-10], helpers.GetMD5HashWithNewLine(newRoute.Domain)[:5])
@@ -370,6 +379,13 @@ func handleAPIRoute(defaultIngressClass string, apiRoute RouteV2) (RouteV2, erro
370379
if apiRoute.AlternativeNames != nil && *routeAdd.Wildcard {
371380
return routeAdd, fmt.Errorf("Route %s has wildcard=true and alternativenames defined, this is not supported", routeAdd.Domain)
372381
}
382+
routeAdd.WildcardApex = helpers.BoolPtr(true)
383+
if apiRoute.WildcardApex != nil {
384+
if !*apiRoute.WildcardApex {
385+
// allow false pass through if required
386+
routeAdd.WildcardApex = apiRoute.WildcardApex
387+
}
388+
}
373389
apiRoute.IngressName = fmt.Sprintf("wildcard-%s", apiRoute.Domain)
374390
if err := validation.IsDNS1123Subdomain(strings.ToLower(apiRoute.IngressName)); err != nil {
375391
apiRoute.IngressName = fmt.Sprintf("%s-%s", apiRoute.IngressName[:len(apiRoute.IngressName)-10], helpers.GetMD5HashWithNewLine(apiRoute.Domain)[:5])

internal/lagoon/routes_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,44 @@ func TestGenerateRouteStructure(t *testing.T) {
402402
Annotations: map[string]string{},
403403
AlternativeNames: []string{},
404404
Wildcard: helpers.BoolPtr(true),
405+
WildcardApex: helpers.BoolPtr(true),
406+
IngressName: "wildcard-www.example.com",
407+
RequestVerification: helpers.BoolPtr(false),
408+
},
409+
},
410+
},
411+
},
412+
{
413+
name: "test8 - wildcard with tls-acme false wildcard apex disabled",
414+
args: args{
415+
yamlRoutes: &RoutesV2{},
416+
yamlRouteMap: map[string][]Route{
417+
"nginx": {
418+
{
419+
Ingresses: map[string]Ingress{
420+
"www.example.com": {
421+
TLSAcme: helpers.BoolPtr(false),
422+
Wildcard: helpers.BoolPtr(true),
423+
WildcardApex: helpers.BoolPtr(false),
424+
},
425+
},
426+
},
427+
},
428+
},
429+
activeStandby: false,
430+
},
431+
want: &RoutesV2{
432+
Routes: []RouteV2{
433+
{
434+
Domain: "www.example.com",
435+
LagoonService: "nginx",
436+
MonitoringPath: "/",
437+
Insecure: helpers.StrPtr("Redirect"),
438+
TLSAcme: helpers.BoolPtr(false),
439+
Annotations: map[string]string{},
440+
AlternativeNames: []string{},
441+
Wildcard: helpers.BoolPtr(true),
442+
WildcardApex: helpers.BoolPtr(false),
405443
IngressName: "wildcard-www.example.com",
406444
RequestVerification: helpers.BoolPtr(false),
407445
},
@@ -633,6 +671,7 @@ func TestMergeRouteStructures(t *testing.T) {
633671
Annotations: map[string]string{},
634672
AlternativeNames: []string{},
635673
Wildcard: helpers.BoolPtr(true),
674+
WildcardApex: helpers.BoolPtr(true),
636675
IngressName: "example.com",
637676
RequestVerification: helpers.BoolPtr(false),
638677
},
@@ -645,6 +684,7 @@ func TestMergeRouteStructures(t *testing.T) {
645684
Annotations: map[string]string{},
646685
AlternativeNames: []string{},
647686
Wildcard: helpers.BoolPtr(true),
687+
WildcardApex: helpers.BoolPtr(true),
648688
IngressName: "a.example.com",
649689
RequestVerification: helpers.BoolPtr(false),
650690
},

internal/templating/templates_ingress.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func GenerateIngressTemplate(
5252
truncatedRouteDomain = fmt.Sprintf("%s-%s", strings.Split(subdomain, "-")[0], helpers.GetMD5HashWithNewLine(route.Domain)[:5])
5353
}
5454
// set the domain to include the wildcard prefix
55+
if route.WildcardApex != nil && *route.WildcardApex {
56+
route.AlternativeNames = append(route.AlternativeNames, route.Domain)
57+
}
5558
route.Domain = fmt.Sprintf("*.%s", route.Domain)
5659
}
5760

internal/templating/templates_ingress_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ func TestGenerateIngressTemplate(t *testing.T) {
456456
},
457457
IngressClass: "nginx",
458458
Wildcard: helpers.BoolPtr(true),
459+
WildcardApex: helpers.BoolPtr(true),
459460
IngressName: "wildcard-www.example.com",
460461
},
461462
values: generator.BuildValues{
@@ -502,6 +503,7 @@ func TestGenerateIngressTemplate(t *testing.T) {
502503
},
503504
IngressClass: "nginx",
504505
Wildcard: helpers.BoolPtr(true),
506+
WildcardApex: helpers.BoolPtr(true),
505507
IngressName: "wildcard-this-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.www.e-f1945",
506508
},
507509
values: generator.BuildValues{
@@ -700,6 +702,53 @@ func TestGenerateIngressTemplate(t *testing.T) {
700702
},
701703
want: "test-resources/ingress/result-custom-ingress9.yaml",
702704
},
705+
{
706+
name: "wildcard ingress no apex",
707+
args: args{
708+
route: lagoon.RouteV2{
709+
Domain: "www.example.com",
710+
LagoonService: "nginx",
711+
MonitoringPath: "/",
712+
Insecure: helpers.StrPtr("Redirect"),
713+
TLSAcme: helpers.BoolPtr(false),
714+
Migrate: helpers.BoolPtr(false),
715+
Annotations: map[string]string{
716+
"custom-annotation": "custom annotation value",
717+
},
718+
Fastly: lagoon.Fastly{
719+
Watch: false,
720+
},
721+
IngressClass: "nginx",
722+
Wildcard: helpers.BoolPtr(true),
723+
WildcardApex: helpers.BoolPtr(false),
724+
IngressName: "wildcard-www.example.com",
725+
},
726+
values: generator.BuildValues{
727+
Project: "example-project",
728+
Environment: "environment",
729+
EnvironmentType: "development",
730+
Namespace: "myexample-project-environment",
731+
BuildType: "branch",
732+
LagoonVersion: "v2.x.x",
733+
Kubernetes: "lagoon.local",
734+
Branch: "environment",
735+
Monitoring: generator.MonitoringConfig{
736+
AlertContact: "abcdefg",
737+
StatusPageID: "12345",
738+
Enabled: true,
739+
},
740+
Services: []generator.ServiceValues{
741+
{
742+
Name: "nginx",
743+
OverrideName: "nginx",
744+
Type: "nginx-php",
745+
},
746+
},
747+
},
748+
activeStandby: false,
749+
},
750+
want: "test-resources/ingress/result-wildcard-ingress3.yaml",
751+
},
703752
}
704753
for _, tt := range tests {
705754
t.Run(tt.name, func(t *testing.T) {

internal/templating/test-resources/ingress/result-wildcard-ingress1.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ spec:
4141
name: http
4242
path: /
4343
pathType: Prefix
44+
- host: www.example.com
45+
http:
46+
paths:
47+
- backend:
48+
service:
49+
name: nginx
50+
port:
51+
name: http
52+
path: /
53+
pathType: Prefix
4454
tls:
4555
- hosts:
4656
- '*.www.example.com'
57+
- www.example.com
4758
secretName: wildcard-www.example.com-tls
4859
status:
4960
loadBalancer: {}

internal/templating/test-resources/ingress/result-wildcard-ingress2.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ spec:
4141
name: http
4242
path: /
4343
pathType: Prefix
44+
- host: this-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.www.example.com
45+
http:
46+
paths:
47+
- backend:
48+
service:
49+
name: nginx
50+
port:
51+
name: http
52+
path: /
53+
pathType: Prefix
4454
tls:
4555
- hosts:
4656
- '*.this-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.www.example.com'
57+
- this-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.extra-long-name.a-really-long-name-that-should-truncate.www.example.com
4758
secretName: wildcard-this-truncate-f1945-tls
4859
status:
4960
loadBalancer: {}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
apiVersion: networking.k8s.io/v1
3+
kind: Ingress
4+
metadata:
5+
annotations:
6+
acme.cert-manager.io/http01-ingress-class: nginx
7+
custom-annotation: custom annotation value
8+
fastly.amazee.io/watch: "false"
9+
idling.amazee.io/disable-request-verification: "false"
10+
ingress.kubernetes.io/ssl-redirect: "true"
11+
kubernetes.io/tls-acme: "false"
12+
lagoon.sh/branch: environment
13+
lagoon.sh/version: v2.x.x
14+
nginx.ingress.kubernetes.io/server-snippet: |
15+
add_header X-Robots-Tag "noindex, nofollow";
16+
nginx.ingress.kubernetes.io/ssl-redirect: "true"
17+
creationTimestamp: null
18+
labels:
19+
app.kubernetes.io/instance: wildcard-www.example.com
20+
app.kubernetes.io/managed-by: build-deploy-tool
21+
app.kubernetes.io/name: custom-ingress
22+
lagoon.sh/autogenerated: "false"
23+
lagoon.sh/buildType: branch
24+
lagoon.sh/environment: environment
25+
lagoon.sh/environmentType: development
26+
lagoon.sh/project: example-project
27+
lagoon.sh/service: wildcard-www.example.com
28+
lagoon.sh/service-type: custom-ingress
29+
lagoon.sh/template: custom-ingress-0.1.0
30+
name: wildcard-www.example.com
31+
spec:
32+
ingressClassName: nginx
33+
rules:
34+
- host: '*.www.example.com'
35+
http:
36+
paths:
37+
- backend:
38+
service:
39+
name: nginx
40+
port:
41+
name: http
42+
path: /
43+
pathType: Prefix
44+
tls:
45+
- hosts:
46+
- '*.www.example.com'
47+
secretName: wildcard-www.example.com-tls
48+
status:
49+
loadBalancer: {}

internal/testdata/node/ingress-templates/ingress-22/example.com.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ spec:
3939
name: http
4040
path: /
4141
pathType: Prefix
42+
- host: example.com
43+
http:
44+
paths:
45+
- backend:
46+
service:
47+
name: node
48+
port:
49+
name: http
50+
path: /
51+
pathType: Prefix
4252
tls:
4353
- hosts:
4454
- '*.example.com'
55+
- example.com
4556
secretName: wildcard-example.com-tls
4657
status:
4758
loadBalancer: {}

0 commit comments

Comments
 (0)