Skip to content

Commit 639717b

Browse files
author
Matthieu
committed
feat(postgres): add postgis image flavor
Introduce a `flavor: postgresql | postgis` enum on the postgres app. `postgis` swaps the CNPG image to `ghcr.io/cloudnative-pg/postgis`, which ships PostGIS and companion extensions (postgis_topology, postgis_raster, pgrouting, address_standardizer) precompiled, so tenants can activate them per database via `databases.<name>.extensions`. The cluster and the post-install init Job share a single `postgres.imageName` helper to stay wire-compatible. Tag mapping for the postgis variant lives in `files/postgis-versions.yaml` (v13-v17 covered; v18 returns a clear error at install). Signed-off-by: Matthieu <[email protected]>
1 parent e4f9a18 commit 639717b

10 files changed

Lines changed: 168 additions & 15 deletions

File tree

‎api/apps/v1alpha1/postgresql/types.go‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/apps/postgres/README.md‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,49 @@ See:
158158

159159
- <https://cloudnative-pg.io/documentation/1.15/rolling_update/#manual-updates-supervised>
160160

161+
### How to enable PostGIS
162+
163+
Set `flavor: postgis` to bootstrap the cluster from the CloudNativePG
164+
PostGIS image (`ghcr.io/cloudnative-pg/postgis`) instead of the standard
165+
PostgreSQL image. The variant ships PostGIS and its companion extensions
166+
(`postgis_topology`, `postgis_raster`, `pgrouting`,
167+
`address_standardizer`) precompiled, so they can be activated per
168+
database via `databases.<name>.extensions`:
169+
170+
```yaml
171+
spec:
172+
flavor: postgis
173+
version: v17
174+
databases:
175+
gis:
176+
extensions:
177+
- postgis
178+
- postgis_topology
179+
```
180+
181+
`flavor` is a bootstrap-time choice. Switching it on an existing release
182+
swaps the cluster image and CNPG will fail to restart the existing pods
183+
if the on-disk format does not match — plan a fresh release (with
184+
`bootstrap.recovery` from a backup if needed) when changing `flavor`.
185+
PostgreSQL v18 is not yet covered by the PostGIS image; pick a version
186+
in `[v13, v17]` when `flavor: postgis`.
187+
161188
## Parameters
162189

163190
### Common parameters
164191

165-
| Name | Description | Type | Value |
166-
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ | ---------- | ---------- |
167-
| `replicas` | Number of Postgres replicas. | `int` | `2` |
168-
| `resources` | Explicit CPU and memory configuration for each PostgreSQL replica. When omitted, the preset defined in `resourcesPreset` is applied. | `object` | `{}` |
169-
| `resources.cpu` | CPU available to each replica. | `quantity` | `""` |
170-
| `resources.memory` | Memory (RAM) available to each replica. | `quantity` | `""` |
171-
| `resourcesPreset` | Default sizing preset used when `resources` is omitted. | `string` | `t1.micro` |
172-
| `size` | Persistent Volume Claim size available for application data. | `quantity` | `10Gi` |
173-
| `storageClass` | StorageClass used to store the data. | `string` | `""` |
174-
| `external` | Enable external access from outside the cluster. | `bool` | `false` |
175-
| `version` | PostgreSQL major version to deploy | `string` | `v18` |
192+
| Name | Description | Type | Value |
193+
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ------------ |
194+
| `replicas` | Number of Postgres replicas. | `int` | `2` |
195+
| `resources` | Explicit CPU and memory configuration for each PostgreSQL replica. When omitted, the preset defined in `resourcesPreset` is applied. | `object` | `{}` |
196+
| `resources.cpu` | CPU available to each replica. | `quantity` | `""` |
197+
| `resources.memory` | Memory (RAM) available to each replica. | `quantity` | `""` |
198+
| `resourcesPreset` | Default sizing preset used when `resources` is omitted. | `string` | `t1.micro` |
199+
| `size` | Persistent Volume Claim size available for application data. | `quantity` | `10Gi` |
200+
| `storageClass` | StorageClass used to store the data. | `string` | `""` |
201+
| `external` | Enable external access from outside the cluster. | `bool` | `false` |
202+
| `version` | PostgreSQL major version to deploy | `string` | `v18` |
203+
| `flavor` | PostgreSQL image flavor. `postgresql` (default) uses the standard CloudNativePG image. `postgis` switches to the CloudNativePG PostGIS image, which ships PostGIS and companion extensions (postgis_topology, postgis_raster, pgrouting, address_standardizer) precompiled. Declare the needed extensions in `databases.<name>.extensions` to enable them per database. | `string` | `postgresql` |
176204

177205

178206
### Application-specific parameters
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"v17": "17.7-3.5"
2+
"v16": "16.11-3.5"
3+
"v15": "15.15-3.5"
4+
"v14": "14.20-3.5"
5+
"v13": "13.22-3.5"

‎packages/apps/postgres/templates/_versions.tpl‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,20 @@
66
{{- index $versionMap .Values.version }}
77
{{- end }}
88

