blob: 0bd4b74c764db264dd3372627a59d77299adbc52 [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:: [
255 "/_matrix/client/(v2_alpha|r0)/sync",
256 "/_matrix/client/(api/v1|v2_alpha|r0)/events",
257 "/_matrix/client/(api/v1|r0)/initialSync",
258 "/_matrix/client/(api/v1|r0)/rooms/[^/]+/initialSync",
259 "/_matrix/client/(api/v1|r0|unstable)/publicRooms",
260 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/joined_members",
261 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/context/.*",
262 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/members",
263 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state",
264 "/_matrix/client/(api/v1|r0|unstable)/account/3pid",
265 "/_matrix/client/(api/v1|r0|unstable)/keys/query",
266 "/_matrix/client/(api/v1|r0|unstable)/keys/changes",
267 "/_matrix/client/versions",
268 "/_matrix/client/(api/v1|r0|unstable)/voip/turnServer",
269 "/_matrix/client/(api/v1|r0|unstable)/joined_groups",
270 "/_matrix/client/(api/v1|r0|unstable)/publicised_groups",
271 "/_matrix/client/(api/v1|r0|unstable)/publicised_groups/",
272 # Blocked by https://github.com/matrix-org/synapse/issues/8966
273 # "/_matrix/client/(api/v1|r0|unstable)/login",
274 # "/_matrix/client/(r0|unstable)/register",
275 # "/_matrix/client/(r0|unstable)/auth/.*/fallback/web",
276 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/send",
277 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state/",
278 "/_matrix/client/(api/v1|r0|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)",
279 "/_matrix/client/(api/v1|r0|unstable)/join/",
280 "/_matrix/client/(api/v1|r0|unstable)/profile/",
281 "/_matrix/federation/v1/event/",
282 "/_matrix/federation/v1/state/",
283 "/_matrix/federation/v1/state_ids/",
284 "/_matrix/federation/v1/backfill/",
285 "/_matrix/federation/v1/get_missing_events/",
286 "/_matrix/federation/v1/publicRooms",
287 "/_matrix/federation/v1/query/",
288 "/_matrix/federation/v1/make_join/",
289 "/_matrix/federation/v1/make_leave/",
290 "/_matrix/federation/v1/send_join/",
291 "/_matrix/federation/v2/send_join/",
292 "/_matrix/federation/v1/send_leave/",
293 "/_matrix/federation/v2/send_leave/",
294 "/_matrix/federation/v1/invite/",
295 "/_matrix/federation/v2/invite/",
296 "/_matrix/federation/v1/query_auth/",
297 "/_matrix/federation/v1/event_auth/",
298 "/_matrix/federation/v1/exchange_third_party_invite/",
299 "/_matrix/federation/v1/user/devices/",
300 "/_matrix/federation/v1/get_groups_publicised",
301 "/_matrix/key/v2/query",
302 "/_matrix/federation/v1/send/",
303 ],
304 },
305
306 # Synapse media worker. This handles access to uploads and media stored in app.dataVolume
307 mediaWorker: {
308 deployment: app.SynapseWorker("synapse-media", "synapse.app.media_repository", kube.StatefulSet) {
309 cfg+: {
310 mountData: true,
311 localConfig+: {
312 worker_listeners: [{
313 type: "http",
314 port: 8008,
315 x_forwarded: true,
316 bind_addresses: ["::"],
317 resources: [{ names: ["media"]}],
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100318 }, {
319 port: 9092,
320 type: "metrics",
321 bind_address: "0.0.0.0",
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100322 }],
323 },
324 },
325 },
326 svc: app.ns.Contain(kube.Service("synapse-media")) {
327 target_pod:: app.mediaWorker.deployment.spec.template,
328 },
329 },
Piotr Dobrowolski529e1812021-02-13 19:44:37 +0100330
331 appserviceWorker: if cfg.appserviceWorker then {
332 # Worker responsible for sending traffic to registered appservices
333 deployment: app.SynapseWorker("synapse-appservice", "synapse.app.appservice", kube.StatefulSet) {
334 cfg+: {
335 localConfig+: {
336 worker_listeners: [{
337 type: "http",
338 port: 8008,
339 x_forwarded: true,
340 bind_addresses: ["::"],
341 resources: [{ names: [] }],
342 }, {
343 port: 9092,
344 type: "metrics",
345 bind_address: "0.0.0.0",
346 }],
347 },
348 },
349 },
350 } else null,
351
352 federationSenderWorker: if cfg.federationWorker then {
353 deployment: app.SynapseWorker("synapse-federation-sender", "synapse.app.federation_sender", kube.StatefulSet) {
354 cfg+: {
355 localConfig+: {
356 worker_listeners: [{
357 type: "http",
358 port: 8008,
359 x_forwarded: true,
360 bind_addresses: ["::"],
361 resources: [{ names: [] }],
362 }, {
363 port: 9092,
364 type: "metrics",
365 bind_address: "0.0.0.0",
366 }],
367 },
368 },
369 spec+: {
370 replicas: 2,
371 },
372 },
373 } else null,
Piotr Dobrowolskib67ae482021-01-31 10:35:38 +0100374}