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 3e0ac69
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ TODO
| dex.service.ports.http.port | int | `8000` | |
| dex.service.type | string | `"NodePort"` | |
| diracx.manageOSIndices | bool | `true` | |
| diracx.manageSQLSchema.enabled | bool | `true` | |
| diracx.manageSQLSchema.env | object | `{}` | |
| diracx.modulesToInstall | list | `[]` | |
| diracx.mysqlDatabases[0] | string | `"AuthDB"` | |
| diracx.mysqlDatabases[1] | string | `"JobDB"` | |
Expand All @@ -121,6 +119,8 @@ TODO
| init-secrets.serviceAccount.create | bool | `true` | |
| init-secrets.serviceAccount.enabled | bool | `true` | |
| init-secrets.serviceAccount.name | string | `nil` | |
| init-sql.enabled | bool | `true` | |
| init-sql.env | object | `{}` | |
| 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 }}
4 changes: 2 additions & 2 deletions diracx/templates/secrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ stringData:
{{- end }}
{{- end }}
---
{{- if .Values.diracx.manageSQLSchema.enabled }}
{{- if index .Values "init-sql" "enabled" }}
apiVersion: v1
kind: Secret
metadata:
name: diracx-init-mysql-secrets
stringData:
{{ .Values.diracx.manageSQLSchema.env | toYaml | indent 2 }}
{{ (index .Values "init-sql" "env") | toYaml | indent 2 }}
---
{{- end }}
{{- if .Values.diracx.manageOSIndices }}
Expand Down
8 changes: 4 additions & 4 deletions diracx/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ init-secrets:
create: true
name: # Specify a pre-existing ServiceAccount name

init-sql:
# Should DiracX include an init container which manages the SQL DB schema?
enabled: true
env: {}

developer:
enabled: true
Expand All @@ -85,10 +89,6 @@ diracx:
DIRACX_CONFIG_BACKEND_URL: "git+file:///cs_store/initialRepo"
DIRACX_SERVICE_AUTH_TOKEN_KEY: "file:///signing-key/rsa256.key"
DIRACX_SERVICE_AUTH_ALLOWED_REDIRECTS: '["http://anything:8000/docs/oauth2-redirect"]'
# Should DiracX include an init container which manages the SQL DB schema?
manageSQLSchema:
enabled: true
env: {}
# Should DiracX include an init container which manages the OS DB indices?
manageOSIndices: true
# Which DiracX MySQL DBs are used?
Expand Down

0 comments on commit 3e0ac69

Please sign in to comment.