Skip to content

Commit 3f7ead0

Browse files
committed
G2P-5032 Seeding of strategy added.
1 parent 44d231d commit 3f7ead0

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{- if .Values.seedData.enabled }}
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: {{ include "common.names.fullname" $ }}-seed-data
6+
labels:
7+
{{- include "spar.labels" . | nindent 4 }}
8+
data:
9+
seed.sql: |
10+
-- SPAR strategy seed data. Idempotent: each row is inserted by its pinned
11+
-- id with ON CONFLICT (id) DO NOTHING, so existing rows are never changed.
12+
{{- range .Values.seedData.strategies }}
13+
INSERT INTO strategy (id, description, strategy_type, construct_strategy, deconstruct_strategy, active, created_at, updated_at)
14+
VALUES ({{ .id }}, '{{ .description | replace "'" "''" }}', '{{ .strategy_type | replace "'" "''" }}', '{{ .construct_strategy | replace "'" "''" }}', '{{ .deconstruct_strategy | replace "'" "''" }}', {{ if hasKey . "active" }}{{ .active }}{{ else }}true{{ end }}, now(), now())
15+
ON CONFLICT (id) DO NOTHING;
16+
{{- end }}
17+
{{- end }}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{{- if .Values.seedData.enabled }}
2+
apiVersion: batch/v1
3+
kind: Job
4+
metadata:
5+
name: {{ include "common.names.fullname" $ }}-seed-data-{{ .Release.Revision }}
6+
labels:
7+
{{- include "spar.labels" . | nindent 4 }}
8+
annotations:
9+
# Run after the release's resources are in place (post-install) and on every
10+
# upgrade. The script itself waits for the DB and the `strategy` table
11+
# (created by the API `migrate`), so it is resilient to ordering.
12+
"helm.sh/hook": post-install,post-upgrade
13+
"helm.sh/hook-weight": "5"
14+
# Keep the Job after a successful run (do NOT use hook-succeeded) so it stays
15+
# visible for inspection; it's removed before the next run (before-hook-creation)
16+
# and garbage-collected by ttlSecondsAfterFinished.
17+
"helm.sh/hook-delete-policy": before-hook-creation
18+
spec:
19+
backoffLimit: 6
20+
ttlSecondsAfterFinished: 600
21+
template:
22+
metadata:
23+
labels:
24+
{{- include "spar.labels" . | nindent 8 }}
25+
spec:
26+
restartPolicy: OnFailure
27+
{{- with .Values.sparMapperAPI.imagePullSecrets }}
28+
imagePullSecrets:
29+
{{- toYaml . | nindent 8 }}
30+
{{- end }}
31+
containers:
32+
- name: seed-data
33+
image: {{ .Values.seedData.image }}
34+
env:
35+
- name: PGHOST
36+
value: '{{ tpl .Values.global.postgresqlHost $ }}'
37+
- name: PGPORT
38+
value: "5432"
39+
- name: PGDATABASE
40+
value: '{{ tpl .Values.global.sparDB $ }}'
41+
- name: PGUSER
42+
value: '{{ tpl .Values.global.sparDBUser $ }}'
43+
- name: PGPASSWORD
44+
valueFrom:
45+
secretKeyRef:
46+
name: '{{ tpl .Values.global.sparDBSecret $ }}'
47+
key: '{{ tpl .Values.global.sparDBUserPasswordKey $ }}'
48+
command:
49+
- sh
50+
- -c
51+
- |
52+
set -e
53+
echo "Waiting for database ${PGHOST}:${PGPORT}/${PGDATABASE} ..."
54+
until pg_isready -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE"; do sleep 3; done
55+
echo "Waiting for 'strategy' table (created by the API migrate) ..."
56+
until [ -n "$(psql -tAc "SELECT to_regclass('public.strategy')")" ]; do sleep 5; done
57+
echo "Seeding strategy table ..."
58+
psql -v ON_ERROR_STOP=1 -f /seed/seed.sql
59+
echo "Seed complete."
60+
volumeMounts:
61+
- name: seed-sql
62+
mountPath: /seed
63+
readOnly: true
64+
volumes:
65+
- name: seed-sql
66+
configMap:
67+
name: {{ include "common.names.fullname" $ }}-seed-data
68+
{{- end }}

deployment/charts/spar/values.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ keycloak-init:
6666
redirectUris: []
6767
webOrigins: []
6868

69+
# Seeds shared SPAR reference data (the `strategy` table) at install/upgrade via
70+
# a post-install/post-upgrade Job. Seeding is ADDITIVE and IDEMPOTENT: each row
71+
# is inserted by its pinned `id` with `ON CONFLICT (id) DO NOTHING`, so existing
72+
# rows are never modified or deleted. To add a strategy later, append a new entry
73+
# with a new `id` and run `helm upgrade` — nothing else changes.
74+
#
75+
# A Strategy defines how a Financial Address (FA) — or an ID — is serialized and
76+
# parsed:
77+
# construct_strategy : a Python format string, e.g. "{bank_code}-{acc}"
78+
# deconstruct_strategy : a regex with named groups, e.g. "(?P<bank_code>.*)-(?P<acc>.*)"
79+
# The keys must match the FA/ID field names. The link/update API references a
80+
# strategy by its integer `id` (fa.strategy_id), so ids must stay stable.
81+
seedData:
82+
enabled: true
83+
# Image providing psql / pg_isready for the seed Job.
84+
image: jbergknoff/postgresql-client
85+
strategies:
86+
- id: 1
87+
description: Key Cloak
88+
strategy_type: ID
89+
construct_strategy: 'token:{sub}@nationalId'
90+
deconstruct_strategy: '^token:(?P<sub>.[^.]*)@nationalId$'
91+
- id: 2
92+
description: Bank
93+
strategy_type: FA
94+
construct_strategy: 'account_number:{account_number}.branch_name:{branch_name}.branch_code:{branch_code}.bank_name:{bank_name}.bank_code:{bank_code}.fa_type:{fa_type}'
95+
deconstruct_strategy: '^account_number:(?P<account_number>.*)\.branch_name:(?P<branch_name>.*)\.branch_code:(?P<branch_code>.*)\.bank_name:(?P<bank_name>.*)\.bank_code:(?P<bank_code>.*)\.fa_type:(?P<fa_type>.*)$'
96+
- id: 3
97+
description: Email
98+
strategy_type: FA
99+
construct_strategy: 'email_address:{email_address}.wallet_provider_name:{wallet_provider_name}.wallet_provider_code:{wallet_provider_code}.fa_type:{fa_type}'
100+
deconstruct_strategy: '^email_address:(?P<email_address>.*)\.wallet_provider_name:(?P<wallet_provider_name>.*)\.wallet_provider_code:(?P<wallet_provider_code>.*)\.fa_type:(?P<fa_type>.*)$'
101+
- id: 4
102+
description: Phone
103+
strategy_type: FA
104+
construct_strategy: 'mobile_number:{mobile_number}.wallet_provider_name:{wallet_provider_name}.wallet_provider_code:{wallet_provider_code}.fa_type:{fa_type}'
105+
deconstruct_strategy: '^mobile_number:(?P<mobile_number>.*)\.wallet_provider_name:(?P<wallet_provider_name>.*)\.wallet_provider_code:(?P<wallet_provider_code>.*)\.fa_type:(?P<fa_type>.*)$'
106+
69107
sparMapperAPI:
70108
enabled: true
71109
sparHostname: spar.trial.openg2p.org

0 commit comments

Comments
 (0)