Skip to content

Commit

Permalink
Add init-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr committed Oct 6, 2023
1 parent 3e8e683 commit 04eaa27
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ TODO
| init-secrets.serviceAccount.create | bool | `true` | |
| init-secrets.serviceAccount.enabled | bool | `true` | |
| init-secrets.serviceAccount.name | string | `nil` | |
| init-sql.enabled | bool | `true` | |
| minio.consoleIngress.enabled | bool | `false` | |
| minio.consoleService.type | string | `"NodePort"` | |
| minio.enabled | bool | `true` | |
Expand Down
20 changes: 20 additions & 0 deletions diracx/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,23 @@ Create the name of the service account to use for init-secrets job
{{ coalesce $initSecretsValues.serviceAccount.name .Values.global.serviceAccount.name "default" }}
{{- end -}}
{{- end -}}

{{/*
Return the fullname template for the init-sql job.
*/}}
{{- define "init-sql.fullname" -}}
{{- printf "%s-init-sql" .Release.Name -}}
{{- end -}}

{{/*
Create a default fully qualified job name for init-sql.
Due to the job only being allowed to run once, we add the chart revision so helm
upgrades don't cause errors trying to create the already ran job.
Due to the helm delete not cleaning up these jobs, we add a random value to
reduce collisions.
*/}}
{{- define "init-sql.jobname" -}}
{{- $name := include "init-sql.fullname" . | trunc 55 | trimSuffix "-" -}}
{{- $rand := randAlphaNum 3 | lower }}
{{- printf "%s-%d-%s" $name .Release.Revision $rand | trunc 63 | trimSuffix "-" -}}
{{- end -}}
20 changes: 0 additions & 20 deletions diracx/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,6 @@ spec:
{{- end }}

initContainers:
{{- if .Values.diracx.manageSQLSchema.enabled }}
- name: create-sql-db-schema
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
command: ["bash", "/entrypoint.sh"]
args: ["python", "-m", "diracx.db", "init-sql"]
volumeMounts: {{ toYaml $commonVolumeMounts | nindent 10 }}
{{- if .Values.mysql.enabled }}
env:
{{- range $dbName := .Values.diracx.mysqlDatabases }}
- name: DIRACX_DB_URL_{{ $dbName | upper }}
valueFrom:
secretKeyRef:
name: diracx-sql-root-connection-urls
key: DIRACX_DB_URL_{{ $dbName | upper }}
{{- end }}
{{- end }}
envFrom:
- secretRef:
name: diracx-init-mysql-secrets
{{- end }}
{{- if .Values.diracx.manageOSIndices }}
- name: create-os-db-indices
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
Expand Down
5 changes: 5 additions & 0 deletions diracx/templates/init-sql/_init-sql.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

python -m diracx.db init-sql
15 changes: 15 additions & 0 deletions diracx/templates/init-sql/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- $initSQLValues := index .Values "init-sql" "enabled" -}}
{{- if $initSQLValues -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "init-sql.fullname" . }}
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": post-install,pre-upgrade
"helm.sh/hook-weight": "-3"
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
data:
init-sql: |
{{- include (print $.Template.BasePath "/init-sql/_init-sql.sh.tpl") . | nindent 4 }}
{{- end -}}
82 changes: 82 additions & 0 deletions diracx/templates/init-sql/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{{- $initSQLValues := index .Values "init-sql" -}}
{{- if $initSQLValues.enabled }}

{{/* Define common volume mounts for reusability */}}
{{- $commonVolumeMounts := list }}
{{- $commonVolumeMounts = append $commonVolumeMounts (dict "mountPath" "/entrypoint.sh" "name" "container-entrypoint" "subPath" "entrypoint.sh") }}
{{- if and .Values.developer.enabled .Values.developer.enableCoverage }}
{{- $commonVolumeMounts = append $commonVolumeMounts (dict "mountPath" "/diracx-coveragerc" "name" "container-entrypoint" "subPath" "coveragerc") }}
{{- $commonVolumeMounts = append $commonVolumeMounts (dict "mountPath" "/coverage-reports" "name" "coverage-data" "readOnly" false) }}
{{- end }}
{{- if and .Values.developer.enabled .Values.developer.modulesToEditableInstall }}
{{- $commonVolumeMounts = append $commonVolumeMounts (dict "mountPath" .Values.developer.sourcePath "name" "diracx-code-mount" "readOnly" true) }}
{{- range $module := .Values.developer.modulesToEditableInstall }}
{{- $commonVolumeMounts = append $commonVolumeMounts (dict "mountPath" (printf "%s/%s/src/%s.egg-info" $.Values.developer.sourcePath $module $module) "name" (printf "%s-editable-install" (lower $module)) "readOnly" false) }}
{{- end }}
{{- end }}

apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "init-sql.jobname" . }}
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": post-install,pre-upgrade
"helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
spec:
template:
metadata:
annotations:
{{- range $key, $value := $initSQLValues.annotations }}
{{ $key }}: {{ $value | quote }}
{{- end }}
spec:
restartPolicy: Never
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/bin/bash", "/entrypoint.sh"]
args: ["/bin/bash", "/scripts/init-sql"]
volumeMounts:
{{ toYaml $commonVolumeMounts | nindent 12 }}
- name: scripts
mountPath: /scripts
{{- if .Values.mysql.enabled }}
env:
{{- range $dbName := .Values.diracx.mysqlDatabases }}
- name: DIRACX_DB_URL_{{ $dbName | upper }}
valueFrom:
secretKeyRef:
name: diracx-sql-root-connection-urls
key: DIRACX_DB_URL_{{ $dbName | upper }}
{{- end }}
{{- end }}
envFrom:
- secretRef:
name: diracx-init-mysql-secrets
resources:
{{- toYaml $initSQLValues.resources | nindent 12 }}
volumes:
- name: scripts
configMap:
name: {{ template "init-sql.fullname" . }}
{{- if and .Values.developer.enabled .Values.developer.modulesToEditableInstall }}
- name: diracx-code-mount
persistentVolumeClaim:
claimName: pvc-diracx-code
{{- range $module := .Values.developer.modulesToEditableInstall }}
- name: {{ lower $module }}-editable-install
empty-dir:
sizeLimit: 5Mi
{{- end }}
{{- end }}
- name: container-entrypoint
configMap:
name: diracx-container-entrypoint
{{- if and .Values.developer.enabled .Values.developer.enableCoverage }}
- name: coverage-data
persistentVolumeClaim:
claimName: pvc-coverage
{{- end }}
{{- end }}
2 changes: 2 additions & 0 deletions diracx/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ init-secrets:
create: true
name: # Specify a pre-existing ServiceAccount name

init-sql:
enabled: true

developer:
enabled: true
Expand Down

0 comments on commit 04eaa27

Please sign in to comment.