blob: 71d03d72c19ebc22709d37b889dc1b2522c65222 [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 },
72 } 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 },
90 } else {}),
91
92 # Synapse process Deployment/StatefulSet base resource.
93 SynapseWorker(name, workerType, builder):: app.ns.Contain(builder(name)) {
94 local worker = self,
95 cfg:: {
96 # Configuration customization. Can contain environment substitution
97 # syntax, as used in worker_name value.
98 localConfig: {
99 worker_app: workerType,
100 worker_name: "$(POD_NAME)",
101
102 # The replication listener on the main synapse process.
103 worker_replication_host: "synapse-replication-master",
104 worker_replication_http_port: 9093,
105 },
106
107 # Mount app.dataVolume in /data
108 mountData: false,
109 },
110
111 spec+: {
112 replicas: 1,
113 template+: {
114 spec+: {
115 volumes_: {
116 config: kube.ConfigMapVolume(app.configMap),
117 secrets: { secret: { secretName: "synapse" } },
118 } + {
119 [k]: { secret: { secretName: "appservice-%s-registration" % [k] } }
120 for k in std.objectFields(app.appservices)
121 } + if worker.cfg.mountData then {
122 data: kube.PersistentVolumeClaimVolume(app.dataVolume),
123 } else {},
124 containers_: {
125 web: kube.Container("synapse") {
126 image: cfg.image,
127 command: [
128 "/bin/sh", "-c", |||
129 set -e
130 echo "${X_SECRETS_CONFIG}" > /tmp/secrets.yaml
131 echo "${X_LOCAL_CONFIG}" > /tmp/local.yaml
132 exec python -m ${SYNAPSE_WORKER} --config-path /conf/homeserver.yaml --config-path /tmp/secrets.yaml --config-path /tmp/local.yaml
133 |||
134 ],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100135 resources: {
136 requests: { cpu: "300m", memory: "1Gi" },
137 limits: { cpu: "1500m", memory: "2Gi" },
138 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100139 ports_: {
140 http: { containerPort: 8008 },
141 metrics: { containerPort: 9092 },
142 replication: { containerPort: 9093 },
143 },
144 env_: {
145 SYNAPSE_WORKER: workerType,
146
147 SYNAPSE_MACAROON_SECRET_KEY: cfg.macaroonSecretKey,
148 SYNAPSE_REGISTRATION_SHARED_SECRET: cfg.registrationSharedSecret,
149 WORKER_REPLICATION_SECRET: cfg.workerReplicationSecret,
150 POSTGRES_PASSWORD: app.postgres.cfg.password,
151 REDIS_PASSWORD: app.redis.cfg.password,
152 POD_NAME: { fieldRef: { fieldPath: "metadata.name" } },
153 OIDC_CLIENT_SECRET: if cfg.oidc.enable then cfg.oidc.config.client_secret else "",
154
155 X_SECRETS_CONFIG: std.manifestYamlDoc(app.secretsConfig),
156 X_LOCAL_CONFIG: std.manifestYamlDoc(worker.cfg.localConfig),
157 },
158 volumeMounts_: {
159 config: { mountPath: "/conf", },
160 secrets: { mountPath: "/secrets" },
161 } + {
162 [k]: { mountPath: "/appservices/%s" % [k] }
163 for k in std.objectFields(app.appservices)
164 } + if worker.cfg.mountData then {
165 data: { mountPath: "/data" },
166 } else {},
Piotr Dobrowolski77af94d2021-09-16 22:17:58 +0200167 readinessProbe: {
168 httpGet: {
169 path: "/health",
170 port: "http",
171 },
172 initialDelaySeconds: 5,
173 periodSeconds: 10,
174 },
175 livenessProbe: {
176 httpGet: {
177 path: "/health",
178 port: "http",
179 },
180 initialDelaySeconds: 60,
181 periodSeconds: 30,
182 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100183 },
184 },
185 securityContext: {
186 runAsUser: 991,
187 runAsGroup: 991,
188 fsGroup: 991,
189 },
190 },
191 },
192 },
193 },
194
195 # Synapse main process
196 main: {
197 deployment: app.SynapseWorker("synapse", "synapse.app.homeserver", kube.Deployment) {
198 cfg+: {
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100199 localConfig: {
200 # Following configuration values need to cause master
201 # process restart.
202 notify_appservices: app.config.notify_appservices,
203 # send_federation: app.config.send_federation,
204 # federation_sender_instances: app.config.federation_sender_instances,
205 }
206 },
207 spec+: {
208 strategy+: {
209 rollingUpdate: {
210 maxSurge: 0,
211 maxUnavailable: 1,
212 },
213 },
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100214 },
215 },
216 svc: app.ns.Contain(kube.Service("synapse")) {
217 target_pod:: app.main.deployment.spec.template,
218 },
219 replicationSvc: app.ns.Contain(kube.Service("synapse-replication-master")) {
220 target_pod:: app.main.deployment.spec.template,
221 spec+: {
222 ports: [
223 { port: 9093, name: 'replication', targetPort: 9093 },
224 ],
225 },
226 },
227 },
228
229 genericWorker: {
230 # Synapse generic worker deployment
231 deployment: app.SynapseWorker("synapse-generic", "synapse.app.generic_worker", kube.StatefulSet) {
232 cfg+: {
233 localConfig+: {
234 worker_listeners: [{
235 type: "http",
236 port: 8008,
237 x_forwarded: true,
238 bind_addresses: ["::"],
239 resources: [{ names: ["client", "federation"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100240 }, {
241 port: 9092,
242 type: "metrics",
243 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100244 }],
245 },
246 },
247 },
248 svc: app.ns.Contain(kube.Service("synapse-generic")) {
249 target_pod:: app.genericWorker.deployment.spec.template,
250 },
251
252 # Following paths can be handled by generic workers.
253 # See: https://github.com/matrix-org/synapse/blob/master/docs/workers.md
254 paths:: [
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200255 "/_matrix/client/(r0|v3)/sync",
256 "/_matrix/client/(api/v1|r0|v3)/events",
257 "/_matrix/client/(api/v1|r0|v3)/initialSync",
258 "/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100259 "/_matrix/federation/v1/event/",
260 "/_matrix/federation/v1/state/",
261 "/_matrix/federation/v1/state_ids/",
262 "/_matrix/federation/v1/backfill/",
263 "/_matrix/federation/v1/get_missing_events/",
264 "/_matrix/federation/v1/publicRooms",
265 "/_matrix/federation/v1/query/",
266 "/_matrix/federation/v1/make_join/",
267 "/_matrix/federation/v1/make_leave/",
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200268 "/_matrix/federation/(v1|v2)/send_join/",
269 "/_matrix/federation/(v1|v2)/send_leave/",
270 "/_matrix/federation/(v1|v2)/invite/",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100271 "/_matrix/federation/v1/event_auth/",
272 "/_matrix/federation/v1/exchange_third_party_invite/",
273 "/_matrix/federation/v1/user/devices/",
274 "/_matrix/federation/v1/get_groups_publicised",
275 "/_matrix/key/v2/query",
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200276 "/_matrix/federation/(v1|unstable/org.matrix.msc2946)/hierarchy/",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100277 "/_matrix/federation/v1/send/",
Mateusz Lenik8bd24f42022-04-17 19:56:06 +0200278 "/_matrix/client/(api/v1|r0|v3|unstable)/createRoom",
279 "/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms",
280 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members",
281 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*",
282 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members",
283 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state",
284 "/_matrix/client/(v1|unstable/org.matrix.msc2946)/rooms/.*/hierarchy",
285 "/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary",
286 "/_matrix/client/(r0|v3|unstable)/account/3pid",
287 "/_matrix/client/(r0|v3|unstable)/devices",
288 "/_matrix/client/versions",
289 "/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer",
290 "/_matrix/client/(r0|v3|unstable)/joined_groups",
291 "/_matrix/client/(r0|v3|unstable)/publicised_groups",
292 "/_matrix/client/(r0|v3|unstable)/publicised_groups/",
293 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/",
294 "/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms",
295 "/_matrix/client/(api/v1|r0|v3|unstable)/search",
296 "/_matrix/client/(r0|v3|unstable)/keys/query",
297 "/_matrix/client/(r0|v3|unstable)/keys/changes",
298 "/_matrix/client/(r0|v3|unstable)/keys/claim",
299 "/_matrix/client/(r0|v3|unstable)/room_keys/",
300 "/_matrix/client/(api/v1|r0|v3|unstable)/login",
301 "/_matrix/client/(r0|v3|unstable)/register",
302 "/_matrix/client/v1/register/m.login.registration_token/validity",
303 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact",
304 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send",
305 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/",
306 "/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)",
307 "/_matrix/client/(api/v1|r0|v3|unstable)/join/",
308 "/_matrix/client/(api/v1|r0|v3|unstable)/profile/",
Piotr Dobrowolski7d0e56c2022-04-30 00:25:39 +0200309
310 # These need to be handled by stream writers, not supported yet
311 # "/_matrix/client/(r0|v3|unstable)/sendToDevice/",
312 # "/_matrix/client/(r0|v3|unstable)/.*/tags",
313 # "/_matrix/client/(r0|v3|unstable)/.*/account_data",
314 # "/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt",
315 # "/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers",
316 # "/_matrix/client/(api/v1|r0|v3|unstable)/presence/",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100317 ],
318 },
319
320 # Synapse media worker. This handles access to uploads and media stored in app.dataVolume
321 mediaWorker: {
322 deployment: app.SynapseWorker("synapse-media", "synapse.app.media_repository", kube.StatefulSet) {
323 cfg+: {
324 mountData: true,
325 localConfig+: {
326 worker_listeners: [{
327 type: "http",
328 port: 8008,
329 x_forwarded: true,
330 bind_addresses: ["::"],
331 resources: [{ names: ["media"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100332 }, {
333 port: 9092,
334 type: "metrics",
335 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100336 }],
337 },
338 },
339 },
340 svc: app.ns.Contain(kube.Service("synapse-media")) {
341 target_pod:: app.mediaWorker.deployment.spec.template,
342 },
343 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100344
345 appserviceWorker: if cfg.appserviceWorker then {
346 # Worker responsible for sending traffic to registered appservices
347 deployment: app.SynapseWorker("synapse-appservice", "synapse.app.appservice", kube.StatefulSet) {
348 cfg+: {
349 localConfig+: {
350 worker_listeners: [{
351 type: "http",
352 port: 8008,
353 x_forwarded: true,
354 bind_addresses: ["::"],
355 resources: [{ names: [] }],
356 }, {
357 port: 9092,
358 type: "metrics",
359 bind_address: "0.0.0.0",
360 }],
361 },
362 },
363 },
364 } else null,
365
366 federationSenderWorker: if cfg.federationWorker then {
367 deployment: app.SynapseWorker("synapse-federation-sender", "synapse.app.federation_sender", kube.StatefulSet) {
368 cfg+: {
369 localConfig+: {
370 worker_listeners: [{
371 type: "http",
372 port: 8008,
373 x_forwarded: true,
374 bind_addresses: ["::"],
375 resources: [{ names: [] }],
376 }, {
377 port: 9092,
378 type: "metrics",
379 bind_address: "0.0.0.0",
380 }],
381 },
382 },
383 spec+: {
384 replicas: 2,
385 },
386 },
387 } else null,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100388}