blob: 7b10f6f2a1b2ed45066e98d091d47060451b7457 [file] [log] [blame]
Serge Bazanski60076c72020-11-03 19:17:25 +01001# Matrix server (synapse).
Piotr Dobrowolskia2226912019-05-14 18:49:29 +02002# This needs a secret provisioned, create with:
Serge Bazanski60076c72020-11-03 19:17:25 +01003# ns=matrix
4# kubectl -n $ns create secret generic synapse --from-literal=postgres_password=$(pwgen 24 1) --from-literal=macaroon_secret_key=$(pwgen 32 1) --from-literal=registration_shared_secret=$(pwgen 32 1)
5# kubectl -n $ns create secret generic oauth2-cas-proxy --from-literal=oauth2_secret=...
Serge Bazanskide627512020-08-24 21:17:55 +00006#
Serge Bazanski21a96162020-11-03 23:34:36 +01007# After starting, re-create the postgres database (because docker/postgres won't let you set ENCODING):
8# postgres=# drop database synapse;
9# DROP DATABASE
10# postgres=# CREATE DATABASE synapse
11# postgres-# ENCODING 'UTF8'
12# postgres-# LC_COLLATE='C'
13# postgres-# LC_CTYPE='C'
14# postgres-# template=template0
15# postgres-# OWNER synapse;
16# CREATE DATABASE
17#
Serge Bazanskide627512020-08-24 21:17:55 +000018# Sequencing appservices is fun. The appservice needs to run first (for
19# instance, via a bootstrap job), and on startup it will spit out a
20# registration file. This registration file then needs to be fed to synapse -
21# this is done via specialy named secrets (appservice-X-registration, for X key
22# in the appservices object).
23#
24# For appservice-irc instances, you can use this oneliner magic to get the
25# registration YAML from logs.
Piotr Dobrowolski3ea979d2019-05-23 16:11:52 +020026# kubectl -n matrix create secret generic appservice-irc-freenode-registration --from-file=registration.yaml=<(kubectl logs -n matrix $(kubectl get pods -n matrix --selector=job-name=appservice-irc-freenode-bootstrap --output=jsonpath='{.items[*].metadata.name}') | tail -n +4 | sed -r 's/(.*aliases:.*)/ group_id: "+freenode:hackerspace.pl"\n\1/')
Serge Bazanskide627512020-08-24 21:17:55 +000027#
28# For appservice-telegram instances, you can use this oneliner magic:
29# kubectl -n matrix create secret generic appservice-telegram-prod-registration --from-file=registration.yaml=<(kubectl -n matrix logs job/appservice-telegram-prod-bootstrap | grep -A 100 SNIPSNIP | grep -v SNIPSNIP)
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020030
Serge Bazanski60076c72020-11-03 19:17:25 +010031local kube = import "../../../kube/kube.libsonnet";
32local postgres = import "../../../kube/postgres.libsonnet";
Serge Bazanskicdba2912020-08-24 19:11:10 +000033
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020034{
35 local app = self,
36 local cfg = app.cfg,
37 cfg:: {
Serge Bazanski60076c72020-11-03 19:17:25 +010038 namespace: error "cfg.namespace must be set",
39 # webDomain is the domain name at which element will run
40 webDomain: error "cfg.webDomain must be set",
41 # serverName is the server part of the MXID this homeserver will cover
42 serverName: error "cfg.serverName must be set",
Serge Bazanskide627512020-08-24 21:17:55 +000043 storageClassName: "waw-hdd-redundant-3",
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +020044
Serge Bazanskiace32c02020-11-03 22:04:06 +010045 images: {
46 synapse: "matrixdotorg/synapse:v1.19.2",
Serge Bazanskid67635d2020-12-29 23:25:54 +010047 riot: "vectorim/riot-web:v1.7.16",
Serge Bazanskiace32c02020-11-03 22:04:06 +010048 casProxy: "registry.k0.hswaw.net/q3k/oauth2-cas-proxy:0.1.4",
49 appserviceIRC: "matrixdotorg/matrix-appservice-irc:release-0.17.1",
50 # That's v0.8.2 - we just don't trust that host to not re-tag images.
51 appserviceTelegram: "dock.mau.dev/tulir/mautrix-telegram@sha256:9e68eaa80c9e4a75d9a09ec92dc4898b12d48390e01efa4de40ce882a6f7e330",
52 },
53
Norbert Szulcc67abc22020-11-08 16:46:56 +010054 # Central Authentication Scheme, a single-sign-on system. Note: this flow is now called 'SSO' in Matrix, we keep this name for legacy reasons.
55 # Refer to https://matrix.org/docs/spec/client_server/r0.6.1#sso-client-login
Serge Bazanskiace32c02020-11-03 22:04:06 +010056 cas: {
57 # whether to enable the CAS proxy (ie. connect to hswaw sso via OAuth)
58 enable: false,
Norbert Szulc014c9cd2020-11-08 16:44:48 +010059 # generate client ID and secret in with your OAuth2 provider, refer to https://www.oauth.com/oauth2-servers/client-registration/client-id-secret/
60 oauth2: {
61 clientID: error "cas.oauth2.clientID must be set",
62 clientSecret: error "cas.oauth2.clientSecret must be set",
63 scope: error "cas.oauth2.scope must be set",
64 authorizeURL: error "cas.oauth2.authorizeURL must be set",
65 tokenURL: error "cas.oauth2.tokenURL must be set",
66 userinfoURL: error "cas.oauth2.userinfoURL must be set",
67 },
Serge Bazanskiace32c02020-11-03 22:04:06 +010068 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020069 },
70
71 metadata(component):: {
Piotr Dobrowolski4b4231d2019-05-15 11:41:21 +020072 namespace: cfg.namespace,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020073 labels: {
74 "app.kubernetes.io/name": "matrix",
75 "app.kubernetes.io/managed-by": "kubecfg",
76 "app.kubernetes.io/component": component,
77 },
78 },
79
Piotr Dobrowolski4b4231d2019-05-15 11:41:21 +020080 namespace: kube.Namespace(cfg.namespace),
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020081
Serge Bazanskic0c037a2020-08-23 01:24:03 +000082 postgres3: postgres {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020083 cfg+: {
84 namespace: cfg.namespace,
85 appName: "synapse",
86 database: "synapse",
87 username: "synapse",
Serge Bazanskic0c037a2020-08-23 01:24:03 +000088 prefix: "waw3-",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020089 password: { secretKeyRef: { name: "synapse", key: "postgres_password" } },
Serge Bazanskide627512020-08-24 21:17:55 +000090 storageClassName: cfg.storageClassName,
Serge Bazanskic0c037a2020-08-23 01:24:03 +000091 storageSize: "100Gi",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020092 },
93 },
94
Serge Bazanskic0c037a2020-08-23 01:24:03 +000095 dataVolume: kube.PersistentVolumeClaim("synapse-data-waw3") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020096 metadata+: app.metadata("synapse-data"),
97 spec+: {
Serge Bazanskide627512020-08-24 21:17:55 +000098 storageClassName: cfg.storageClassName,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020099 accessModes: [ "ReadWriteOnce" ],
100 resources: {
101 requests: {
102 storage: "50Gi",
103 },
104 },
105 },
106 },
Piotr Dobrowolskiffbb47c2019-05-16 12:18:39 +0200107
Serge Bazanski21a96162020-11-03 23:34:36 +0100108 // homeserver.yaml that will be used to run synapse (in synapseConfigMap).
Serge Bazanskiace32c02020-11-03 22:04:06 +0100109 // This is based off of //app/matrix/lib/synapse/homeserver.yaml with some fields overriden per
110 // deployment.
111 // Note this is a templated yaml - {{}}/{%%} style. This templatization is consumed by the Docker
112 // container startup magic.
Serge Bazanski21a96162020-11-03 23:34:36 +0100113 synapseConfig:: (std.native("parseYaml"))(importstr "synapse/homeserver.yaml")[0] {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100114 server_name: cfg.serverName,
115 public_baseurl: "https://%s" % [cfg.webDomain],
116 signing_key_path: "/data/%s.signing.key" % [cfg.serverName],
Serge Bazanski21a96162020-11-03 23:34:36 +0100117 app_service_config_files: [
118 "/appservices/%s/registration.yaml" % [k]
119 for k in std.objectFields(app.appservices)
120 ],
121 } + (if cfg.cas.enable then {
122 cas_config: {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100123 enabled: true,
124 server_url: "https://%s/_cas" % [cfg.webDomain],
125 service_url: "https://%s" % [cfg.webDomain],
Serge Bazanski21a96162020-11-03 23:34:36 +0100126 },
127 } else {}),
Serge Bazanskiace32c02020-11-03 22:04:06 +0100128
Serge Bazanski21a96162020-11-03 23:34:36 +0100129 synapseConfigMap: kube.ConfigMap("synapse") {
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200130 metadata+: app.metadata("synapse"),
131 data: {
Serge Bazanski21a96162020-11-03 23:34:36 +0100132 "homeserver.yaml": std.manifestYamlDoc(app.synapseConfig),
Serge Bazanski60076c72020-11-03 19:17:25 +0100133 "log.config": importstr "synapse/log.config",
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200134 },
135 },
136
Serge Bazanskiace32c02020-11-03 22:04:06 +0100137 casDeployment: if cfg.cas.enable then kube.Deployment("oauth2-cas-proxy") {
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200138 metadata+: app.metadata("oauth2-cas-proxy"),
139 spec+: {
140 replicas: 1,
141 template+: {
142 spec+: {
143 containers_: {
144 proxy: kube.Container("oauth2-cas-proxy") {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100145 image: cfg.images.casProxy,
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200146 ports_: {
147 http: { containerPort: 5000 },
148 },
149 env_: {
Serge Bazanski60076c72020-11-03 19:17:25 +0100150 BASE_URL: "https://%s" % [cfg.webDomain],
151 SERVICE_URL: "https://%s" % [cfg.webDomain],
Norbert Szulc014c9cd2020-11-08 16:44:48 +0100152 OAUTH2_CLIENT: cfg.cas.oauth2.clientID,
153 OAUTH2_SECRET: cfg.cas.oauth2.clientSecret,
154 OAUTH2_SCOPE: cfg.cas.oauth2.scope,
155 OAUTH2_AUTHORIZE: cfg.cas.oauth2.authorizeURL,
156 OAUTH2_TOKEN: cfg.cas.oauth2.tokenURL,
157 OAUTH2_USERINFO: cfg.cas.oauth2.userinfoURL,
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200158 },
159 },
160 },
161 },
162 },
163 },
164 },
165
Serge Bazanskiace32c02020-11-03 22:04:06 +0100166 casSvc: if cfg.cas.enable then kube.Service("oauth2-cas-proxy") {
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200167 metadata+: app.metadata("oauth2-cas-proxy"),
168 target_pod:: app.casDeployment.spec.template,
169 },
170
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200171 synapseDeployment: kube.Deployment("synapse") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200172 metadata+: app.metadata("synapse"),
173 spec+: {
174 replicas: 1,
175 template+: {
176 spec+: {
177 volumes_: {
178 data: kube.PersistentVolumeClaimVolume(app.dataVolume),
Serge Bazanski21a96162020-11-03 23:34:36 +0100179 config: kube.ConfigMapVolume(app.synapseConfigMap),
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200180 } + {
181 [k]: { secret: { secretName: "appservice-%s-registration" % [k] } }
182 for k in std.objectFields(app.appservices)
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200183 },
184 containers_: {
185 web: kube.Container("synapse") {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100186 image: cfg.images.synapse,
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100187 command: ["/bin/sh", "-c", "/start.py migrate_config && exec /start.py"],
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200188 ports_: {
189 http: { containerPort: 8008 },
Serge Bazanski1230ac32020-09-12 22:09:46 +0000190 metrics: { containerPort: 9092 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200191 },
192 env_: {
Serge Bazanski21a96162020-11-03 23:34:36 +0100193 SYNAPSE_CONFIG_DIR: "/tmp/config",
194 SYNAPSE_CONFIG_PATH: "/tmp/config/homeserver.yaml",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200195
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100196 # These values are not used in a template, but
197 # are required by /start.py migrate_config
Serge Bazanski60076c72020-11-03 19:17:25 +0100198 SYNAPSE_SERVER_NAME: cfg.serverName,
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100199 SYNAPSE_REPORT_STATS: "no",
200
201 SYNAPSE_MACAROON_SECRET_KEY: { secretKeyRef: { name: "synapse", key: "macaroon_secret_key" } },
202 SYNAPSE_REGISTRATION_SHARED_SECRET: { secretKeyRef: { name: "synapse", key: "registration_shared_secret" } },
203 POSTGRES_PASSWORD: { secretKeyRef: { name: "synapse", key: "postgres_password" } },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200204 },
205 volumeMounts_: {
206 data: { mountPath: "/data" },
Serge Bazanski21a96162020-11-03 23:34:36 +0100207 config: { mountPath: "/conf", },
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200208 } + {
209 [k]: { mountPath: "/appservices/%s" % [k] }
210 for k in std.objectFields(app.appservices)
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200211 },
212 },
213 },
Serge Bazanski21a96162020-11-03 23:34:36 +0100214 securityContext: {
215 runAsUser: 991,
216 runAsGroup: 991,
217 fsGroup: 991,
218 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200219 },
220 },
221 },
222 },
223
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200224 synapseSvc: kube.Service("synapse") {
Piotr Dobrowolskiffbb47c2019-05-16 12:18:39 +0200225 metadata+: app.metadata("synapse"),
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200226 target_pod:: app.synapseDeployment.spec.template,
Piotr Dobrowolskiffbb47c2019-05-16 12:18:39 +0200227 },
228
Norbert Szulc1ef56002020-11-08 16:13:58 +0100229 riotConfig:: {
230 "default_hs_url": "https://%s" % [cfg.webDomain],
231 "disable_custom_urls": false,
232 "disable_guests": false,
233 "disable_login_language_selector": false,
234 "disable_3pid_login": true,
235 "brand": "Riot",
236 "integrations_ui_url": "https://scalar.vector.im/",
237 "integrations_rest_url": "https://scalar.vector.im/api",
238 "integrations_jitsi_widget_url": "https://scalar.vector.im/api/widgets/jitsi.html",
239
240 "bug_report_endpoint_url": "https://riot.im/bugreports/submit",
241 "features": {
242 "feature_groups": "labs",
243 "feature_pinning": "labs",
244 "feature_reactions": "labs"
245 },
246 "default_federate": true,
247 "default_theme": "light",
248 "roomDirectory": {
249 "servers": [
250 cfg.serverName,
251 ]
252 },
253 "welcomeUserId": "@riot-bot:matrix.org",
254 "enable_presence_by_hs_url": {
255 "https://matrix.org": false
256 }
257 },
258
259 riotConfigMap: kube.ConfigMap("riot-web-config") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200260 metadata+: app.metadata("riot-web-config"),
261 data: {
Norbert Szulc1ef56002020-11-08 16:13:58 +0100262 "config.json": std.manifestJsonEx(app.riotConfig, ""),
Serge Bazanski21a96162020-11-03 23:34:36 +0100263 // Standard nginx.conf, made to work when running as unprivileged user.
264 "nginx.conf": |||
265 worker_processes auto;
266
267 error_log /tmp/nginx_error.log warn;
268 pid /tmp/nginx.pid;
269
270 events {
271 worker_connections 1024;
272 }
273
274
275 http {
276 client_body_temp_path /tmp/nginx_client_temp;
277 proxy_temp_path /tmp/nginx_proxy_temp;
278 fastcgi_temp_path /tmp/nginx_fastcgi_temp;
279 uwsgi_temp_path /tmp/nginx_uwsgi_temp;
280 scgi_temp_path /tmp/nginx_scgi_temp;
281
282 include /etc/nginx/mime.types;
283 default_type application/octet-stream;
284 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
285 '$status $body_bytes_sent "$http_referer" '
286 '"$http_user_agent" "$http_x_forwarded_for"';
287 access_log /tmp/nginx_access.log main;
288 sendfile on;
289 keepalive_timeout 65;
290
291 server {
292 listen 8080;
293 server_name localhost;
294
295 location / {
296 root /usr/share/nginx/html;
297 index index.html index.htm;
298 }
299
300 error_page 500 502 503 504 /50x.html;
301 location = /50x.html {
302 root /usr/share/nginx/html;
303 }
304 }
305 }
306 |||,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200307 },
308 },
309
310 riotDeployment: kube.Deployment("riot-web") {
311 metadata+: app.metadata("riot-web"),
312 spec+: {
313 replicas: 1,
314 template+: {
315 spec+: {
316 volumes_: {
Norbert Szulc1ef56002020-11-08 16:13:58 +0100317 config: kube.ConfigMapVolume(app.riotConfigMap),
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200318 },
319 containers_: {
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200320 web: kube.Container("riot-web") {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100321 image: cfg.images.riot,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200322 ports_: {
Serge Bazanski21a96162020-11-03 23:34:36 +0100323 http: { containerPort: 8080 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200324 },
Serge Bazanski21a96162020-11-03 23:34:36 +0100325 volumeMounts: [
326 {
327 name: "config",
Piotr Dobrowolskiaca7e282020-03-21 22:14:38 +0100328 mountPath: "/app/config.json",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200329 subPath: "config.json",
330 },
Serge Bazanski21a96162020-11-03 23:34:36 +0100331 {
332 name: "config",
333 mountPath: "/etc/nginx/nginx.conf",
334 subPath: "nginx.conf",
335 },
336 ],
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200337 },
338 },
Serge Bazanski21a96162020-11-03 23:34:36 +0100339 securityContext: {
340 // nginx:nginx
341 runAsUser: 101,
342 runAsGroup: 101,
343 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200344 },
345 },
346 },
347 },
348
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200349 riotSvc: kube.Service("riot-web") {
350 metadata+: app.metadata("riot-web"),
351 target_pod:: app.riotDeployment.spec.template,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200352 },
353
Serge Bazanskide627512020-08-24 21:17:55 +0000354 // Any appservice you add here will require an appservice-X-registration
355 // secret containing a registration.yaml file. Adding something to this
356 // dictionary will cause Synapse to not start until that secret is
357 // available - so change things carefully!
358 // If bootstrapping a new appservice, just keep it out of this dictionary
359 // until it spits you a registration YAML and you feed that to a secret.
Serge Bazanski60076c72020-11-03 19:17:25 +0100360 appservices: {},
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200361
362 ingress: kube.Ingress("matrix") {
363 metadata+: app.metadata("matrix") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200364 annotations+: {
365 "kubernetes.io/tls-acme": "true",
366 "certmanager.k8s.io/cluster-issuer": "letsencrypt-prod",
367 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
368 },
369 },
370 spec+: {
371 tls: [
372 {
Serge Bazanski60076c72020-11-03 19:17:25 +0100373 hosts: [cfg.webDomain],
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200374 secretName: "synapse-tls",
375 },
376 ],
377 rules: [
378 {
Serge Bazanski60076c72020-11-03 19:17:25 +0100379 host: cfg.webDomain,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200380 http: {
381 paths: [
382 { path: "/", backend: app.riotSvc.name_port },
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200383 { path: "/_matrix", backend: app.synapseSvc.name_port },
Serge Bazanskiace32c02020-11-03 22:04:06 +0100384 ] + (if cfg.cas.enable then [
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200385 { path: "/_cas", backend: app.casSvc.name_port },
Serge Bazanskiace32c02020-11-03 22:04:06 +0100386 ] else [])
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200387 },
388 }
389 ],
390 },
391 },
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200392
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200393}