blob: 1e156f4ef8a18a9fe0ac5ed940aa4141347abe3b [file] [log] [blame]
Bartosz Stebel65b30af2022-11-18 14:45:55 +01001# PostgreSQL on Kubernetes, with versioned names
2
3local kube = import "kube.libsonnet";
4
5{
6 local postgres = self,
7 local cfg = postgres.cfg,
8 cfg:: {
9 namespace: error "namespace must be set",
10 appName: error "app name must be set",
11 storageClassName: "waw-hdd-paranoid-2",
12 prefix: "", # if set, should be 'foo-'
13 version: "10.4", # valid version tag for https://hub.docker.com/_/postgres/
14
15 image: "postgres:" + cfg.version,
16 database: error "database must be set",
17 username: error "username must be set",
18 # not literal, instead ref for env (like { secretKeyRef: ... })
19 password: error "password must be set",
20
21 storageSize: "30Gi",
22
23 # This option can be used to customize initial database creation. For
24 # available options see: https://www.postgresql.org/docs/9.5/app-initdb.html
25 # Changing this option in already existing deployments will not affect
26 # existing database.
27 initdbArgs: null,
28
29 # Extra postgres configuration options passed on startup. Accepts only
30 # string values.
31 # Example: { max_connections: "300" }
32 opts: {},
33 },
34 safeVersion:: std.strReplace(cfg.version, ".", "-"),
35
36 makeName(suffix):: cfg.prefix + suffix + postgres.safeVersion,
37
38 metadata:: {
39 namespace: cfg.namespace,
40 labels: {
41 "app.kubernetes.io/name": cfg.appName,
42 "app.kubernetes.io/managed-by": "kubecfg",
43 "app.kubernetes.io/component": "postgres_v",
44 "hswaw.net/postgres-version": postgres.safeVersion,
45 },
46 },
47
48 volumeClaim: kube.PersistentVolumeClaim(postgres.makeName("postgres")) {
49 metadata+: postgres.metadata,
50 spec+: {
51 storageClassName: cfg.storageClassName,
52 accessModes: [ "ReadWriteOnce" ],
53 resources: {
54 requests: {
55 storage: cfg.storageSize,
56 },
57 },
58 },
59 },
60 deployment: kube.Deployment(postgres.makeName("postgres")) {
61 metadata+: postgres.metadata,
62 spec+: {
63 replicas: 1,
64 template+: {
65 spec+: {
66 volumes_: {
67 data: kube.PersistentVolumeClaimVolume(postgres.volumeClaim),
68 },
69 containers_: {
70 postgres: kube.Container(postgres.makeName("postgres")) {
71 image: cfg.image,
72 ports_: {
73 client: { containerPort: 5432 },
74 },
75 env_: {
76 POSTGRES_DB: cfg.database,
77 POSTGRES_USER: cfg.username,
78 POSTGRES_PASSWORD: cfg.password,
79 PGDATA: "/var/lib/postgresql/data/pgdata",
80 } + if cfg.initdbArgs != null then {
81 POSTGRES_INITDB_ARGS: cfg.initdbArgs,
82 } else {},
83
84 args: std.flatMap(
85 function(k) ["-c", "%s=%s" % [k, cfg.opts[k]]],
86 std.objectFields(cfg.opts),
87 ),
88
89 volumeMounts_: {
90 data: { mountPath: "/var/lib/postgresql/data" },
91 },
92 },
93 },
94 securityContext: {
95 runAsUser: 999,
96 },
97 },
98 },
99 },
100 },
101
102 svc: kube.Service(postgres.makeName("postgres")) {
103 metadata+: postgres.metadata,
104 target_pod:: postgres.deployment.spec.template,
105 spec+: {
106 ports: [
107 { name: "client", port: 5432, targetPort: 5432, protocol: "TCP" },
108 ],
109 type: "ClusterIP",
110 },
111 },
112
113 bouncer: {
114 deployment: kube.Deployment(postgres.makeName("bouncer")) {
115 metadata+: postgres.metadata {
116 labels+: {
117 "app.kubernetes.io/component": "bouncer_v",
118 }
119 },
120 spec+: {
121 replicas: 1,
122 template+: {
123 spec+: {
124 containers_: {
125 bouncer: kube.Container(postgres.makeName("bouncer")) {
126 image: "edoburu/pgbouncer:1.11.0",
127 ports_: {
128 client: { containerPort: 5432 },
129 },
130 env: [
131 { name: "POSTGRES_PASSWORD", valueFrom: cfg.password },
132 { name: "DATABASE_URL", value: "postgres://%s:$(POSTGRES_PASSWORD)@%s/%s" % [cfg.username, postgres.svc.host, cfg.database] },
133 ],
134 },
135 },
136 },
137 },
138 },
139 },
140 svc: kube.Service(postgres.makeName("bouncer")) {
141 metadata+: postgres.metadata {
142 labels+: {
143 "app.kubernetes.io/component": "bouncer",
144 }
145 },
146 target_pod:: postgres.bouncer.deployment.spec.template,
147 spec+: {
148 ports: [
149 { name: "client", port: 5432, targetPort: 5432, protocol: "TCP" },
150 ],
151 type: "ClusterIP",
152 },
153 },
154 },
155}