blob: ec7a7b8085565d4c829ce32b31b1bc45cece09f5 [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,
radex36964dc2023-11-24 11:19:46 +010050 storage:: cfg.storageSize,
51 storageClass:: cfg.storageClassName,
Bartosz Stebel65b30af2022-11-18 14:45:55 +010052 },
53 deployment: kube.Deployment(postgres.makeName("postgres")) {
54 metadata+: postgres.metadata,
55 spec+: {
56 replicas: 1,
57 template+: {
58 spec+: {
59 volumes_: {
60 data: kube.PersistentVolumeClaimVolume(postgres.volumeClaim),
61 },
62 containers_: {
63 postgres: kube.Container(postgres.makeName("postgres")) {
64 image: cfg.image,
65 ports_: {
66 client: { containerPort: 5432 },
67 },
68 env_: {
69 POSTGRES_DB: cfg.database,
70 POSTGRES_USER: cfg.username,
71 POSTGRES_PASSWORD: cfg.password,
72 PGDATA: "/var/lib/postgresql/data/pgdata",
73 } + if cfg.initdbArgs != null then {
74 POSTGRES_INITDB_ARGS: cfg.initdbArgs,
75 } else {},
76
77 args: std.flatMap(
78 function(k) ["-c", "%s=%s" % [k, cfg.opts[k]]],
79 std.objectFields(cfg.opts),
80 ),
81
82 volumeMounts_: {
83 data: { mountPath: "/var/lib/postgresql/data" },
84 },
85 },
86 },
87 securityContext: {
88 runAsUser: 999,
89 },
90 },
91 },
92 },
93 },
94
95 svc: kube.Service(postgres.makeName("postgres")) {
96 metadata+: postgres.metadata,
radex8b8f3872023-11-24 11:09:46 +010097 target:: postgres.deployment,
Bartosz Stebel65b30af2022-11-18 14:45:55 +010098 spec+: {
99 ports: [
100 { name: "client", port: 5432, targetPort: 5432, protocol: "TCP" },
101 ],
102 type: "ClusterIP",
103 },
104 },
105
106 bouncer: {
107 deployment: kube.Deployment(postgres.makeName("bouncer")) {
108 metadata+: postgres.metadata {
109 labels+: {
110 "app.kubernetes.io/component": "bouncer_v",
111 }
112 },
113 spec+: {
114 replicas: 1,
115 template+: {
116 spec+: {
117 containers_: {
118 bouncer: kube.Container(postgres.makeName("bouncer")) {
119 image: "edoburu/pgbouncer:1.11.0",
120 ports_: {
121 client: { containerPort: 5432 },
122 },
123 env: [
124 { name: "POSTGRES_PASSWORD", valueFrom: cfg.password },
125 { name: "DATABASE_URL", value: "postgres://%s:$(POSTGRES_PASSWORD)@%s/%s" % [cfg.username, postgres.svc.host, cfg.database] },
126 ],
127 },
128 },
129 },
130 },
131 },
132 },
133 svc: kube.Service(postgres.makeName("bouncer")) {
134 metadata+: postgres.metadata {
135 labels+: {
136 "app.kubernetes.io/component": "bouncer",
137 }
138 },
radex8b8f3872023-11-24 11:09:46 +0100139 target:: postgres.bouncer.deployment,
Bartosz Stebel65b30af2022-11-18 14:45:55 +0100140 spec+: {
141 ports: [
142 { name: "client", port: 5432, targetPort: 5432, protocol: "TCP" },
143 ],
144 type: "ClusterIP",
145 },
146 },
147 },
148}