Skip to content

Commit abba7a4

Browse files
authored
Merge pull request #841 from MetaCell/feature/CH-256-postgres-operator
CH-256 feat: add PostgreSQL operator support
2 parents 92b6ec2 + 85c0715 commit abba7a4

17 files changed

Lines changed: 619 additions & 362 deletions

File tree

.devcontainer/.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Dev container user configuration
2+
# Copy this file to .env and customize as needed
3+
# These values will bind the container user to your host user for seamless file sharing
4+
5+
# Your username (defaults to 'devuser' if not set)
6+
USERNAME=${USER}
7+
8+
# Your user ID (run 'id -u' on host to get this)
9+
USER_UID=1000
10+
11+
# Your group ID (run 'id -g' on host to get this)
12+
USER_GID=1000

.devcontainer/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
home
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{{- define "deploy_utils.database.postgres.operator" }}
2+
---
3+
apiVersion: v1
4+
kind: Secret
5+
metadata:
6+
name: {{ printf "%s-credentials" .app.harness.database.name | quote }}
7+
namespace: {{ .root.Values.namespace }}
8+
labels:
9+
app: {{ .app.harness.database.name | quote }}
10+
{{ include "deploy_utils.labels" .root | indent 4 }}
11+
type: kubernetes.io/basic-auth
12+
stringData:
13+
username: {{ .app.harness.database.user | quote }}
14+
password: {{ .app.harness.database.pass | quote }}
15+
---
16+
apiVersion: postgresql.cnpg.io/v1
17+
kind: Cluster
18+
metadata:
19+
name: {{ .app.harness.database.name | quote }}
20+
namespace: {{ .root.Values.namespace }}
21+
labels:
22+
app: {{ .app.harness.database.name | quote }}
23+
{{ include "deploy_utils.labels" .root | indent 4 }}
24+
spec:
25+
instances: {{ .app.harness.database.postgres.instances | default 1 }}
26+
27+
inheritedMetadata:
28+
labels:
29+
app: {{ .app.harness.database.name | quote }}
30+
service: db
31+
32+
bootstrap:
33+
initdb:
34+
database: {{ .app.harness.database.postgres.initialdb | quote }}
35+
owner: {{ .app.harness.database.user | quote }}
36+
secret:
37+
name: {{ printf "%s-credentials" .app.harness.database.name | quote }}
38+
39+
storage:
40+
size: {{ .app.harness.database.size }}
41+
42+
{{- with .app.harness.database.resources }}
43+
resources:
44+
{{- with .requests }}
45+
requests:
46+
{{- with .memory }}
47+
memory: {{ . | quote }}
48+
{{- end }}
49+
{{- with .cpu }}
50+
cpu: {{ . | quote }}
51+
{{- end }}
52+
{{- end }}
53+
{{- with .limits }}
54+
limits:
55+
{{- with .memory }}
56+
memory: {{ . | quote }}
57+
{{- end }}
58+
{{- end }}
59+
{{- end }}
60+
61+
{{- if .app.harness.database.image_ref }}
62+
imageName: {{ index (index .app "task-images") .app.harness.database.image_ref | default ("Image ref not found!" | quote) }}
63+
{{- else if .app.harness.database.postgres.image }}
64+
imageName: {{ .app.harness.database.postgres.image | quote }}
65+
{{- end }}
66+
---
67+
apiVersion: v1
68+
kind: Service
69+
metadata:
70+
name: {{ .app.harness.database.name | quote }}
71+
namespace: {{ .root.Values.namespace }}
72+
labels:
73+
app: {{ .app.harness.deployment.name | quote }}
74+
{{ include "deploy_utils.labels" .root | indent 4 }}
75+
spec:
76+
type: {{ if .app.harness.database.expose }}LoadBalancer{{ else }}ClusterIP{{ end }}
77+
selector:
78+
app: {{ .app.harness.database.name | quote }}
79+
cnpg.io/instanceRole: primary
80+
ports:
81+
{{- range $port := .app.harness.database.postgres.ports }}
82+
- name: {{ $port.name }}
83+
port: {{ $port.port }}
84+
targetPort: 5432
85+
{{- end }}
86+
{{- end }}

