From 99c9af7d87c921a372aa7a29b4b0763ce8935d6d Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Wed, 20 Jul 2022 11:51:15 +1000 Subject: [PATCH] fix: use composeservice name instead of lagoonservice name when checking autogenerated routes --- cmd/template_autogen_ingress_test.go | 22 +++++++++ internal/generator/ingress.go | 3 +- internal/generator/ingress_test.go | 27 ++++++----- internal/lagoon/routes.go | 7 +-- internal/lagoon/routes_test.go | 22 ++++----- .../templating/ingress/templates_ingress.go | 11 +++-- .../ingress/templates_ingress_test.go | 6 +-- .../test22-results/nginx-php.yaml | 48 +++++++++++++++++++ .../test22/docker-compose.yml | 21 ++++++++ .../template-autogenerated/test22/lagoon.yml | 16 +++++++ 10 files changed, 147 insertions(+), 36 deletions(-) create mode 100644 test-resources/template-autogenerated/test22-results/nginx-php.yaml create mode 100644 test-resources/template-autogenerated/test22/docker-compose.yml create mode 100644 test-resources/template-autogenerated/test22/lagoon.yml diff --git a/cmd/template_autogen_ingress_test.go b/cmd/template_autogen_ingress_test.go index 95451f89..68303af7 100644 --- a/cmd/template_autogen_ingress_test.go +++ b/cmd/template_autogen_ingress_test.go @@ -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) { diff --git a/internal/generator/ingress.go b/internal/generator/ingress.go index 4415fe40..2370df08 100644 --- a/internal/generator/ingress.go +++ b/internal/generator/ingress.go @@ -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, diff --git a/internal/generator/ingress_test.go b/internal/generator/ingress_test.go index dbe262a5..6d535524 100644 --- a/internal/generator/ingress_test.go +++ b/internal/generator/ingress_test.go @@ -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", }, @@ -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", }, @@ -124,7 +124,7 @@ 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"), @@ -132,7 +132,7 @@ func Test_generateAndMerge(t *testing.T) { }, { Domain: "b.example.com", - Service: "nginx", + LagoonService: "nginx", TLSAcme: helpers.BoolPtr(true), Annotations: map[string]string{}, Insecure: helpers.StrPtr("Redirect"), @@ -140,7 +140,7 @@ func Test_generateAndMerge(t *testing.T) { }, { Domain: "c.example.com", - Service: "nginx", + LagoonService: "nginx", TLSAcme: helpers.BoolPtr(true), Annotations: map[string]string{}, Insecure: helpers.StrPtr("Redirect"), @@ -148,7 +148,7 @@ func Test_generateAndMerge(t *testing.T) { }, { Domain: "test1.example.com", - Service: "nginx", + LagoonService: "nginx", TLSAcme: helpers.BoolPtr(false), MonitoringPath: "/bypass-cache", Insecure: helpers.StrPtr("Redirect"), @@ -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", }, @@ -185,7 +185,7 @@ 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"), @@ -193,7 +193,7 @@ func Test_generateAndMerge(t *testing.T) { }, { Domain: "a.example.com", - Service: "nginx", + LagoonService: "nginx", TLSAcme: helpers.BoolPtr(false), Annotations: map[string]string{}, Insecure: helpers.StrPtr("Redirect"), @@ -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), @@ -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", diff --git a/internal/lagoon/routes.go b/internal/lagoon/routes.go index a7f4d7c7..8f6bfb10 100644 --- a/internal/lagoon/routes.go +++ b/internal/lagoon/routes.go @@ -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"` @@ -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 @@ -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) diff --git a/internal/lagoon/routes_test.go b/internal/lagoon/routes_test.go index 91a21fdc..5b6e555b 100644 --- a/internal/lagoon/routes_test.go +++ b/internal/lagoon/routes_test.go @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), diff --git a/internal/templating/ingress/templates_ingress.go b/internal/templating/ingress/templates_ingress.go index 3c5ff119..1903e323 100644 --- a/internal/templating/ingress/templates_ingress.go +++ b/internal/templating/ingress/templates_ingress.go @@ -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 @@ -180,7 +181,7 @@ func GenerateIngressTemplate( PathType: &pt, Backend: networkv1.IngressBackend{ Service: &networkv1.IngressServiceBackend{ - Name: route.Service, + Name: route.LagoonService, Port: servicePort, }, }, @@ -203,7 +204,7 @@ func GenerateIngressTemplate( PathType: &pt, Backend: networkv1.IngressBackend{ Service: &networkv1.IngressServiceBackend{ - Name: route.Service, + Name: route.LagoonService, Port: servicePort, }, }, diff --git a/internal/templating/ingress/templates_ingress_test.go b/internal/templating/ingress/templates_ingress_test.go index a9663bd5..09d0b66e 100644 --- a/internal/templating/ingress/templates_ingress_test.go +++ b/internal/templating/ingress/templates_ingress_test.go @@ -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), @@ -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), @@ -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), diff --git a/test-resources/template-autogenerated/test22-results/nginx-php.yaml b/test-resources/template-autogenerated/test22-results/nginx-php.yaml new file mode 100644 index 00000000..1dcc694e --- /dev/null +++ b/test-resources/template-autogenerated/test22-results/nginx-php.yaml @@ -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: {} diff --git a/test-resources/template-autogenerated/test22/docker-compose.yml b/test-resources/template-autogenerated/test22/docker-compose.yml new file mode 100644 index 00000000..8489d7fe --- /dev/null +++ b/test-resources/template-autogenerated/test22/docker-compose.yml @@ -0,0 +1,21 @@ +version: '2' +services: + nginx: + networks: + - amazeeio-network + - default + build: + context: . + dockerfile: nginx.dockerfile + labels: + lagoon.type: nginx-php + lagoon.name: nginx-php + volumes: + - .:/app:delegated + environment: + - LAGOON_LOCALDEV_HTTP_PORT=3000 + - LAGOON_ROUTE=http://node.docker.amazee.io + +networks: + amazeeio-network: + external: true \ No newline at end of file diff --git a/test-resources/template-autogenerated/test22/lagoon.yml b/test-resources/template-autogenerated/test22/lagoon.yml new file mode 100644 index 00000000..c2c577f6 --- /dev/null +++ b/test-resources/template-autogenerated/test22/lagoon.yml @@ -0,0 +1,16 @@ +docker-compose-yaml: ../test-resources/template-autogenerated/test22/docker-compose.yml + +routes: + autogenerate: + enabled: false + insecure: Redirect + allowPullrequests: true + +environment_variables: + git_sha: "true" + +environments: + main: + routes: + - node: + - example.com