Skip to content

Commit

Permalink
Merge pull request #69 from uselagoon/compose-service-autogenerated
Browse files Browse the repository at this point in the history
fix: use compose service name instead of lagoon service name
  • Loading branch information
shreddedbacon authored Jul 20, 2022
2 parents 9171936 + 6bab1a0 commit 45220a0
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 36 deletions.
22 changes: 22 additions & 0 deletions cmd/template_autogen_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,28 @@ func TestAutogeneratedIngressGeneration(t *testing.T) {
emptyDir: false,
want: "../test-resources/template-autogenerated/test21-results",
},
{
name: "test22 autogenerated routes where should truncate long dns",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "content-abcdefghijk-net-com-co",
environmentName: "pr-123",
environmentType: "development",
lagoonVersion: "v2.7.x",
buildType: "pullrequest",
prNumber: "123",
prHeadBranch: "main",
prBaseBranch: "main2",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${environment}.${project}.abc1.abc.net.com.co","scope":"internal_system"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "../test-resources/template-autogenerated/test22/lagoon.yml",
templatePath: "../test-resources/template-autogenerated/output",
},
emptyDir: false,
want: "../test-resources/template-autogenerated/test22-results",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion internal/generator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ func generateAutogenRoutes(
"lagoon.sh/service-type": service.Type,
},
Autogenerated: true,
Service: serviceOverrideName,
LagoonService: serviceOverrideName,
ComposeService: serviceName,
IngressName: serviceOverrideName,
Insecure: &insecure,
AlternativeNames: alternativeNames,
Expand Down
27 changes: 14 additions & 13 deletions internal/generator/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_getRoutesFromAPIEnvVar(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "test1.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
Expand Down Expand Up @@ -107,13 +107,13 @@ func Test_generateAndMerge(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "test1.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
{
Domain: "a.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
Expand All @@ -124,31 +124,31 @@ func Test_generateAndMerge(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "a.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
Annotations: map[string]string{},
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/bypass-cache",
},
{
Domain: "b.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(true),
Annotations: map[string]string{},
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/",
},
{
Domain: "c.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(true),
Annotations: map[string]string{},
Insecure: helpers.StrPtr("Redirect"),
MonitoringPath: "/",
},
{
Domain: "test1.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
Insecure: helpers.StrPtr("Redirect"),
Expand All @@ -168,13 +168,13 @@ func Test_generateAndMerge(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "test1.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
{
Domain: "a.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
},
Expand All @@ -185,15 +185,15 @@ func Test_generateAndMerge(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "test1.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
MonitoringPath: "/bypass-cache",
Insecure: helpers.StrPtr("Redirect"),
Annotations: map[string]string{},
},
{
Domain: "a.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(false),
Annotations: map[string]string{},
Insecure: helpers.StrPtr("Redirect"),
Expand Down Expand Up @@ -257,7 +257,7 @@ func Test_generateActiveStandbyRoutes(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "active.example.com",
Service: "nginx",
LagoonService: "nginx",
TLSAcme: helpers.BoolPtr(true),
Annotations: map[string]string{},
Migrate: helpers.BoolPtr(true),
Expand Down Expand Up @@ -374,7 +374,8 @@ func Test_generateAutogenRoutes(t *testing.T) {
Routes: []lagoon.RouteV2{
{
Domain: "nginx-example-com-main.example.com",
Service: "nginx",
LagoonService: "nginx",
ComposeService: "nginx",
Autogenerated: true,
TLSAcme: helpers.BoolPtr(true),
IngressName: "nginx",
Expand Down
7 changes: 4 additions & 3 deletions internal/lagoon/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type RoutesV2 struct {
// RouteV2 is the new route definition
type RouteV2 struct {
Domain string `json:"domain"`
Service string `json:"service"`
LagoonService string `json:"service"`
ComposeService string `json:"composeService"` // the
TLSAcme *bool `json:"tls-acme"`
Migrate *bool `json:"migrate,omitempty"`
Insecure *string `json:"insecure,omitempty"`
Expand Down Expand Up @@ -103,7 +104,7 @@ func GenerateRoutesV2(genRoutes *RoutesV2, routeMap map[string][]Route, variable
// update them from the defaults in this case
for iName, ingress := range lagoonRoute.Ingresses {
newRoute.Domain = iName
newRoute.Service = rName
newRoute.LagoonService = rName
newRoute.Fastly = ingress.Fastly
if ingress.Annotations != nil {
newRoute.Annotations = ingress.Annotations
Expand All @@ -119,7 +120,7 @@ func GenerateRoutesV2(genRoutes *RoutesV2, routeMap map[string][]Route, variable
// this route is just a domain
// keep the defaults, just set the name and service
newRoute.Domain = lagoonRoute.Name
newRoute.Service = rName
newRoute.LagoonService = rName
}
// generate the fastly configuration for this route
err := GenerateFastlyConfiguration(&newRoute.Fastly, "", newRoute.Fastly.ServiceID, newRoute.Domain, secretPrefix, variables)
Expand Down
22 changes: 11 additions & 11 deletions internal/lagoon/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestGenerateRouteStructure(t *testing.T) {
Routes: []RouteV2{
{
Domain: "example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -54,7 +54,7 @@ func TestGenerateRouteStructure(t *testing.T) {
},
{
Domain: "www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestGenerateRouteStructure(t *testing.T) {
Routes: []RouteV2{
{
Domain: "example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -106,7 +106,7 @@ func TestGenerateRouteStructure(t *testing.T) {
},
{
Domain: "www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestMergeRouteStructures(t *testing.T) {
Routes: []RouteV2{
{
Domain: "example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -165,7 +165,7 @@ func TestMergeRouteStructures(t *testing.T) {
},
{
Domain: "www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -177,7 +177,7 @@ func TestMergeRouteStructures(t *testing.T) {
Routes: []RouteV2{
{
Domain: "www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -187,7 +187,7 @@ func TestMergeRouteStructures(t *testing.T) {
},
{
Domain: "another.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -201,7 +201,7 @@ func TestMergeRouteStructures(t *testing.T) {
Routes: []RouteV2{
{
Domain: "example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -214,7 +214,7 @@ func TestMergeRouteStructures(t *testing.T) {
},
{
Domain: "www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand All @@ -224,7 +224,7 @@ func TestMergeRouteStructures(t *testing.T) {
},
{
Domain: "another.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand Down
11 changes: 6 additions & 5 deletions internal/templating/ingress/templates_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ func GenerateIngressTemplate(

// autogenerated domains that are too long break when creating the acme challenge k8s resource
// this injects a shorter domain into the tls spec that is used in the k8s challenge
if _, ok := lValues.Services[route.Service]; ok {
if lValues.Services[route.Service].ShortAutogeneratedRouteDomain != "" && len(route.Domain) > 63 {
ingress.Spec.TLS[0].Hosts = append(ingress.Spec.TLS[0].Hosts, lValues.Services[route.Service].ShortAutogeneratedRouteDomain)
// use the compose service name to check this, as this is how Services are populated from the compose generation
if _, ok := lValues.Services[route.ComposeService]; ok {
if lValues.Services[route.ComposeService].ShortAutogeneratedRouteDomain != "" && len(route.Domain) > 63 {
ingress.Spec.TLS[0].Hosts = append(ingress.Spec.TLS[0].Hosts, lValues.Services[route.ComposeService].ShortAutogeneratedRouteDomain)
}
}
// add the main domain to the tls spec now
Expand Down Expand Up @@ -180,7 +181,7 @@ func GenerateIngressTemplate(
PathType: &pt,
Backend: networkv1.IngressBackend{
Service: &networkv1.IngressServiceBackend{
Name: route.Service,
Name: route.LagoonService,
Port: servicePort,
},
},
Expand All @@ -203,7 +204,7 @@ func GenerateIngressTemplate(
PathType: &pt,
Backend: networkv1.IngressBackend{
Service: &networkv1.IngressServiceBackend{
Name: route.Service,
Name: route.LagoonService,
Port: servicePort,
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/templating/ingress/templates_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGenerateKubeTemplate(t *testing.T) {
args: args{
route: lagoon.RouteV2{
Domain: "extra-long-name.a-really-long-name-that-should-truncate.www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestGenerateKubeTemplate(t *testing.T) {
args: args{
route: lagoon.RouteV2{
Domain: "extra-long-name.a-really-long-name-that-should-truncate.www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestGenerateKubeTemplate(t *testing.T) {
args: args{
route: lagoon.RouteV2{
Domain: "extra-long-name.a-really-long-name-that-should-truncate.www.example.com",
Service: "nginx",
LagoonService: "nginx",
MonitoringPath: "/",
Insecure: helpers.StrPtr("Redirect"),
TLSAcme: helpers.BoolPtr(true),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
fastly.amazee.io/watch: "false"
ingress.kubernetes.io/ssl-redirect: "true"
kubernetes.io/tls-acme: "true"
lagoon.sh/prBaseBranch: main2
lagoon.sh/prHeadBranch: main
lagoon.sh/prNumber: "123"
lagoon.sh/version: v2.7.x
nginx.ingress.kubernetes.io/server-snippet: |
add_header X-Robots-Tag "noindex, nofollow";
nginx.ingress.kubernetes.io/ssl-redirect: "true"
creationTimestamp: null
labels:
app.kubernetes.io/instance: nginx-php
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: autogenerated-ingress
helm.sh/chart: autogenerated-ingress-0.1.0
lagoon.sh/autogenerated: "true"
lagoon.sh/buildType: pullrequest
lagoon.sh/environment: pr-123
lagoon.sh/environmentType: development
lagoon.sh/project: content-abcdefghijk-net-com-co
lagoon.sh/service: nginx-php
lagoon.sh/service-type: nginx-php
name: nginx-php
spec:
rules:
- host: nginx-php.pr-123.content-abcdefghijk-net-com-co.abc1.abc.net.com.co
http:
paths:
- backend:
service:
name: nginx-php
port:
name: http
path: /
pathType: Prefix
tls:
- hosts:
- nginx-php.4acfiibx.xjtthosc.abc1.abc.net.com.co
- nginx-php.pr-123.content-abcdefghijk-net-com-co.abc1.abc.net.com.co
secretName: nginx-php-tls
status:
loadBalancer: {}
Loading

0 comments on commit 45220a0

Please sign in to comment.