Skip to content

Commit 4cd965f

Browse files
committed
Deploy: added PGsql charts.
1 parent 2f8af89 commit 4cd965f

14 files changed

Lines changed: 426 additions & 1 deletion

deploy/Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ HELM_OPENSEARCH_NAMESPACE ?= cogstack
1313
HELM_OPENSEARCH_CHART ?= ./charts/opensearch
1414
HELM_OPENSEARCH_VALUES_FILE ?= ./helm/opensearch.values.yaml
1515
HELM_OPENSEARCH_VALUES_ARG = -f $(HELM_OPENSEARCH_VALUES_FILE)
16+
HELM_POSTGRESQL_RELEASE ?= cogstack-postgresql
17+
HELM_POSTGRESQL_NAMESPACE ?= cogstack
18+
HELM_POSTGRESQL_CHART ?= ./charts/postgresql
19+
HELM_POSTGRESQL_VALUES_FILE ?= ./helm/postgresql.values.yaml
20+
HELM_POSTGRESQL_VALUES_ARG = -f $(HELM_POSTGRESQL_VALUES_FILE)
21+
CNPG_OPERATOR_MINOR ?= 1.28
22+
CNPG_OPERATOR_VERSION ?= 1.28.1
23+
CNPG_OPERATOR_MANIFEST ?= https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-$(CNPG_OPERATOR_MINOR)/releases/cnpg-$(CNPG_OPERATOR_VERSION).yaml
1624

1725
define WITH_ENV
1826
set -a && source ./export_env_vars.sh;
@@ -80,6 +88,15 @@ helm-template-opensearch: ## Render OpenSearch chart using chart defaults plus .
8088
helm-install-opensearch: ## Install/upgrade OpenSearch chart using chart defaults plus ./helm/opensearch.values.yaml
8189
helm upgrade --install $(HELM_OPENSEARCH_RELEASE) $(HELM_OPENSEARCH_CHART) $(HELM_OPENSEARCH_VALUES_ARG) --namespace $(HELM_OPENSEARCH_NAMESPACE) --create-namespace
8290

91+
kube-install-cnpg-operator: ## Install CloudNativePG operator/CRDs from the pinned upstream manifest
92+
kubectl apply --server-side -f $(CNPG_OPERATOR_MANIFEST)
93+
94+
helm-template-postgresql: ## Render CloudNativePG PostgreSQL chart using chart defaults plus ./helm/postgresql.values.yaml
95+
helm template $(HELM_POSTGRESQL_RELEASE) $(HELM_POSTGRESQL_CHART) $(HELM_POSTGRESQL_VALUES_ARG) --namespace $(HELM_POSTGRESQL_NAMESPACE)
96+
97+
helm-install-postgresql: ## Install/upgrade CloudNativePG PostgreSQL chart using chart defaults plus ./helm/postgresql.values.yaml
98+
helm upgrade --install $(HELM_POSTGRESQL_RELEASE) $(HELM_POSTGRESQL_CHART) $(HELM_POSTGRESQL_VALUES_ARG) --namespace $(HELM_POSTGRESQL_NAMESPACE) --create-namespace
99+
83100

84101
remote-deploy-service: ## Deploy one or more services to a remote machine via SSH + docker compose
85102
remote-deploy-service: _check-remote-params
@@ -107,7 +124,7 @@ _check-remote-params:
107124
exit 1; \
108125
fi
109126

110-
.PHONY: remote-deploy-service remote-stop-service remote-delete-service _check-remote-params helm-template-opensearch helm-install-opensearch
127+
.PHONY: remote-deploy-service remote-stop-service remote-delete-service _check-remote-params helm-template-opensearch helm-install-opensearch kube-install-cnpg-operator helm-template-postgresql helm-install-postgresql
111128

112129
# start services
113130

deploy/charts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This directory contains Helm charts owned by this repository's deployment layer.
1010
## Current charts
1111

1212
- `opensearch/` - OpenSearch and/or OpenSearch Dashboards chart used by this repo.
13+
- `postgresql/` - CloudNativePG-backed PostgreSQL cluster chart used by this repo.
1314

1415
## Quick usage
1516

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v2
2+
name: cogstack-postgresql
3+
description: CloudNativePG-backed PostgreSQL cluster for CogStack deployments
4+
type: application
5+
version: 0.1.0
6+
appVersion: "18.1"

