diff --git a/cmd/template_autogen_ingress.go b/cmd/template_autogen_ingress.go index 8e910fc2..c92a935c 100644 --- a/cmd/template_autogen_ingress.go +++ b/cmd/template_autogen_ingress.go @@ -56,7 +56,10 @@ func AutogeneratedIngressGeneration(debug bool) error { if debug { fmt.Println(fmt.Sprintf("Templating autogenerated ingress manifest for %s to %s", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.IngressName))) } - templateYAML := routeTemplater.GenerateIngressTemplate(route, lagoonValues, monitoringContact, monitoringStatusPageID, false) + templateYAML, err := routeTemplater.GenerateIngressTemplate(route, lagoonValues, monitoringContact, monitoringStatusPageID, false) + if err != nil { + return fmt.Errorf("couldn't generate template: %v", err) + } routeTemplater.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, route.IngressName), templateYAML) } diff --git a/cmd/template_autogen_ingress_test.go b/cmd/template_autogen_ingress_test.go index 19d45109..4f90109f 100644 --- a/cmd/template_autogen_ingress_test.go +++ b/cmd/template_autogen_ingress_test.go @@ -374,6 +374,26 @@ func TestAutogeneratedIngressGeneration(t *testing.T) { emptyDir: false, want: "test-resources/template-autogenerated/test16-results", }, + { + name: "test17 autogenerated route development service type override", + args: args{ + alertContact: "alertcontact", + statusPageID: "statuspageid", + projectName: "ci-drush-la-control-k8s", + environmentName: "drush-first", + environmentType: "development", + buildType: "branch", + lagoonVersion: "v2.7.x", + branch: "drush-first", + projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}.${project}.${environment}.172.18.0.3.nip.io","scope":"internal_system"}]`, + envVars: `[]`, + secretPrefix: "fastly-api-", + lagoonYAML: "test-resources/template-autogenerated/drush-lagoon.yml", + templatePath: "test-resources/template-autogenerated/output", + }, + emptyDir: false, + want: "test-resources/template-autogenerated/test17-results", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/cmd/template_ingress.go b/cmd/template_ingress.go index dea9485c..f06816c8 100644 --- a/cmd/template_ingress.go +++ b/cmd/template_ingress.go @@ -53,7 +53,10 @@ func IngressTemplateGeneration(debug bool) error { if debug { fmt.Println(fmt.Sprintf("Templating ingress manifest for %s to %s", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain))) } - templateYAML := routeTemplater.GenerateIngressTemplate(route, lagoonValues, monitoringContact, monitoringStatusPageID, monitoringEnabled) + templateYAML, err := routeTemplater.GenerateIngressTemplate(route, lagoonValues, monitoringContact, monitoringStatusPageID, monitoringEnabled) + if err != nil { + return fmt.Errorf("couldn't generate template: %v", err) + } routeTemplater.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain), templateYAML) } if activeEnv || standbyEnv { @@ -67,7 +70,10 @@ func IngressTemplateGeneration(debug bool) error { if debug { fmt.Println(fmt.Sprintf("Templating active/standby ingress manifest for %s to %s", route.Domain, fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain))) } - templateYAML := routeTemplater.GenerateIngressTemplate(route, lagoonValues, monitoringContact, monitoringStatusPageID, monitoringEnabled) + templateYAML, err := routeTemplater.GenerateIngressTemplate(route, lagoonValues, monitoringContact, monitoringStatusPageID, monitoringEnabled) + if err != nil { + return fmt.Errorf("couldn't generate template: %v", err) + } routeTemplater.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, route.Domain), templateYAML) } } diff --git a/cmd/test-resources/template-autogenerated/drush-docker-compose.yml b/cmd/test-resources/template-autogenerated/drush-docker-compose.yml new file mode 100644 index 00000000..e0fb34ec --- /dev/null +++ b/cmd/test-resources/template-autogenerated/drush-docker-compose.yml @@ -0,0 +1,59 @@ +version: '2' +services: + cli: + build: + context: . + dockerfile: builder.dockerfile + image: builder + labels: + lagoon.type: cli-persistent + lagoon.persistent: /app/web/sites/default/files/ + lagoon.persistent.name: nginx + environment: + LAGOON_PROJECT: ci-drupal + LAGOON_ROUTE: drupal9-mariadb.docker.amazee.io + volumes: + - ./web:/app/web:delegated + nginx: + networks: + - amazeeio-network + - default + build: + context: . + dockerfile: nginx.dockerfile + labels: + lagoon.type: nginx-php-persistent + lagoon.persistent: /app/web/sites/default/files/ + lagoon.persistent.size: 1Gi + lagoon.name: nginx + lagoon.deployment.servicetype: nginx + volumes: + - ./web:/app/web:delegated + depends_on: + - cli + expose: + - "8080" + environment: + LAGOON_LOCALDEV_URL: drupal9-mariadb.docker.amazee.io + php: + build: + context: . + dockerfile: php.dockerfile + labels: + lagoon.type: nginx-php-persistent + lagoon.persistent: /app/web/sites/default/files/ + lagoon.name: nginx + lagoon.deployment.servicetype: php + volumes: + - ./web:/app/web:delegated + depends_on: + - cli + mariadb: + build: + context: . + dockerfile: mariadb.dockerfile + labels: + lagoon.type: mariadb-single +networks: + amazeeio-network: + external: true \ No newline at end of file diff --git a/cmd/test-resources/template-autogenerated/drush-lagoon.yml b/cmd/test-resources/template-autogenerated/drush-lagoon.yml new file mode 100644 index 00000000..c2046d9d --- /dev/null +++ b/cmd/test-resources/template-autogenerated/drush-lagoon.yml @@ -0,0 +1,59 @@ +docker-compose-yaml: test-resources/template-autogenerated/drush-docker-compose.yml + +ssh: lagoon-core-ssh.lagoon.svc:2020 +api: http://lagoon-core-api.lagoon.svc:80/graphql + +environment_variables: + git_sha: 'true' + +tasks: + post-rollout: + - run: + name: env variables + command: env + service: cli + - run: + name: IF no Drupal installed, install Drupal + command: | + cd web + if tables=$(drush sqlq "show tables like \"node\";") && [ -z "$tables" ]; then + # no drupal installed, we install drupal from an db dump + zcat ../drupal.sql.gz | $(drush sql-connect) + fi + # set the sitename to the current SHA and BRANCH + drush -y cset system.site name "LAGOON_GIT_SHA=$LAGOON_GIT_SHA LAGOON_GIT_BRANCH=$LAGOON_GIT_BRANCH" + service: cli + shell: bash + - run: + name: drush cr + command: cd web && drush -y cr + service: cli + +environments: + master: + routes: + - nginx: + - "domain.com": + tls-acme: 'true' + insecure: Allow + - www.domain.com + + cronjobs: + - name: drush cron + schedule: "1 * * * *" + command: drush cron + service: cli + + drupal9-php80-mariadb-dbaas: + cronjobs: + - name: drush cron + schedule: "1 * * * *" + command: drush cron + service: cli + + drupal9-php80-mariadb-single: + cronjobs: + - name: drush cron + schedule: "1 * * * *" + command: drush cron + service: cli \ No newline at end of file diff --git a/cmd/test-resources/template-autogenerated/test17-results/nginx.yaml b/cmd/test-resources/template-autogenerated/test17-results/nginx.yaml new file mode 100644 index 00000000..d26a6e98 --- /dev/null +++ b/cmd/test-resources/template-autogenerated/test17-results/nginx.yaml @@ -0,0 +1,45 @@ +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + annotations: + fastly.amazee.io/watch: "false" + ingress.kubernetes.io/ssl-redirect: "false" + kubernetes.io/tls-acme: "true" + lagoon.sh/branch: drush-first + 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: "false" + creationTimestamp: null + labels: + app.kubernetes.io/instance: nginx + 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: branch + lagoon.sh/environment: drush-first + lagoon.sh/environmentType: development + lagoon.sh/project: ci-drush-la-control-k8s + lagoon.sh/service: nginx + lagoon.sh/service-type: nginx-php-persistent + name: nginx +spec: + rules: + - host: nginx.ci-drush-la-control-k8s.drush-first.172.18.0.3.nip.io + http: + paths: + - backend: + service: + name: nginx + port: + name: http + path: / + pathType: Prefix + tls: + - hosts: + - nginx.ci-drush-la-control-k8s.drush-first.172.18.0.3.nip.io + secretName: nginx-tls +status: + loadBalancer: {} diff --git a/internal/templating/routes/templates.go b/internal/templating/routes/templates.go index d2231296..a011e6c2 100644 --- a/internal/templating/routes/templates.go +++ b/internal/templating/routes/templates.go @@ -21,7 +21,7 @@ func GenerateIngressTemplate( monitoringContact, monitoringStatusPageID string, monitoringEnabled bool, -) []byte { +) ([]byte, error) { // generate the name for the ingress fromt he route domain // shorten it if required with a hash @@ -220,12 +220,15 @@ func GenerateIngressTemplate( // @TODO: we should review this in the future when we stop doing `kubectl apply` in the builds :) // marshal the resulting ingress - ingressBytes, _ := yaml.Marshal(ingress) + ingressBytes, err := yaml.Marshal(ingress) + if err != nil { + return nil, err + } // add the seperator to the template so that it can be `kubectl apply` in bulk as part // of the current build process separator := []byte("---\n") result := append(separator[:], ingressBytes[:]...) - return result + return result, nil } // WriteTemplateFile writes the template to a file. diff --git a/internal/templating/routes/templates_test.go b/internal/templating/routes/templates_test.go index 9df78b60..eaf2d58b 100644 --- a/internal/templating/routes/templates_test.go +++ b/internal/templating/routes/templates_test.go @@ -128,7 +128,10 @@ func TestGenerateKubeTemplate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := GenerateIngressTemplate(tt.args.route, tt.args.values, tt.args.monitoringContact, tt.args.monitoringStatusPageID, tt.args.monitoringEnabled) + got, err := GenerateIngressTemplate(tt.args.route, tt.args.values, tt.args.monitoringContact, tt.args.monitoringStatusPageID, tt.args.monitoringEnabled) + if err != nil { + t.Errorf("couldn't generate template %v: %v", tt.want, err) + } r1, err := os.ReadFile(tt.want) if err != nil { t.Errorf("couldn't read file %v: %v", tt.want, err)