blob: 7b4530817ad362f82c0c83417ce6af3770a57d95 [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
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200277 pathsList:: |||
278 # Sync requests
279 ^/_matrix/client/(r0|v3)/sync$
280 ^/_matrix/client/(api/v1|r0|v3)/events$
281 ^/_matrix/client/(api/v1|r0|v3)/initialSync$
282 ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$
Piotr Dobrowolski7d0e56c2022-04-30 00:25:39 +0200283
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200284 # Federation requests
285 ^/_matrix/federation/v1/event/
286 ^/_matrix/federation/v1/state/
287 ^/_matrix/federation/v1/state_ids/
288 ^/_matrix/federation/v1/backfill/
289 ^/_matrix/federation/v1/get_missing_events/
290 ^/_matrix/federation/v1/publicRooms
291 ^/_matrix/federation/v1/query/
292 ^/_matrix/federation/v1/make_join/
293 ^/_matrix/federation/v1/make_leave/
294 ^/_matrix/federation/(v1|v2)/send_join/
295 ^/_matrix/federation/(v1|v2)/send_leave/
296 ^/_matrix/federation/(v1|v2)/invite/
297 ^/_matrix/federation/v1/event_auth/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200298 ^/_matrix/federation/v1/timestamp_to_event/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200299 ^/_matrix/federation/v1/exchange_third_party_invite/
300 ^/_matrix/federation/v1/user/devices/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200301 ^/_matrix/key/v2/query
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200302 ^/_matrix/federation/v1/hierarchy/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200303
304 # Inbound federation transaction request
305 ^/_matrix/federation/v1/send/
306
307 # Client API requests
308 ^/_matrix/client/(api/v1|r0|v3|unstable)/createRoom$
309 ^/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms$
310 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members$
311 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*$
312 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members$
313 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200314 ^/_matrix/client/v1/rooms/.*/hierarchy$
315 ^/_matrix/client/(v1|unstable)/rooms/.*/relations/
316 ^/_matrix/client/v1/rooms/.*/threads$
317 ^/_matrix/client/unstable/org.matrix.msc2716/rooms/.*/batch_send$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200318 ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$
319 ^/_matrix/client/(r0|v3|unstable)/account/3pid$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200320 ^/_matrix/client/(r0|v3|unstable)/account/whoami$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200321 ^/_matrix/client/(r0|v3|unstable)/devices$
322 ^/_matrix/client/versions$
323 ^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200324 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/
325 ^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200326 ^/_matrix/client/v1/rooms/.*/timestamp_to_event$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200327 ^/_matrix/client/(api/v1|r0|v3|unstable)/search$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200328 ^/_matrix/client/(r0|v3|unstable)/user/.*/filter(/|$)
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200329
330 # Encryption requests
331 ^/_matrix/client/(r0|v3|unstable)/keys/query$
332 ^/_matrix/client/(r0|v3|unstable)/keys/changes$
333 ^/_matrix/client/(r0|v3|unstable)/keys/claim$
334 ^/_matrix/client/(r0|v3|unstable)/room_keys/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200335 ^/_matrix/client/(r0|v3|unstable)/keys/upload/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200336
337 # Registration/login requests
338 ^/_matrix/client/(api/v1|r0|v3|unstable)/login$
339 ^/_matrix/client/(r0|v3|unstable)/register$
340 ^/_matrix/client/v1/register/m.login.registration_token/validity$
341
342 # Event sending requests
343 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact
344 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send
345 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/
346 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$
347 ^/_matrix/client/(api/v1|r0|v3|unstable)/join/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200348 ^/_matrix/client/(api/v1|r0|v3|unstable)/knock/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200349 ^/_matrix/client/(api/v1|r0|v3|unstable)/profile/
350
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200351 # Account data requests
352 #^/_matrix/client/(r0|v3|unstable)/.*/tags
353 #^/_matrix/client/(r0|v3|unstable)/.*/account_data
354
355 # Receipts requests
356 #^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt
357 #^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers
358
359 # Presence requests
360 #^/_matrix/client/(api/v1|r0|v3|unstable)/presence/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200361
362 # User directory search requests
363 #^/_matrix/client/(r0|v3|unstable)/user_directory/search$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200364 |||,
365
366 paths:: std.filterMap(
367 # Ignore comments and empty lines
368 function(v) !std.startsWith(v, '#') && std.length(v) > 1,
369 # Strip leading ^
370 function(v) std.substr(v, 1, std.length(v) - 1),
371 std.split(self.pathsList, "\n")
372 ),
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100373 },
374
375 # Synapse media worker. This handles access to uploads and media stored in app.dataVolume
376 mediaWorker: {
377 deployment: app.SynapseWorker("synapse-media", "synapse.app.media_repository", kube.StatefulSet) {
378 cfg+: {
379 mountData: true,
380 localConfig+: {
381 worker_listeners: [{
382 type: "http",
383 port: 8008,
384 x_forwarded: true,
385 bind_addresses: ["::"],
386 resources: [{ names: ["media"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100387 }, {
388 port: 9092,
389 type: "metrics",
390 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100391 }],
392 },
393 },
394 },
395 svc: app.ns.Contain(kube.Service("synapse-media")) {
396 target_pod:: app.mediaWorker.deployment.spec.template,
397 },
398 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100399
400 appserviceWorker: if cfg.appserviceWorker then {
401 # Worker responsible for sending traffic to registered appservices
402 deployment: app.SynapseWorker("synapse-appservice", "synapse.app.appservice", kube.StatefulSet) {
403 cfg+: {
404 localConfig+: {
405 worker_listeners: [{
406 type: "http",
407 port: 8008,
408 x_forwarded: true,
409 bind_addresses: ["::"],
410 resources: [{ names: [] }],
411 }, {
412 port: 9092,
413 type: "metrics",
414 bind_address: "0.0.0.0",
415 }],
416 },
417 },
418 },
419 } else null,
420
421 federationSenderWorker: if cfg.federationWorker then {
422 deployment: app.SynapseWorker("synapse-federation-sender", "synapse.app.federation_sender", kube.StatefulSet) {
423 cfg+: {
424 localConfig+: {
425 worker_listeners: [{
426 type: "http",
427 port: 8008,
428 x_forwarded: true,
429 bind_addresses: ["::"],
430 resources: [{ names: [] }],
431 }, {
432 port: 9092,
433 type: "metrics",
434 bind_address: "0.0.0.0",
435 }],
436 },
437 },
438 spec+: {
439 replicas: 2,
440 },
441 },
442 } else null,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100443}