deploy/charts/postgresql/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# cogstack-postgresql Helm Chart
2+
3+
Helm chart for deploying the repo's production PostgreSQL database on Kubernetes using the CloudNativePG operator.
4+
5+
## What this chart deploys
6+
7+
- A CloudNativePG `Cluster` resource for PostgreSQL
8+
- An application credentials `Secret` by default
9+
- A bootstrap SQL `ConfigMap` sourced from `services/cogstack-db/pgsql/schemas/`
10+
11+
## Prerequisites
12+
13+
1. Install the CloudNativePG operator and CRDs first.
14+
2. Have a working default `StorageClass`, or set `cluster.storage.storageClass` in your values file.
15+
3. Use at least 3 worker nodes for HA production deployments.
16+
17+
Pinned operator install example:
18+
19+
```bash
20+
kubectl apply --server-side -f \
21+
https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.28/releases/cnpg-1.28.1.yaml
22+
```
23+
24+
## Install
25+
26+
```bash
27+
helm upgrade --install cogstack-postgresql ./deploy/charts/postgresql \
28+
-f ./deploy/helm/postgresql.values.yaml \
29+
--namespace cogstack --create-namespace
30+
```
31+
32+
## Render templates
33+
34+
```bash
35+
helm template cogstack-postgresql ./deploy/charts/postgresql \
36+
-f ./deploy/helm/postgresql.values.yaml \
37+
--namespace cogstack
38+
```
39+
40+
## Notes
41+
42+
- The chart consumes shared repo sources through bundled files under `deploy/charts/postgresql/files/`.
43+
- `deploy/database.env` is used as the default source for `DATABASE_DB_NAME`, `POSTGRES_DB_MAX_CONNECTIONS`, and the default CPU/memory sizing hints.
44+
- `security/env/users_database.env` is used as the default source for `DATABASE_USER` and `DATABASE_PASSWORD`.
45+
- The bootstrap SQL comes from the shared PostgreSQL schema files under `services/cogstack-db/pgsql/schemas/`.
46+
- `POSTGRES_DB_SCHEMA_PREFIX` is respected when selecting bundled custom schema files after `annotations_nlp_create_schema.sql`.
47+
- Docker-specific settings that do not map cleanly to Kubernetes, such as `DATABASE_DOCKER_SHM_SIZE`, are intentionally not consumed.
48+
- By default, the application owner is not granted PostgreSQL superuser. If you need compose-like behavior, set `cluster.ownerSuperuser=true`.
49+
- Backups are intentionally not enabled by default in this chart. Wire object storage and WAL archiving before using this for production data.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../deploy/database.env
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../services/cogstack-db/pgsql/schemas/annotations_nlp_create_schema.sql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../services/cogstack-db/pgsql/schemas/cogstack_db_databank.sql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../security/env/users_database.env
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CloudNativePG operator resources must already be installed before this chart can be applied.
2+
3+
PostgreSQL services created by the operator:
4+
- read/write primary service: {{ include "cogstack-postgresql.fullname" . }}-rw
5+
- read-only replicas service: {{ include "cogstack-postgresql.fullname" . }}-ro
6+
- any-instance service: {{ include "cogstack-postgresql.fullname" . }}-r
7+
8+
Application credentials secret:
9+
- {{ include "cogstack-postgresql.appSecretName" . }}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{{/* Chart name */}}
2+
{{- define "cogstack-postgresql.name" -}}
3+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
4+
{{- end -}}
5+
6+
{{/* Fully qualified name */}}
7+
{{- define "cogstack-postgresql.fullname" -}}
8+
{{- if .Values.fullnameOverride -}}
9+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
10+
{{- else -}}
11+
{{- $name := default .Chart.Name .Values.nameOverride -}}
12+
{{- if contains $name .Release.Name -}}
13+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
14+
{{- else -}}
15+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
16+
{{- end -}}
17+
{{- end -}}
18+
{{- end -}}
19+
20+
{{/* Common labels */}}
21+
{{- define "cogstack-postgresql.labels" -}}
22+
helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" }}
23+
app.kubernetes.io/name: {{ include "cogstack-postgresql.name" . }}
24+
app.kubernetes.io/instance: {{ .Release.Name }}
25+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
26+
app.kubernetes.io/managed-by: {{ .Release.Service }}
27+
{{- end -}}
28+
29+
{{/* Application credentials secret name */}}
30+
{{- define "cogstack-postgresql.appSecretName" -}}
31+
{{- if .Values.credentials.existingAppSecret -}}
32+
{{- .Values.credentials.existingAppSecret -}}
33+
{{- else -}}
34+
{{- printf "%s-app" (include "cogstack-postgresql.fullname" .) -}}
35+
{{- end -}}
36+
{{- end -}}
37+
38+
{{/* Bootstrap SQL configmap name */}}
39+
{{- define "cogstack-postgresql.initdbConfigMapName" -}}
40+
{{- printf "%s-initdb" (include "cogstack-postgresql.fullname" .) -}}
41+
{{- end -}}
42+
43+
{{/* Normalize docker-style memory strings to Kubernetes quantities */}}
44+
{{- define "cogstack-postgresql.normalizeMemory" -}}
45+
{{- $value := toString . | trim -}}
46+
{{- if regexMatch "^[0-9]+g$" $value -}}
47+
{{- printf "%sGi" (trimSuffix "g" $value) -}}
48+
{{- else if regexMatch "^[0-9]+m$" $value -}}
49+
{{- printf "%sMi" (trimSuffix "m" $value) -}}
50+
{{- else if regexMatch "^[0-9]+k$" $value -}}
51+
{{- printf "%sKi" (trimSuffix "k" $value) -}}
52+
{{- else -}}
53+
{{- $value -}}
54+
{{- end -}}
55+
{{- end -}}
56+
57+
{{/* Parse deploy/database.env into a filtered YAML map */}}
58+
{{- define "cogstack-postgresql.parsedEnvFile" -}}
59+
{{- $root := . -}}
60+
{{- $envData := dict -}}
61+
{{- $rawEnv := $root.Values.envFile.raw | default ($root.Files.Get "files/deploy-database.envfile") -}}
62+
{{- if $rawEnv -}}
63+
{{- $renderedEnv := tpl $rawEnv $root -}}
64+
{{- range $line := splitList "\n" $renderedEnv }}
65+
{{- $clean := trim (replace "\r" "" $line) -}}
66+
{{- if and $clean (not (hasPrefix "#" $clean)) -}}
67+
{{- if regexMatch "^[A-Za-z_][A-Za-z0-9_]*=" $clean -}}
68+
{{- $key := regexFind "^[A-Za-z_][A-Za-z0-9_]*" $clean -}}
69+
{{- $val := trim (regexReplaceAll "^[A-Za-z_][A-Za-z0-9_]*=" $clean "") -}}
70+
{{- if has $key $root.Values.envFile.includeKeys -}}
71+
{{- if and (hasPrefix "\"" $val) (hasSuffix "\"" $val) -}}
72+
{{- $val = trimSuffix "\"" (trimPrefix "\"" $val) -}}
73+
{{- else if and (hasPrefix "'" $val) (hasSuffix "'" $val) -}}
74+
{{- $val = trimSuffix "'" (trimPrefix "'" $val) -}}
75+
{{- end -}}
76+
{{- if regexMatch "^\\$[A-Za-z_][A-Za-z0-9_]*$" $val -}}
77+
{{- $refKey := trimPrefix "$" $val -}}
78+
{{- if hasKey $envData $refKey -}}
79+
{{- $val = index $envData $refKey -}}
80+
{{- end -}}
81+
{{- end -}}
82+
{{- $_ := set $envData $key $val -}}
83+
{{- end -}}
84+
{{- end -}}
85+
{{- end -}}
86+
{{- end -}}
87+
{{- end -}}
88+
{{ toYaml $envData }}
89+
{{- end -}}
90+
91+
{{/* Parse security/env/users_database.env into a filtered YAML map */}}
92+
{{- define "cogstack-postgresql.parsedUsersEnvFile" -}}
93+
{{- $root := . -}}
94+
{{- $usersData := dict -}}
95+
{{- $rawUsers := $root.Values.usersEnvFile.raw | default ($root.Files.Get "files/users-database.envfile") -}}
96+
{{- if $rawUsers -}}
97+
{{- $renderedUsers := tpl $rawUsers $root -}}
98+
{{- range $line := splitList "\n" $renderedUsers }}
99+
{{- $clean := trim (replace "\r" "" $line) -}}
100+
{{- if and $clean (not (hasPrefix "#" $clean)) -}}
101+
{{- if regexMatch "^[A-Za-z_][A-Za-z0-9_]*=" $clean -}}
102+
{{- $key := regexFind "^[A-Za-z_][A-Za-z0-9_]*" $clean -}}
103+
{{- $val := trim (regexReplaceAll "^[A-Za-z_][A-Za-z0-9_]*=" $clean "") -}}
104+
{{- if has $key $root.Values.usersEnvFile.includeKeys -}}
105+
{{- if and (hasPrefix "\"" $val) (hasSuffix "\"" $val) -}}
106+
{{- $val = trimSuffix "\"" (trimPrefix "\"" $val) -}}
107+
{{- else if and (hasPrefix "'" $val) (hasSuffix "'" $val) -}}
108+
{{- $val = trimSuffix "'" (trimPrefix "'" $val) -}}
109+
{{- end -}}
110+
{{- $_ := set $usersData $key $val -}}
111+
{{- end -}}
112+
{{- end -}}
113+
{{- end -}}
114+
{{- end -}}
115+
{{- end -}}
116+
{{ toYaml $usersData }}
117+
{{- end -}}

0 commit comments

Comments
 (0)