9+
{{- define "postgres.postgisVersionMap" }}
10+
{{- $versionMap := .Files.Get "files/postgis-versions.yaml" | fromYaml }}
11+
{{- if not (hasKey $versionMap .Values.version) }}
12+
{{- printf `PostgreSQL version %s is not supported by the postgis flavor, allowed versions are %s` $.Values.version (keys $versionMap | sortAlpha) | fail }}
13+
{{- end }}
14+
{{- index $versionMap .Values.version }}
15+
{{- end }}
16+
17+
{{- define "postgres.imageName" -}}
18+
{{- $flavor := default "postgresql" .Values.flavor -}}
19+
{{- if eq $flavor "postgis" -}}
20+
{{- printf "ghcr.io/cloudnative-pg/postgis:%s" (include "postgres.postgisVersionMap" . | trim) -}}
21+
{{- else -}}
22+
{{- printf "ghcr.io/cloudnative-pg/postgresql:%s" (include "postgres.versionMap" . | trim | trimPrefix "v") -}}
23+
{{- end -}}
24+
{{- end -}}
25+

‎packages/apps/postgres/templates/db.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ spec:
9292

9393
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 4 }}
9494

95-
imageName: ghcr.io/cloudnative-pg/postgresql:{{ include "postgres.versionMap" $ | trimPrefix "v" }}
95+
imageName: {{ include "postgres.imageName" $ }}
9696
enableSuperuserAccess: true
9797
{{- if .Values._cluster.scheduling }}
9898
{{- $rawConstraints := get .Values._cluster.scheduling "globalAppTopologySpreadConstraints" }}

‎packages/apps/postgres/templates/init-job.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ spec:
2929
restartPolicy: Never
3030
containers:
3131
- name: postgres
32-
image: ghcr.io/cloudnative-pg/postgresql:{{ include "postgres.versionMap" $ | trimPrefix "v" }}
32+
image: {{ include "postgres.imageName" $ }}
3333
command:
3434
- bash
3535
- /scripts/init.sh
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
suite: image flavor selection
2+
3+
# Locks in the `flavor` enum -> image mapping. The cluster and the
4+
# post-install init Job must always agree on the image so that the Job's
5+
# psql is wire-compatible with the running cluster; the helper
6+
# `postgres.imageName` is shared between both templates for that reason.
7+
8+
release:
9+
name: pg-test
10+
namespace: tenant-test
11+
12+
set:
13+
_cluster:
14+
scheduling: {}
15+
16+
tests:
17+
- it: defaults to the standard postgresql image
18+
set:
19+
version: v17
20+
templates:
21+
- templates/db.yaml
22+
documentSelector:
23+
path: kind
24+
value: Cluster
25+
asserts:
26+
- equal:
27+
path: spec.imageName
28+
value: ghcr.io/cloudnative-pg/postgresql:17.7
29+
30+
- it: explicit flavor=postgresql resolves to the standard image
31+
set:
32+
version: v17
33+
flavor: postgresql
34+
templates:
35+
- templates/db.yaml
36+
documentSelector:
37+
path: kind
38+
value: Cluster
39+
asserts:
40+
- equal:
41+
path: spec.imageName
42+
value: ghcr.io/cloudnative-pg/postgresql:17.7
43+
44+
- it: flavor=postgis switches the Cluster image to the postgis variant
45+
set:
46+
version: v17
47+
flavor: postgis
48+
templates:
49+
- templates/db.yaml
50+
documentSelector:
51+
path: kind
52+
value: Cluster
53+
asserts:
54+
- equal:
55+
path: spec.imageName
56+
value: ghcr.io/cloudnative-pg/postgis:17.7-3.5
57+
58+
- it: flavor=postgis switches the init Job image to the postgis variant
59+
set:
60+
version: v17
61+
flavor: postgis
62+
templates:
63+
- templates/init-job.yaml
64+
documentSelector:
65+
path: kind
66+
value: Job
67+
asserts:
68+
- equal:
69+
path: spec.template.spec.containers[0].image
70+
value: ghcr.io/cloudnative-pg/postgis:17.7-3.5
71+
72+
- it: flavor=postgis fails for a PostgreSQL version with no postgis tag
73+
set:
74+
version: v18
75+
flavor: postgis
76+
templates:
77+
- templates/init-job.yaml
78+
asserts:
79+
- failedTemplate:
80+
errorMessage: "PostgreSQL version v18 is not supported by the postgis flavor, allowed versions are [v13 v14 v15 v16 v17]"

‎packages/apps/postgres/values.schema.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@
131131
"v13"
132132
]
133133
},
134+
"flavor": {
135+
"description": "PostgreSQL image flavor. `postgresql` (default) uses the standard CloudNativePG image. `postgis` switches to the CloudNativePG PostGIS image, which ships PostGIS and companion extensions (postgis_topology, postgis_raster, pgrouting, address_standardizer) precompiled. Declare the needed extensions in `databases.\u003cname\u003e.extensions` to enable them per database.",
136+
"type": "string",
137+
"default": "postgresql",
138+
"enum": [
139+
"postgresql",
140+
"postgis"
141+
]
142+
},
134143
"postgresql": {
135144
"description": "PostgreSQL server configuration.",
136145
"type": "object",

‎packages/apps/postgres/values.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ external: false
8585
## @param {Version} version - PostgreSQL major version to deploy
8686
version: v18
8787

88+
##
89+
## @enum {string} Flavor
90+
## @value postgresql
91+
## @value postgis
92+
93+
## @param {Flavor} flavor - PostgreSQL image flavor. `postgresql` (default) uses the standard CloudNativePG image. `postgis` switches to the CloudNativePG PostGIS image, which ships PostGIS and companion extensions (postgis_topology, postgis_raster, pgrouting, address_standardizer) precompiled. Declare the needed extensions in `databases.<name>.extensions` to enable them per database.
94+
flavor: postgresql
95+
8896
## @section Application-specific parameters
8997
##
9098

0 commit comments

Comments
 (0)