blob: 07656da30d75ba38473254cfe877a83b21f73769 [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")) {
radex36964dc2023-11-24 11:19:46 +010034 storage:: "50Gi",
35 storageClass:: cfg.storageClassName,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010036 },
37
38 // homeserver.yaml that will be used to run synapse (in configMap).
39 // This is based off of //app/matrix/lib/synapse/homeserver.yaml with some fields overriden per
40 // deployment.
41 config:: (std.native("parseYaml"))(importstr "synapse/homeserver-ng.yaml")[0] {
42 server_name: cfg.serverName,
43 public_baseurl: "https://%s" % [cfg.webDomain],
44 signing_key_path: "/secrets/homeserver_signing_key",
45 app_service_config_files: [
46 "/appservices/%s/registration.yaml" % [k]
47 for k in std.objectFields(app.appservices)
48 ],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +010049
50 notify_appservices: cfg.appserviceWorker == false,
51
52 # FIXME(informatic) Rolling out with federationWorkers = true breaks
53 # *some* federation, needs investigation...
54 #send_federation: cfg.federationWorker == false,
55 #federation_sender_instances: if cfg.federationWorker then [
56 # "%s-%s" % [app.federationSenderWorker.deployment.metadata.name, idx]
57 # for idx in std.range(0, app.federationSenderWorker.deployment.spec.replicas)
58 #] else [],
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010059 } + (if cfg.cas.enable then {
60 cas_config: {
61 enabled: true,
62 server_url: "https://%s/_cas" % [cfg.webDomain],
63 service_url: "https://%s" % [cfg.webDomain],
64 },
Piotr Dobrowolski690ed452022-05-07 11:27:24 +020065 } else {}) + (if cfg.coturn.enable then {
66 turn_uris: [ "turn:%s?transport=udp" % cfg.coturn.config.domain, "turn:%s?transport=tcp" % cfg.coturn.config.domain ],
67
68 # Lifetime of single TURN user credentials - 1 day, recommended by TURN REST
69 # spec, see https://datatracker.ietf.org/doc/html/draft-uberti-behave-turn-rest-00#section-2.2
70 turn_user_lifetime: 24 * 60 * 60 * 1000,
71 turn_allow_guests: true,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010072 } else {}),
73
74 configMap: app.ns.Contain(kube.ConfigMap("synapse")) {
75 data: {
76 "homeserver.yaml": std.manifestYamlDoc(app.config),
77 "log.config": importstr "synapse/log.config",
78 },
79 },
80
81 // homeserver-secrets.yaml contains all the templated secret variables from
82 // base homeserver.yaml passed as yaml-encoded environment variable.
83 // $(ENVVAR)-encoded variables are resolved by Kubernetes on pod startup
84 secretsConfig:: (std.native("parseYaml"))(importstr "synapse/homeserver-secrets.yaml")[0] {
85 } + (if cfg.oidc.enable then {
86 oidc_config: cfg.oidc.config {
87 enabled: true,
88 client_secret: "$(OIDC_CLIENT_SECRET)",
89 },
Piotr Dobrowolski690ed452022-05-07 11:27:24 +020090 } else {}) + (if cfg.coturn.enable then {
91 turn_shared_secret: "$(TURN_SHARED_SECRET)",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +010092 } else {}),
93
94 # Synapse process Deployment/StatefulSet base resource.
95 SynapseWorker(name, workerType, builder):: app.ns.Contain(builder(name)) {
96 local worker = self,
97 cfg:: {
98 # Configuration customization. Can contain environment substitution
99 # syntax, as used in worker_name value.
100 localConfig: {
101 worker_app: workerType,
102 worker_name: "$(POD_NAME)",
103
104 # The replication listener on the main synapse process.
105 worker_replication_host: "synapse-replication-master",
106 worker_replication_http_port: 9093,
107 },
108
109 # Mount app.dataVolume in /data
110 mountData: false,
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200111
112 resources: {
Serge Bazanskif2628682023-03-26 21:56:09 +0200113 requests: { cpu: "300m", memory: "2Gi" },
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200114 limits: { cpu: "1500m", memory: "2Gi" },
115 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100116 },
117
118 spec+: {
119 replicas: 1,
120 template+: {
121 spec+: {
122 volumes_: {
123 config: kube.ConfigMapVolume(app.configMap),
124 secrets: { secret: { secretName: "synapse" } },
125 } + {
126 [k]: { secret: { secretName: "appservice-%s-registration" % [k] } }
127 for k in std.objectFields(app.appservices)
128 } + if worker.cfg.mountData then {
129 data: kube.PersistentVolumeClaimVolume(app.dataVolume),
130 } else {},
131 containers_: {
132 web: kube.Container("synapse") {
133 image: cfg.image,
134 command: [
135 "/bin/sh", "-c", |||
136 set -e
137 echo "${X_SECRETS_CONFIG}" > /tmp/secrets.yaml
138 echo "${X_LOCAL_CONFIG}" > /tmp/local.yaml
139 exec python -m ${SYNAPSE_WORKER} --config-path /conf/homeserver.yaml --config-path /tmp/secrets.yaml --config-path /tmp/local.yaml
140 |||
141 ],
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200142 resources: worker.cfg.resources,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100143 ports_: {
144 http: { containerPort: 8008 },
145 metrics: { containerPort: 9092 },
146 replication: { containerPort: 9093 },
147 },
148 env_: {
149 SYNAPSE_WORKER: workerType,
150
151 SYNAPSE_MACAROON_SECRET_KEY: cfg.macaroonSecretKey,
152 SYNAPSE_REGISTRATION_SHARED_SECRET: cfg.registrationSharedSecret,
153 WORKER_REPLICATION_SECRET: cfg.workerReplicationSecret,
Serge Bazanskif2628682023-03-26 21:56:09 +0200154
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100155 POSTGRES_PASSWORD: app.postgres.cfg.password,
Serge Bazanskif2628682023-03-26 21:56:09 +0200156 POSTGRES_USER: app.postgres.cfg.username,
157 POSTGRES_DB: app.postgres.cfg.database,
158 POSTGRES_HOST: app.postgres.cfg.host,
159 POSTGRES_PORT: app.postgres.cfg.port,
160
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100161 REDIS_PASSWORD: app.redis.cfg.password,
162 POD_NAME: { fieldRef: { fieldPath: "metadata.name" } },
163 OIDC_CLIENT_SECRET: if cfg.oidc.enable then cfg.oidc.config.client_secret else "",
Piotr Dobrowolski690ed452022-05-07 11:27:24 +0200164 TURN_SHARED_SECRET: if cfg.coturn.enable then cfg.coturn.config.authSecret else "",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100165
166 X_SECRETS_CONFIG: std.manifestYamlDoc(app.secretsConfig),
167 X_LOCAL_CONFIG: std.manifestYamlDoc(worker.cfg.localConfig),
168 },
169 volumeMounts_: {
170 config: { mountPath: "/conf", },
171 secrets: { mountPath: "/secrets" },
172 } + {
173 [k]: { mountPath: "/appservices/%s" % [k] }
174 for k in std.objectFields(app.appservices)
175 } + if worker.cfg.mountData then {
176 data: { mountPath: "/data" },
177 } else {},
Piotr Dobrowolski77af94d2021-09-16 22:17:58 +0200178 readinessProbe: {
179 httpGet: {
180 path: "/health",
181 port: "http",
182 },
183 initialDelaySeconds: 5,
184 periodSeconds: 10,
185 },
186 livenessProbe: {
187 httpGet: {
188 path: "/health",
189 port: "http",
190 },
191 initialDelaySeconds: 60,
192 periodSeconds: 30,
193 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100194 },
195 },
196 securityContext: {
197 runAsUser: 991,
198 runAsGroup: 991,
199 fsGroup: 991,
200 },
201 },
202 },
203 },
204 },
205
206 # Synapse main process
207 main: {
208 deployment: app.SynapseWorker("synapse", "synapse.app.homeserver", kube.Deployment) {
209 cfg+: {
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100210 localConfig: {
211 # Following configuration values need to cause master
212 # process restart.
213 notify_appservices: app.config.notify_appservices,
214 # send_federation: app.config.send_federation,
215 # federation_sender_instances: app.config.federation_sender_instances,
Piotr Dobrowolskia8bb6152022-09-13 20:37:33 +0200216 },
217
218 resources+: {
219 limits+: { memory: "4Gi" },
220 requests+: { memory: "2Gi" },
221 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100222 },
223 spec+: {
224 strategy+: {
225 rollingUpdate: {
226 maxSurge: 0,
227 maxUnavailable: 1,
228 },
229 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100230 },
231 },
232 svc: app.ns.Contain(kube.Service("synapse")) {
radex8b8f3872023-11-24 11:09:46 +0100233 target:: app.main.deployment,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100234 },
235 replicationSvc: app.ns.Contain(kube.Service("synapse-replication-master")) {
radex8b8f3872023-11-24 11:09:46 +0100236 target:: app.main.deployment,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100237 spec+: {
238 ports: [
239 { port: 9093, name: 'replication', targetPort: 9093 },
240 ],
241 },
242 },
243 },
244
245 genericWorker: {
246 # Synapse generic worker deployment
247 deployment: app.SynapseWorker("synapse-generic", "synapse.app.generic_worker", kube.StatefulSet) {
248 cfg+: {
249 localConfig+: {
250 worker_listeners: [{
251 type: "http",
252 port: 8008,
253 x_forwarded: true,
254 bind_addresses: ["::"],
255 resources: [{ names: ["client", "federation"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100256 }, {
257 port: 9092,
258 type: "metrics",
259 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100260 }],
261 },
262 },
263 },
264 svc: app.ns.Contain(kube.Service("synapse-generic")) {
radex8b8f3872023-11-24 11:09:46 +0100265 target:: app.genericWorker.deployment,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100266 },
267
268 # Following paths can be handled by generic workers.
269 # See: https://github.com/matrix-org/synapse/blob/master/docs/workers.md
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200270 pathsList:: |||
271 # Sync requests
272 ^/_matrix/client/(r0|v3)/sync$
273 ^/_matrix/client/(api/v1|r0|v3)/events$
274 ^/_matrix/client/(api/v1|r0|v3)/initialSync$
275 ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$
Piotr Dobrowolski7d0e56c2022-04-30 00:25:39 +0200276
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200277 # Federation requests
278 ^/_matrix/federation/v1/event/
279 ^/_matrix/federation/v1/state/
280 ^/_matrix/federation/v1/state_ids/
281 ^/_matrix/federation/v1/backfill/
282 ^/_matrix/federation/v1/get_missing_events/
283 ^/_matrix/federation/v1/publicRooms
284 ^/_matrix/federation/v1/query/
285 ^/_matrix/federation/v1/make_join/
286 ^/_matrix/federation/v1/make_leave/
287 ^/_matrix/federation/(v1|v2)/send_join/
288 ^/_matrix/federation/(v1|v2)/send_leave/
289 ^/_matrix/federation/(v1|v2)/invite/
290 ^/_matrix/federation/v1/event_auth/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200291 ^/_matrix/federation/v1/timestamp_to_event/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200292 ^/_matrix/federation/v1/exchange_third_party_invite/
293 ^/_matrix/federation/v1/user/devices/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200294 ^/_matrix/key/v2/query
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200295 ^/_matrix/federation/v1/hierarchy/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200296
297 # Inbound federation transaction request
298 ^/_matrix/federation/v1/send/
299
300 # Client API requests
301 ^/_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$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200307 ^/_matrix/client/v1/rooms/.*/hierarchy$
308 ^/_matrix/client/(v1|unstable)/rooms/.*/relations/
309 ^/_matrix/client/v1/rooms/.*/threads$
310 ^/_matrix/client/unstable/org.matrix.msc2716/rooms/.*/batch_send$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200311 ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$
312 ^/_matrix/client/(r0|v3|unstable)/account/3pid$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200313 ^/_matrix/client/(r0|v3|unstable)/account/whoami$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200314 ^/_matrix/client/(r0|v3|unstable)/devices$
315 ^/_matrix/client/versions$
316 ^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200317 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/
318 ^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200319 ^/_matrix/client/v1/rooms/.*/timestamp_to_event$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200320 ^/_matrix/client/(api/v1|r0|v3|unstable)/search$
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200321 ^/_matrix/client/(r0|v3|unstable)/user/.*/filter(/|$)
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200322
323 # Encryption requests
324 ^/_matrix/client/(r0|v3|unstable)/keys/query$
325 ^/_matrix/client/(r0|v3|unstable)/keys/changes$
326 ^/_matrix/client/(r0|v3|unstable)/keys/claim$
327 ^/_matrix/client/(r0|v3|unstable)/room_keys/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200328 ^/_matrix/client/(r0|v3|unstable)/keys/upload/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200329
330 # Registration/login requests
331 ^/_matrix/client/(api/v1|r0|v3|unstable)/login$
332 ^/_matrix/client/(r0|v3|unstable)/register$
333 ^/_matrix/client/v1/register/m.login.registration_token/validity$
334
335 # Event sending requests
336 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact
337 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send
338 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/
339 ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$
340 ^/_matrix/client/(api/v1|r0|v3|unstable)/join/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200341 ^/_matrix/client/(api/v1|r0|v3|unstable)/knock/
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200342 ^/_matrix/client/(api/v1|r0|v3|unstable)/profile/
343
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200344 # Account data requests
345 #^/_matrix/client/(r0|v3|unstable)/.*/tags
346 #^/_matrix/client/(r0|v3|unstable)/.*/account_data
347
348 # Receipts requests
349 #^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt
350 #^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers
351
352 # Presence requests
353 #^/_matrix/client/(api/v1|r0|v3|unstable)/presence/
Piotr Dobrowolski926252c2023-03-28 23:58:20 +0200354
355 # User directory search requests
356 #^/_matrix/client/(r0|v3|unstable)/user_directory/search$
Piotr Dobrowolski6bd5d202023-03-28 23:44:14 +0200357 |||,
358
359 paths:: std.filterMap(
360 # Ignore comments and empty lines
361 function(v) !std.startsWith(v, '#') && std.length(v) > 1,
362 # Strip leading ^
363 function(v) std.substr(v, 1, std.length(v) - 1),
364 std.split(self.pathsList, "\n")
365 ),
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100366 },
367
368 # Synapse media worker. This handles access to uploads and media stored in app.dataVolume
369 mediaWorker: {
370 deployment: app.SynapseWorker("synapse-media", "synapse.app.media_repository", kube.StatefulSet) {
371 cfg+: {
372 mountData: true,
373 localConfig+: {
374 worker_listeners: [{
375 type: "http",
376 port: 8008,
377 x_forwarded: true,
378 bind_addresses: ["::"],
379 resources: [{ names: ["media"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100380 }, {
381 port: 9092,
382 type: "metrics",
383 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100384 }],
385 },
386 },
387 },
388 svc: app.ns.Contain(kube.Service("synapse-media")) {
radex8b8f3872023-11-24 11:09:46 +0100389 target:: app.mediaWorker.deployment,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100390 },
391 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100392
393 appserviceWorker: if cfg.appserviceWorker then {
394 # Worker responsible for sending traffic to registered appservices
395 deployment: app.SynapseWorker("synapse-appservice", "synapse.app.appservice", kube.StatefulSet) {
396 cfg+: {
397 localConfig+: {
398 worker_listeners: [{
399 type: "http",
400 port: 8008,
401 x_forwarded: true,
402 bind_addresses: ["::"],
403 resources: [{ names: [] }],
404 }, {
405 port: 9092,
406 type: "metrics",
407 bind_address: "0.0.0.0",
408 }],
409 },
410 },
411 },
412 } else null,
413
414 federationSenderWorker: if cfg.federationWorker then {
415 deployment: app.SynapseWorker("synapse-federation-sender", "synapse.app.federation_sender", kube.StatefulSet) {
416 cfg+: {
417 localConfig+: {
418 worker_listeners: [{
419 type: "http",
420 port: 8008,
421 x_forwarded: true,
422 bind_addresses: ["::"],
423 resources: [{ names: [] }],
424 }, {
425 port: 9092,
426 type: "metrics",
427 bind_address: "0.0.0.0",
428 }],
429 },
430 },
431 spec+: {
432 replicas: 2,
433 },
434 },
435 } else null,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100436}