deployment-configuration/helm/templates/auto-database.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ spec:
1414
---
1515
{{- end }}
1616
{{- define "deploy_utils.database" }}
17+
{{- if and (eq .app.harness.database.type "postgres") .app.harness.database.postgres.operator }}
18+
{{- include "deploy_utils.database.postgres.operator" . }}
19+
{{- else }}
1720
---
1821
kind: PersistentVolumeClaim
1922
apiVersion: v1
@@ -105,6 +108,7 @@ spec:
105108
- name: {{ $port.name }}
106109
port: {{ $port.port }}
107110
{{- end }}
111+
{{- end }}
108112
---
109113
{{- include "deploy_utils.database_network_policy" (dict "root" .root "app" .app) }}
110114
{{ end }}

deployment-configuration/helm/templates/auto-network-policies.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,38 @@ spec:
117117
protocol: UDP
118118
- port: 53
119119
protocol: TCP
120+
{{- if and (eq .app.harness.database.type "postgres") .app.harness.database.postgres.operator }}
121+
# Allow CNPG pods to reach the Kubernetes API server
122+
{{- $apiCidrs := list }}
123+
{{- $kubeSvc := (lookup "v1" "Service" "default" "kubernetes") }}
124+
{{- $kubeEp := (lookup "v1" "Endpoints" "default" "kubernetes") }}
125+
{{- if $kubeSvc }}
126+
{{- if $kubeSvc.spec }}
127+
{{- if $kubeSvc.spec.clusterIP }}
128+
{{- $apiCidrs = append $apiCidrs (printf "%s/32" $kubeSvc.spec.clusterIP) }}
129+
{{- end }}
130+
{{- end }}
131+
{{- end }}
132+
{{- if and $kubeEp $kubeEp.subsets }}
133+
{{- range $subset := $kubeEp.subsets }}
134+
{{- range $addr := $subset.addresses }}
135+
{{- $apiCidrs = append $apiCidrs (printf "%s/32" $addr.ip) }}
136+
{{- end }}
137+
{{- end }}
138+
{{- end }}
139+
{{- /* Fall back to configured CIDRs when lookup returns nothing (e.g. helm template) */ -}}
140+
{{- if not $apiCidrs }}
141+
{{- $apiCidrs = .app.harness.database.postgres.apiServerCidr }}
142+
{{- end }}
143+
{{- range $cidr := $apiCidrs }}
144+
- to:
145+
- ipBlock:
146+
cidr: {{ $cidr }}
147+
ports:
148+
- port: 443
149+
protocol: TCP
150+
{{- end }}
151+
{{- end }}
120152
{{- range $ns := $allowedNamespaces }}
121153
# Allow traffic to whitelisted namespace
122154
- to:

deployment-configuration/helm/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ registry:
1414
# -- Optional secret used for pulling from docker registry.
1515
secret:
1616
# -- Secret name used for pulling from docker registry.
17-
name:
17+
name: e
1818
# -- Value of the secret used for pulling from docker registry in json encoded format.
19-
value:
19+
value: sd
2020
# -- Docker tag used to pull images.
2121
tag: latest
2222
# -- List of applications.

deployment-configuration/value-template.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@ harness:
9595
port: 27017
9696
# -- settings for postgers database (for type==postgres)
9797
postgres:
98-
image: postgres:13
98+
image:
9999
initialdb: cloudharness
100+
# -- Use the CloudNative-PG operator instead of a plain Deployment. Requires the CNPG operator to be installed in the cluster.
101+
operator: false
102+
# -- Number of PostgreSQL instances managed by the CNPG operator (only used when operator is true)
103+
instances: 1
104+
# -- CIDR(s) allowed for CNPG pods to reach the Kubernetes API server (port 443).
105+
# -- Resolved automatically at deploy time via cluster lookup. Set explicitly only as a fallback for helm-template or air-gapped use.
106+
apiServerCidr: []
100107
ports:
101108
- name: http
102109
port: 5432

0 commit comments

Comments
 (0)