blob: 6bf614409253db829e3acaef971d2d6a6d800ad0 [file] [log] [blame]
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +01001local kube = import "../../../kube/kube.libsonnet";
2
3{
4 local app = self,
5 local cfg = app.cfg,
6 cfg:: {
7 image: error "cfg.image needs to be set",
8 storageClassName: error "cfg.storrageClassName needs to be set",
9
10 # webDomain is the domain name at which synapse instance will run
11 webDomain: error "cfg.webDomain must be set",
12 # serverName is the server part of the MXID this homeserver will cover
13 serverName: error "cfg.serverName must be set",
14
15 cas: { enable: false },
16 oidc: { enable: false },
17
Piotr Dobrowolski529e1812021-02-13 19:44:37 +010018 appserviceWorker: false,
19 federationWorker: false,
20
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010021 macaroonSecretKey: error "cfg.macaroonSecretKey needs to be set",
22 registrationSharedSecret: error "cfg.registationSharedSecret needs to be set",
23 workerReplicationSecret: error "cfg.workerReplicationSecret needs to be set",
24 },
25
26 ns:: error "ns needs to be provided",
27 postgres:: error "postgres needs to be provided",
28 redis:: error "redis needs to be provided",
29
30 // See matrix-ng.libsonnet for description
31 appservices:: error "appservices need to be provided",
32
33 dataVolume: app.ns.Contain(kube.PersistentVolumeClaim("synapse-data-waw3")) {
34 spec+: {
35 storageClassName: cfg.storageClassName,
36 accessModes: [ "ReadWriteOnce" ],
37 resources: {
38 requests: {
39 storage: "50Gi",
40 },
41 },
42 },
43 },
44
45 // homeserver.yaml that will be used to run synapse (in configMap).
46 // This is based off of //app/matrix/lib/synapse/homeserver.yaml with some fields overriden per
47 // deployment.
48 config:: (std.native("parseYaml"))(importstr "synapse/homeserver-ng.yaml")[0] {
49 server_name: cfg.serverName,
50 public_baseurl: "https://%s" % [cfg.webDomain],
51 signing_key_path: "/secrets/homeserver_signing_key",
52 app_service_config_files: [
53 "/appservices/%s/registration.yaml" % [k]
54 for k in std.objectFields(app.appservices)
55 ],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +010056
57 notify_appservices: cfg.appserviceWorker == false,
58
59 # FIXME(informatic) Rolling out with federationWorkers = true breaks
60 # *some* federation, needs investigation...
61 #send_federation: cfg.federationWorker == false,
62 #federation_sender_instances: if cfg.federationWorker then [
63 # "%s-%s" % [app.federationSenderWorker.deployment.metadata.name, idx]
64 # for idx in std.range(0, app.federationSenderWorker.deployment.spec.replicas)
65 #] else [],
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010066 } + (if cfg.cas.enable then {
67 cas_config: {
68 enabled: true,
69 server_url: "https://%s/_cas" % [cfg.webDomain],
70 service_url: "https://%s" % [cfg.webDomain],
71 },
Piotr Dobrowolski690ed452022-05-07 11:27:24 +020072 } else {}) + (if cfg.coturn.enable then {
73 turn_uris: [ "turn:%s?transport=udp" % cfg.coturn.config.domain, "turn:%s?transport=tcp" % cfg.coturn.config.domain ],
74
75 # Lifetime of single TURN user credentials - 1 day, recommended by TURN REST
76 # spec, see https://datatracker.ietf.org/doc/html/draft-uberti-behave-turn-rest-00#section-2.2
77 turn_user_lifetime: 24 * 60 * 60 * 1000,
78 turn_allow_guests: true,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010079 } else {}),
80
81 configMap: app.ns.Contain(kube.ConfigMap("synapse")) {
82 data: {
83 "homeserver.yaml": std.manifestYamlDoc(app.config),
84 "log.config": importstr "synapse/log.config",
85 },
86 },
87
88 // homeserver-secrets.yaml contains all the templated secret variables from
89 // base homeserver.yaml passed as yaml-encoded environment variable.
90 // $(ENVVAR)-encoded variables are resolved by Kubernetes on pod startup
91 secretsConfig:: (std.native("parseYaml"))(importstr "synapse/homeserver-secrets.yaml")[0] {
92 } + (if cfg.oidc.enable then {
93 oidc_config: cfg.oidc.config {
94 enabled: true,
95 client_secret: "$(OIDC_CLIENT_SECRET)",
96 },
Piotr Dobrowolski690ed452022-05-07 11:27:24 +020097 } else {}) + (if cfg.coturn.enable then {
98 turn_shared_secret: "$(TURN_SHARED_SECRET)",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010099 } else {}),
100
101 # Synapse process Deployment/StatefulSet base resource.
102 SynapseWorker(name, workerType, builder):: app.ns.Contain(builder(name)) {
103 local worker = self,
104 cfg:: {
105 # Configuration customization. Can contain environment substitution
106 # syntax, as used in worker_name value.
107 localConfig: {
108 worker_app: workerType,
109 worker_name: "$(POD_NAME)",
110
111 # The replication listener on the main synapse process.
112 worker_replication_host: "synapse-replication-master",
113 worker_replication_http_port: 9093,
114 },
115
116 # Mount app.dataVolume in /data
117 mountData: false,
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200118
119 resources: {
Serge Bazanskif2628682023-03-26 21:56:09 +0200120 requests: { cpu: "300m", memory: "2Gi" },
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200121 limits: { cpu: "1500m", memory: "2Gi" },
122 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100123 },
124
125 spec+: {
126 replicas: 1,
127 template+: {
128 spec+: {
129 volumes_: {
130 config: kube.ConfigMapVolume(app.configMap),
131 secrets: { secret: { secretName: "synapse" } },
132 } + {
133 [k]: { secret: { secretName: "appservice-%s-registration" % [k] } }
134 for k in std.objectFields(app.appservices)
135 } + if worker.cfg.mountData then {
136 data: kube.PersistentVolumeClaimVolume(app.dataVolume),
137 } else {},
138 containers_: {
139 web: kube.Container("synapse") {
140 image: cfg.image,
141 command: [
142 "/bin/sh", "-c", |||
143 set -e
144 echo "${X_SECRETS_CONFIG}" > /tmp/secrets.yaml
145 echo "${X_LOCAL_CONFIG}" > /tmp/local.yaml
146 exec python -m ${SYNAPSE_WORKER} --config-path /conf/homeserver.yaml --config-path /tmp/secrets.yaml --config-path /tmp/local.yaml
147 |||
148 ],
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200149 resources: worker.cfg.resources,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100150 ports_: {
151 http: { containerPort: 8008 },
152 metrics: { containerPort: 9092 },
153 replication: { containerPort: 9093 },
154 },
155 env_: {
156 SYNAPSE_WORKER: workerType,
157
158 SYNAPSE_MACAROON_SECRET_KEY: cfg.macaroonSecretKey,
159 SYNAPSE_REGISTRATION_SHARED_SECRET: cfg.registrationSharedSecret,
160 WORKER_REPLICATION_SECRET: cfg.workerReplicationSecret,
Serge Bazanskif2628682023-03-26 21:56:09 +0200161
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100162 POSTGRES_PASSWORD: app.postgres.cfg.password,
Serge Bazanskif2628682023-03-26 21:56:09 +0200163 POSTGRES_USER: app.postgres.cfg.username,
164 POSTGRES_DB: app.postgres.cfg.database,
165 POSTGRES_HOST: app.postgres.cfg.host,
166 POSTGRES_PORT: app.postgres.cfg.port,
167
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100168 REDIS_PASSWORD: app.redis.cfg.password,
169 POD_NAME: { fieldRef: { fieldPath: "metadata.name" } },
170 OIDC_CLIENT_SECRET: if cfg.oidc.enable then cfg.oidc.config.client_secret else "",
Piotr Dobrowolski690ed452022-05-07 11:27:24 +0200171 TURN_SHARED_SECRET: if cfg.coturn.enable then cfg.coturn.config.authSecret else "",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100172
173 X_SECRETS_CONFIG: std.manifestYamlDoc(app.secretsConfig),
174 X_LOCAL_CONFIG: std.manifestYamlDoc(worker.cfg.localConfig),
175 },
176 volumeMounts_: {
177 config: { mountPath: "/conf", },
178 secrets: { mountPath: "/secrets" },
179 } + {
180 [k]: { mountPath: "/appservices/%s" % [k] }
181 for k in std.objectFields(app.appservices)
182 } + if worker.cfg.mountData then {
183 data: { mountPath: "/data" },
184 } else {},
Piotr Dobrowolski77af94d2021-09-16 22:17:58 +0200185 readinessProbe: {
186 httpGet: {
187 path: "/health",
188 port: "http",
189 },
190 initialDelaySeconds: 5,
191 periodSeconds: 10,
192 },
193 livenessProbe: {
194 httpGet: {
195 path: "/health",
196 port: "http",
197 },
198 initialDelaySeconds: 60,
199 periodSeconds: 30,
200 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100201 },
202 },
203 securityContext: {
204 runAsUser: 991,
205 runAsGroup: 991,
206 fsGroup: 991,
207 },
208 },
209 },
210 },
211 },
212
213 # Synapse main process
214 main: {
215 deployment: app.SynapseWorker("synapse", "synapse.app.homeserver", kube.Deployment) {
216 cfg+: {
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100217 localConfig: {
218 # Following configuration values need to cause master
219 # process restart.
220 notify_appservices: app.config.notify_appservices,
221 # send_federation: app.config.send_federation,
222 # federation_sender_instances: app.config.federation_sender_instances,
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200223 },
224
225 resources+: {
226 limits+: { memory: "4Gi" },
227 requests+: { memory: "2Gi" },
228 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100229 },
230 spec+: {
231 strategy+: {
232 rollingUpdate: {
233 maxSurge: 0,
234 maxUnavailable: 1,
235 },
236 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100237 },
238 },
239 svc: app.ns.Contain(kube.Service("synapse")) {
240 target_pod:: app.main.deployment.spec.template,
241 },
242 replicationSvc: app.ns.Contain(kube.Service("synapse-replication-master")) {
243 target_pod:: app.main.deployment.spec.template,
244 spec+: {
245 ports: [
246 { port: 9093, name: 'replication', targetPort: 9093 },
247 ],
248 },
249 },
250 },
251
252 genericWorker: {
253 # Synapse generic worker deployment
254 deployment: app.SynapseWorker("synapse-generic", "synapse.app.generic_worker", kube.StatefulSet) {
255 cfg+: {
256 localConfig+: {
257 worker_listeners: [{
258 type: "http",
259 port: 8008,
260 x_forwarded: true,
261 bind_addresses: ["::"],
262 resources: [{ names: ["client", "federation"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100263 }, {
264 port: 9092,
265 type: "metrics",
266 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100267 }],
268 },
269 },
270 },
271 svc: app.ns.Contain(kube.Service("synapse-generic")) {
272 target_pod:: app.genericWorker.deployment.spec.template,
273 },
274
275 # Following paths can be handled by generic workers.
276 # See: https://github.com/matrix-org/synapse/blob/master/docs/workers.md
277 paths:: [
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200278 "/_matrix/client/(r0|v3)/sync",
279 "/_matrix/client/(api/v1|r0|v3)/events",
280 "/_matrix/client/(api/v1|r0|v3)/initialSync",
281 "/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100282 "/_matrix/federation/v1/event/",
283 "/_matrix/federation/v1/state/",
284 "/_matrix/federation/v1/state_ids/",
285 "/_matrix/federation/v1/backfill/",
286 "/_matrix/federation/v1/get_missing_events/",
287 "/_matrix/federation/v1/publicRooms",
288 "/_matrix/federation/v1/query/",
289 "/_matrix/federation/v1/make_join/",
290 "/_matrix/federation/v1/make_leave/",
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200291 "/_matrix/federation/(v1|v2)/send_join/",
292 "/_matrix/federation/(v1|v2)/send_leave/",
293 "/_matrix/federation/(v1|v2)/invite/",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100294 "/_matrix/federation/v1/event_auth/",
295 "/_matrix/federation/v1/exchange_third_party_invite/",
296 "/_matrix/federation/v1/user/devices/",
297 "/_matrix/federation/v1/get_groups_publicised",
298 "/_matrix/key/v2/query",
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200299 "/_matrix/federation/(v1|unstable/org.matrix.msc2946)/hierarchy/",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100300 "/_matrix/federation/v1/send/",
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200301 "/_matrix/client/(api/v1|r0|v3|unstable)/createRoom",
302 "/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms",
303 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members",
304 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*",
305 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members",
306 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state",
307 "/_matrix/client/(v1|unstable/org.matrix.msc2946)/rooms/.*/hierarchy",
308 "/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary",
309 "/_matrix/client/(r0|v3|unstable)/account/3pid",
310 "/_matrix/client/(r0|v3|unstable)/devices",
311 "/_matrix/client/versions",
312 "/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer",
313 "/_matrix/client/(r0|v3|unstable)/joined_groups",
314 "/_matrix/client/(r0|v3|unstable)/publicised_groups",
315 "/_matrix/client/(r0|v3|unstable)/publicised_groups/",
316 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/",
317 "/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms",
318 "/_matrix/client/(api/v1|r0|v3|unstable)/search",
319 "/_matrix/client/(r0|v3|unstable)/keys/query",
320 "/_matrix/client/(r0|v3|unstable)/keys/changes",
321 "/_matrix/client/(r0|v3|unstable)/keys/claim",
322 "/_matrix/client/(r0|v3|unstable)/room_keys/",
323 "/_matrix/client/(api/v1|r0|v3|unstable)/login",
324 "/_matrix/client/(r0|v3|unstable)/register",
325 "/_matrix/client/v1/register/m.login.registration_token/validity",
326 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact",
327 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send",
328 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/",
329 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)",
330 "/_matrix/client/(api/v1|r0|v3|unstable)/join/",
Serge Bazanskif2628682023-03-26 21:56:09 +0200331 "/_matrix/client/(api/v1|r0|v3|unstable)/profile/"
Piotr Dobrowolski7d0e56c2022-04-30 00:25:39 +0200332
333 # These need to be handled by stream writers, not supported yet
334 # "/_matrix/client/(r0|v3|unstable)/sendToDevice/",
335 # "/_matrix/client/(r0|v3|unstable)/.*/tags",
336 # "/_matrix/client/(r0|v3|unstable)/.*/account_data",
337 # "/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt",
338 # "/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers",
339 # "/_matrix/client/(api/v1|r0|v3|unstable)/presence/",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100340 ],
341 },
342
343 # Synapse media worker. This handles access to uploads and media stored in app.dataVolume
344 mediaWorker: {
345 deployment: app.SynapseWorker("synapse-media", "synapse.app.media_repository", kube.StatefulSet) {
346 cfg+: {
347 mountData: true,
348 localConfig+: {
349 worker_listeners: [{
350 type: "http",
351 port: 8008,
352 x_forwarded: true,
353 bind_addresses: ["::"],
354 resources: [{ names: ["media"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100355 }, {
356 port: 9092,
357 type: "metrics",
358 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100359 }],
360 },
361 },
362 },
363 svc: app.ns.Contain(kube.Service("synapse-media")) {
364 target_pod:: app.mediaWorker.deployment.spec.template,
365 },
366 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100367
368 appserviceWorker: if cfg.appserviceWorker then {
369 # Worker responsible for sending traffic to registered appservices
370 deployment: app.SynapseWorker("synapse-appservice", "synapse.app.appservice", kube.StatefulSet) {
371 cfg+: {
372 localConfig+: {
373 worker_listeners: [{
374 type: "http",
375 port: 8008,
376 x_forwarded: true,
377 bind_addresses: ["::"],
378 resources: [{ names: [] }],
379 }, {
380 port: 9092,
381 type: "metrics",
382 bind_address: "0.0.0.0",
383 }],
384 },
385 },
386 },
387 } else null,
388
389 federationSenderWorker: if cfg.federationWorker then {
390 deployment: app.SynapseWorker("synapse-federation-sender", "synapse.app.federation_sender", kube.StatefulSet) {
391 cfg+: {
392 localConfig+: {
393 worker_listeners: [{
394 type: "http",
395 port: 8008,
396 x_forwarded: true,
397 bind_addresses: ["::"],
398 resources: [{ names: [] }],
399 }, {
400 port: 9092,
401 type: "metrics",
402 bind_address: "0.0.0.0",
403 }],
404 },
405 },
406 spec+: {
407 replicas: 2,
408 },
409 },
410 } else null,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100411}