Run migrations in Helm post-install/upgrade hook Job (mysql/postgres)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cbbeb0cd1b
commit
02884ed30d
7 changed files with 127 additions and 1 deletions
|
|
@ -20,6 +20,8 @@ docker compose up --build # frontend :8080, backend, spamassassin
|
|||
Kubernetes: `helm install mail-tester ./helm -f my-values.yaml` (frontend and backend are separate deployments,
|
||||
SpamAssassin is a sidecar of the backend; backend scales with `replicaCountBackend` on mysql/postgres; sqlite is limited to 1).
|
||||
|
||||
Migrations run at backend startup; `EXECUTE_MIGRATIONS=false` disables that and `mailtester migrate` applies them once and exits (the Helm chart does this in a post-install/post-upgrade hook Job for mysql/postgres).
|
||||
|
||||
Database: `DB_DRIVER=sqlite|mysql|postgres` with `DB_HOST`, `DB_PORT`, `DB_USER`, `DB_PASSWORD`, `DB_NAME`
|
||||
(`DB_SSLMODE` for postgres; for sqlite `DB_NAME` is the file path). See `.env.example`.
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ package main
|
|||
import (
|
||||
"golang.org/x/time/rate"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
|
|
@ -25,6 +27,10 @@ func docs(r *gin.Engine) {
|
|||
|
||||
func main() {
|
||||
log.Init()
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
||||
runMigrate()
|
||||
return
|
||||
}
|
||||
domain := config.GetOrFatal("MAIL_DOMAIN")
|
||||
jmapURL := config.GetOrFatal("JMAP_URL")
|
||||
token := config.GetOrDefault("JMAP_TOKEN", "")
|
||||
|
|
@ -41,6 +47,12 @@ func main() {
|
|||
if err != nil {
|
||||
log.Log.Fatal("database: ", err)
|
||||
}
|
||||
// EXECUTE_MIGRATIONS=false when migrations run elsewhere (Helm hook Job: "mailtester migrate")
|
||||
if config.GetOrDefault("EXECUTE_MIGRATIONS", "true") == "true" {
|
||||
if err := migrate(db); err != nil {
|
||||
log.Log.Fatal("migrations: ", err)
|
||||
}
|
||||
}
|
||||
go runWorker(db, NewJMAP(jmapURL, user, pass, token), config.GetOrDefault("SPAMD_ADDR", ""))
|
||||
|
||||
s := &server{db: db, domain: domain, limits: map[string]*rate.Limiter{}}
|
||||
|
|
@ -49,3 +61,27 @@ func main() {
|
|||
srv := &http.Server{Addr: addr, Handler: s.routes()}
|
||||
log.Log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
|
||||
// runMigrate applies migrations and exits. It retries while the database is not accepting connections yet.
|
||||
func runMigrate() {
|
||||
timeout, err := time.ParseDuration(config.GetOrDefault("MIGRATE_WAIT_TIMEOUT", "5m"))
|
||||
if err != nil {
|
||||
log.Log.Fatal("MIGRATE_WAIT_TIMEOUT: ", err)
|
||||
}
|
||||
deadline := time.Now().Add(timeout)
|
||||
for {
|
||||
db, err := openDB()
|
||||
if err == nil {
|
||||
if err = migrate(db); err != nil {
|
||||
log.Log.Fatal("migrations: ", err)
|
||||
}
|
||||
log.Log.Info("migrations applied")
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
log.Log.Fatal("database: ", err)
|
||||
}
|
||||
log.Log.Warn("database not ready: ", err)
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ func openDB() (*gorm.DB, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, migrate(db)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// runWorker polls JMAP for mail of waiting tests and purges everything older than 24h.
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ func TestClaimIsExclusive(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := migrate(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Create(&Test{ID: "a", Status: "waiting", Checks: []Check{}, CreatedAt: time.Now()})
|
||||
var won atomic.Int32
|
||||
var wg sync.WaitGroup
|
||||
|
|
|
|||
|
|
@ -93,6 +93,15 @@ app.kubernetes.io/instance: {{ .Release.Name }}
|
|||
- name: TRUSTED_PROXIES
|
||||
value: {{ .Values.trustedProxies | quote }}
|
||||
{{- end }}
|
||||
{{- if include "mail-tester.migrationJob" . }}
|
||||
- name: EXECUTE_MIGRATIONS
|
||||
value: "false" # the migrate hook Job does it
|
||||
{{- end }}
|
||||
{{ include "mail-tester.dbEnv" . }}
|
||||
{{- end }}
|
||||
|
||||
{{/* Only the DB_* settings; shared by the backend and the migrate Job. */}}
|
||||
{{- define "mail-tester.dbEnv" -}}
|
||||
- name: DB_DRIVER
|
||||
value: {{ .Values.db.driver | quote }}
|
||||
{{- if eq .Values.db.driver "sqlite" }}
|
||||
|
|
@ -120,3 +129,8 @@ app.kubernetes.io/instance: {{ .Release.Name }}
|
|||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{/* Non-empty when migrations run in a hook Job: networked DB and migrations.job.enabled. */}}
|
||||
{{- define "mail-tester.migrationJob" -}}
|
||||
{{- if and .Values.migrations.job.enabled (ne .Values.db.driver "sqlite") -}}true{{- end -}}
|
||||
{{- end }}
|
||||
|
|
|
|||
58
helm/templates/job-migrate.yaml
Normal file
58
helm/templates/job-migrate.yaml
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
{{- /*
|
||||
Applies database migrations as a Helm hook Job, so backend replicas don't migrate on startup.
|
||||
Needs a networked database (mysql/postgres): sqlite lives on the backend's ReadWriteOnce volume,
|
||||
so there the backend migrates itself on startup. On a fresh install the hook has to be post-install,
|
||||
because the DB password Secret is created by this release.
|
||||
*/ -}}
|
||||
{{- if include "mail-tester.migrationJob" . }}
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: {{ include "mail-tester.fullname" . }}-migrate
|
||||
labels:
|
||||
{{- include "mail-tester.labels" . | nindent 4 }}
|
||||
mail-tester/service: migrate
|
||||
annotations:
|
||||
"helm.sh/hook": {{ .Values.migrations.job.hooks | quote }}
|
||||
"helm.sh/hook-weight": "0"
|
||||
# Replace the previous Job on the next run, remove it once it succeeded; a failed one is kept for its logs.
|
||||
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
|
||||
spec:
|
||||
backoffLimit: {{ .Values.migrations.job.backoffLimit }}
|
||||
{{- with .Values.migrations.job.activeDeadlineSeconds }}
|
||||
activeDeadlineSeconds: {{ . }}
|
||||
{{- end }}
|
||||
template:
|
||||
metadata:
|
||||
{{- with .Values.podAnnotations }}
|
||||
annotations:
|
||||
{{- toYaml . | nindent 8 }}
|
||||
{{- end }}
|
||||
labels:
|
||||
mail-tester/service: migrate
|
||||
{{- include "mail-tester.selectorLabels" . | nindent 8 }}
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
{{- with .Values.imagePullSecrets }}
|
||||
imagePullSecrets:
|
||||
{{- toYaml . | nindent 8 }}
|
||||
{{- end }}
|
||||
serviceAccountName: {{ include "mail-tester.serviceAccountName" . }}
|
||||
securityContext:
|
||||
{{- toYaml .Values.podSecurityContext | nindent 8 }}
|
||||
containers:
|
||||
- name: migrate
|
||||
securityContext:
|
||||
{{- toYaml .Values.securityContext | nindent 12 }}
|
||||
image: "{{ .Values.image.repositoryBackend }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
|
||||
imagePullPolicy: {{ .Values.image.pullPolicy }}
|
||||
args: ["migrate"]
|
||||
env:
|
||||
{{- include "mail-tester.dbEnv" . | nindent 12 }}
|
||||
- name: LOG_LEVEL
|
||||
value: {{ .Values.logLevel | quote }}
|
||||
- name: MIGRATE_WAIT_TIMEOUT
|
||||
value: {{ .Values.migrations.job.waitTimeout | quote }}
|
||||
resources:
|
||||
{{- toYaml .Values.migrations.job.resources | nindent 12 }}
|
||||
{{- end }}
|
||||
|
|
@ -79,6 +79,19 @@ persistence:
|
|||
storageClass: ""
|
||||
existingClaim: ""
|
||||
|
||||
migrations:
|
||||
job:
|
||||
# Run migrations in a Helm hook Job instead of in every backend pod at startup.
|
||||
# Only for mysql/postgres; with sqlite the backend always migrates itself.
|
||||
enabled: true
|
||||
# A fresh install needs post-install (the DB Secret is created by this release).
|
||||
# "post-install,pre-upgrade" migrates before new pods roll out on upgrades (works with an external DB).
|
||||
hooks: "post-install,post-upgrade"
|
||||
waitTimeout: 5m # keeps retrying while the database is not accepting connections yet
|
||||
backoffLimit: 3
|
||||
activeDeadlineSeconds: 900
|
||||
resources: {}
|
||||
|
||||
trustedProxies: "" # e.g. the ingress controller CIDR, needed for correct per-IP rate limiting
|
||||
logLevel: INFO
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue