Skip to content

Commit

Permalink
Merge pull request #30 from uselagoon/template-errors
Browse files Browse the repository at this point in the history
chore: expose template errors if there are any
  • Loading branch information
shreddedbacon authored May 17, 2022
2 parents 9277a09 + cc9e6c4 commit 84f06e7
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 7 deletions.
5 changes: 4 additions & 1 deletion cmd/template_autogen_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/template_autogen_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions cmd/template_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
59 changes: 59 additions & 0 deletions cmd/test-resources/template-autogenerated/drush-docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions cmd/test-resources/template-autogenerated/drush-lagoon.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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: {}
9 changes: 6 additions & 3 deletions internal/templating/routes/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion internal/templating/routes/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 84f06e7

Please sign in to comment.