Skip to content

Commit

Permalink
add helm migration hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Aug 25, 2024
1 parent c307de4 commit ecc2f35
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 6 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/packages-migrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ on:
pull_request:
branches: ["*"]
paths:
- packages/db/**
- packages/db/drizzle/**
- .github/workflows/packages-migrations.yaml
- pnpm-lock.yaml
push:
branches: ["main"]
paths:
- packages/db/**
- packages/db/drizzle/**
- .github/workflows/packages-migrations.yaml
- pnpm-lock.yamlaa

jobs:
build:
Expand Down
7 changes: 5 additions & 2 deletions deploy/ctrlplane/Chart.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ dependencies:
- name: job-policy-checker
repository: file://charts/job-policy-checker
version: 0.1.0
digest: sha256:f886849879773b410f68b2d3293065605b5c82987ad285d9c974b9f9ee0f3bd2
generated: "2024-08-23T23:39:43.317846-04:00"
- name: migrations
repository: file://charts/migrations
version: 0.1.0
digest: sha256:7009169bab87614dc2bf33508ac69a2ce674c4565a6fdc0f3b571ad461c7d991
generated: "2024-08-24T23:49:27.165058-04:00"
4 changes: 4 additions & 0 deletions deploy/ctrlplane/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ dependencies:
version: "*.*.*"
repository: file://charts/job-policy-checker
condition: job-policy-checker.install
- name: migrations
version: "*.*.*"
repository: file://charts/migrations
condition: migrations.install
23 changes: 23 additions & 0 deletions deploy/ctrlplane/charts/migrations/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
6 changes: 6 additions & 0 deletions deploy/ctrlplane/charts/migrations/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: migrations
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
62 changes: 62 additions & 0 deletions deploy/ctrlplane/charts/migrations/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "migrations.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "migrations.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "migrations.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "migrations.labels" -}}
helm.sh/chart: {{ include "migrations.chart" . }}
{{ include "migrations.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "migrations.selectorLabels" -}}
app.kubernetes.io/name: {{ include "migrations.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "migrations.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "migrations.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
35 changes: 35 additions & 0 deletions deploy/ctrlplane/charts/migrations/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "migrations.fullname" . }}
labels:
{{- include "migrations.labels" . | nindent 4 }}
{{- if .Values.job.labels }}
{{- toYaml .Values.job.labels | nindent 4 }}
{{- end }}
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
{{- if .Values.job.annotations }}
{{- toYaml .Values.job.annotations | nindent 4 }}
{{- end }}
spec:
activeDeadlineSeconds: {{ .Values.activeDeadlineSeconds }}
backoffLimit: {{ .Values.backoffLimit }}
template:
metadata:
name: "database"
labels:
{{- include "migrations.labels" . | nindent 8 }}
spec:
restartPolicy: OnFailure
automountServiceAccountToken: false
containers:
- name: migrations
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
{{- include "ctrlplane.extraEnv" . | nindent 12 }}
{{- include "ctrlplane.extraEnvFrom" (dict "root" $ "local" .) | nindent 12 }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
28 changes: 28 additions & 0 deletions deploy/ctrlplane/charts/migrations/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
nameOverride: ""
fullnameOverride: ""

activeDeadlineSeconds: 3600
backoffLimit: 6

image:
repository: ctrlplane/migrations
tag: latest
pullPolicy: Always

extraEnv: {}
extraEnvFrom: {}

tolerations: []
pod: {}

job:
labels: {}
annotations: {}

resources:
requests:
cpu: 500m
memory: 1Gi
limits:
cpu: 1000m
memory: 4Gi
3 changes: 3 additions & 0 deletions deploy/ctrlplane/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ webservice:
job-policy-checker:
install: true

migrations:
install: true

ingress:
create: true
class: ""
Expand Down
50 changes: 50 additions & 0 deletions deploy/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: "3.8"

services:
postgres:
image: postgres:15
container_name: postgres
environment:
POSTGRES_USER: ctrlplane
POSTGRES_PASSWORD: ctrlplane
POSTGRES_DB: ctrlplane
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
environment:
RABBITMQ_DEFAULT_USER: ctrlplane
RABBITMQ_DEFAULT_PASS: ctrlplane
ports:
- "5672:5672"
- "15672:15672"

webservice:
image: ctrlplane/webservice
container_name: webservice
environment:
AUTH_SECRET: "<auth-secret>"
DATABASE_URL: postgres://ctrlplane:ctrlplane@postgres:5432/ctrlplane
RABBITMQ_URL: amqp://ctrlplane:ctrlplane@rabbitmq:5672
depends_on:
- postgres
- rabbitmq
ports:
- "8080:8080"

job-policy-checker:
image: ctrlplane/my-job-policy-checker-image
container_name: job-policy-checker
environment:
POSTGRES_URL: postgres://ctrlplane:ctrlplane@postgres:5432/ctrlplane
RABBITMQ_URL: amqp://ctrlplane:ctrlplane@rabbitmq:5672
depends_on:
- postgres
- rabbitmq

volumes:
postgres_data:

0 comments on commit ecc2f35

Please sign in to comment.