From 3f9bbad70d6d55e035b5902f8e7dd04e68078c99 Mon Sep 17 00:00:00 2001 From: Ben Jackson Date: Tue, 20 Sep 2022 12:28:19 +1000 Subject: [PATCH] fix: monitoring only on primary route (#118) --- cmd/template_ingress_test.go | 18 +++++++ .../templating/ingress/templates_ingress.go | 6 ++- .../ingress/templates_ingress_test.go | 8 ++- .../test10-results/main2.example.com.yaml | 5 +- .../test18-results/example.com.yaml | 49 +++++++++++++++++++ .../test18-results/www.example.com.yaml | 46 +++++++++++++++++ .../test18/docker-compose.yml | 20 ++++++++ .../template-ingress/test18/lagoon.yml | 11 +++++ .../test7-results/www.example.com.yaml | 5 +- ...domain-will-be-not-be-main-domain.com.yaml | 5 +- .../test9-results/main.example.com.yaml | 5 +- 11 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 test-resources/template-ingress/test18-results/example.com.yaml create mode 100644 test-resources/template-ingress/test18-results/www.example.com.yaml create mode 100644 test-resources/template-ingress/test18/docker-compose.yml create mode 100644 test-resources/template-ingress/test18/lagoon.yml diff --git a/cmd/template_ingress_test.go b/cmd/template_ingress_test.go index 6a058c24..54ba3f62 100644 --- a/cmd/template_ingress_test.go +++ b/cmd/template_ingress_test.go @@ -374,6 +374,24 @@ func TestTemplateRoutes(t *testing.T) { }, want: "../test-resources/template-ingress/test17-results", }, + { + name: "test18 check first route has monitoring only", + args: args{ + alertContact: "alertcontact", + statusPageID: "statuspageid", + projectName: "example-project", + environmentName: "main", + environmentType: "production", + buildType: "branch", + lagoonVersion: "v2.7.x", + branch: "main", + projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`, + envVars: `[]`, + lagoonYAML: "../test-resources/template-ingress/test18/lagoon.yml", + templatePath: "../test-resources/template-ingress/output", + }, + want: "../test-resources/template-ingress/test18-results", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/templating/ingress/templates_ingress.go b/internal/templating/ingress/templates_ingress.go index bbaab4cf..f6f53ceb 100644 --- a/internal/templating/ingress/templates_ingress.go +++ b/internal/templating/ingress/templates_ingress.go @@ -2,6 +2,7 @@ package routes import ( "fmt" + "net/url" "strconv" "strings" @@ -85,7 +86,10 @@ func GenerateIngressTemplate( if lValues.EnvironmentType == "production" { // monitoring is only available in production environments additionalAnnotations["monitor.stakater.com/enabled"] = "false" - if lValues.Monitoring.Enabled && !route.Autogenerated { + primaryIngress, _ := url.Parse(lValues.Route) + // check if monitoring enabled, route isn't autogenerated, and the primary ingress from the .lagoon.yml is this processed routedomain + // and enable monitoring on the primary ingress only. + if lValues.Monitoring.Enabled && !route.Autogenerated && primaryIngress.Host == routeDomain { // only add the monitring annotations if monitoring is enabled additionalAnnotations["monitor.stakater.com/enabled"] = "true" additionalAnnotations["uptimerobot.monitor.stakater.com/alert-contacts"] = "unconfigured" diff --git a/internal/templating/ingress/templates_ingress_test.go b/internal/templating/ingress/templates_ingress_test.go index 2f6bd3c5..dc75733a 100644 --- a/internal/templating/ingress/templates_ingress_test.go +++ b/internal/templating/ingress/templates_ingress_test.go @@ -55,6 +55,7 @@ func TestGenerateKubeTemplate(t *testing.T) { StatusPageID: "12345", Enabled: true, }, + Route: "https://extra-long-name.a-really-long-name-that-should-truncate.www.example.com/", }, activeStandby: true, }, @@ -91,6 +92,7 @@ func TestGenerateKubeTemplate(t *testing.T) { StatusPageID: "12345", Enabled: true, }, + Route: "https://extra-long-name.a-really-long-name-that-should-truncate.www.example.com/", }, activeStandby: false, }, @@ -127,6 +129,7 @@ func TestGenerateKubeTemplate(t *testing.T) { StatusPageID: "12345", Enabled: true, }, + Route: "https://extra-long-name.a-really-long-name-that-should-truncate.www.example.com/", }, activeStandby: false, }, @@ -164,6 +167,7 @@ func TestGenerateKubeTemplate(t *testing.T) { StatusPageID: "12345", Enabled: true, }, + Route: "https://extra-long-name.a-really-long-name-that-should-truncate.www.example.com/", }, activeStandby: false, }, @@ -203,6 +207,7 @@ func TestGenerateKubeTemplate(t *testing.T) { StatusPageID: "12345", Enabled: true, }, + Route: "https://extra-long-name.a-really-long-name-that-should-truncate.www.example.com/", }, activeStandby: false, }, @@ -245,6 +250,7 @@ func TestGenerateKubeTemplate(t *testing.T) { StatusPageID: "12345", Enabled: true, }, + Route: "https://extra-long-name.a-really-long-name-that-should-truncate.www.example.com/", }, activeStandby: false, }, @@ -403,7 +409,7 @@ func TestGenerateKubeTemplate(t *testing.T) { wantErr: true, }, { - name: "test10 - custom ingress with exceptionally log subdomain", + name: "test10 - custom ingress with exceptionally long subdomain", args: args{ route: lagoon.RouteV2{ Domain: "hmm-this-is-a-really-long-branch-name-designed-to-test-a-specific-feature.www.example.com", diff --git a/test-resources/template-ingress/test10-results/main2.example.com.yaml b/test-resources/template-ingress/test10-results/main2.example.com.yaml index 74ca4dd6..eb166e30 100644 --- a/test-resources/template-ingress/test10-results/main2.example.com.yaml +++ b/test-resources/template-ingress/test10-results/main2.example.com.yaml @@ -8,12 +8,9 @@ metadata: kubernetes.io/tls-acme: "true" lagoon.sh/branch: main2 lagoon.sh/version: v2.7.x - monitor.stakater.com/enabled: "true" + monitor.stakater.com/enabled: "false" monitor.stakater.com/overridePath: / 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: main2.example.com diff --git a/test-resources/template-ingress/test18-results/example.com.yaml b/test-resources/template-ingress/test18-results/example.com.yaml new file mode 100644 index 00000000..872863e6 --- /dev/null +++ b/test-resources/template-ingress/test18-results/example.com.yaml @@ -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: "true" + lagoon.sh/branch: main + lagoon.sh/version: v2.7.x + monitor.stakater.com/enabled: "true" + monitor.stakater.com/overridePath: / + 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: 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: example.com + lagoon.sh/service-type: custom-ingress + name: example.com +spec: + rules: + - host: example.com + http: + paths: + - backend: + service: + name: node + port: + name: http + path: / + pathType: Prefix + tls: + - hosts: + - example.com + secretName: example.com-tls +status: + loadBalancer: {} diff --git a/test-resources/template-ingress/test18-results/www.example.com.yaml b/test-resources/template-ingress/test18-results/www.example.com.yaml new file mode 100644 index 00000000..9f94b2bd --- /dev/null +++ b/test-resources/template-ingress/test18-results/www.example.com.yaml @@ -0,0 +1,46 @@ +--- +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/branch: main + lagoon.sh/version: v2.7.x + monitor.stakater.com/enabled: "false" + monitor.stakater.com/overridePath: / + nginx.ingress.kubernetes.io/ssl-redirect: "true" + creationTimestamp: null + labels: + app.kubernetes.io/instance: www.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: www.example.com + lagoon.sh/service-type: custom-ingress + name: www.example.com +spec: + rules: + - host: www.example.com + http: + paths: + - backend: + service: + name: node + port: + name: http + path: / + pathType: Prefix + tls: + - hosts: + - www.example.com + secretName: www.example.com-tls +status: + loadBalancer: {} diff --git a/test-resources/template-ingress/test18/docker-compose.yml b/test-resources/template-ingress/test18/docker-compose.yml new file mode 100644 index 00000000..85386270 --- /dev/null +++ b/test-resources/template-ingress/test18/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/test-resources/template-ingress/test18/lagoon.yml b/test-resources/template-ingress/test18/lagoon.yml new file mode 100644 index 00000000..19608662 --- /dev/null +++ b/test-resources/template-ingress/test18/lagoon.yml @@ -0,0 +1,11 @@ +docker-compose-yaml: ../test-resources/template-ingress/test18/docker-compose.yml + +environment_variables: + git_sha: "true" + +environments: + main: + routes: + - node: + - example.com + - www.example.com diff --git a/test-resources/template-ingress/test7-results/www.example.com.yaml b/test-resources/template-ingress/test7-results/www.example.com.yaml index 108f72d0..9f94b2bd 100644 --- a/test-resources/template-ingress/test7-results/www.example.com.yaml +++ b/test-resources/template-ingress/test7-results/www.example.com.yaml @@ -8,12 +8,9 @@ metadata: kubernetes.io/tls-acme: "true" lagoon.sh/branch: main lagoon.sh/version: v2.7.x - monitor.stakater.com/enabled: "true" + monitor.stakater.com/enabled: "false" monitor.stakater.com/overridePath: / 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: www.example.com diff --git a/test-resources/template-ingress/test8-results/customdomain-will-be-not-be-main-domain.com.yaml b/test-resources/template-ingress/test8-results/customdomain-will-be-not-be-main-domain.com.yaml index 0eee9e49..78bacd1f 100644 --- a/test-resources/template-ingress/test8-results/customdomain-will-be-not-be-main-domain.com.yaml +++ b/test-resources/template-ingress/test8-results/customdomain-will-be-not-be-main-domain.com.yaml @@ -8,12 +8,9 @@ metadata: kubernetes.io/tls-acme: "true" lagoon.sh/branch: branch/routes lagoon.sh/version: v2.7.x - monitor.stakater.com/enabled: "true" + monitor.stakater.com/enabled: "false" monitor.stakater.com/overridePath: / 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: customdomain-will-be-not-be-main-domain.com diff --git a/test-resources/template-ingress/test9-results/main.example.com.yaml b/test-resources/template-ingress/test9-results/main.example.com.yaml index c44c69c4..878f5a34 100644 --- a/test-resources/template-ingress/test9-results/main.example.com.yaml +++ b/test-resources/template-ingress/test9-results/main.example.com.yaml @@ -8,12 +8,9 @@ metadata: kubernetes.io/tls-acme: "true" lagoon.sh/branch: main lagoon.sh/version: v2.7.x - monitor.stakater.com/enabled: "true" + monitor.stakater.com/enabled: "false" monitor.stakater.com/overridePath: / 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: main.example.com