blob: ed033bb7a8676af8dc7a79e9bcad5fce1b632f1a [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#
7# Sequencing appservices is fun. The appservice needs to run first (for
8# instance, via a bootstrap job), and on startup it will spit out a
9# registration file. This registration file then needs to be fed to synapse -
10# this is done via specialy named secrets (appservice-X-registration, for X key
11# in the appservices object).
12#
13# For appservice-irc instances, you can use this oneliner magic to get the
14# registration YAML from logs.
Piotr Dobrowolski3ea979d2019-05-23 16:11:52 +020015# 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 +000016#
17# For appservice-telegram instances, you can use this oneliner magic:
18# 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 +020019
Serge Bazanski60076c72020-11-03 19:17:25 +010020local kube = import "../../../kube/kube.libsonnet";
21local postgres = import "../../../kube/postgres.libsonnet";
Serge Bazanskicdba2912020-08-24 19:11:10 +000022
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020023{
24 local app = self,
25 local cfg = app.cfg,
26 cfg:: {
Serge Bazanski60076c72020-11-03 19:17:25 +010027 namespace: error "cfg.namespace must be set",
28 # webDomain is the domain name at which element will run
29 webDomain: error "cfg.webDomain must be set",
30 # serverName is the server part of the MXID this homeserver will cover
31 serverName: error "cfg.serverName must be set",
Serge Bazanskide627512020-08-24 21:17:55 +000032 storageClassName: "waw-hdd-redundant-3",
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +020033
Serge Bazanskiace32c02020-11-03 22:04:06 +010034 images: {
35 synapse: "matrixdotorg/synapse:v1.19.2",
36 riot: "vectorim/riot-web:v1.7.7",
37 casProxy: "registry.k0.hswaw.net/q3k/oauth2-cas-proxy:0.1.4",
38 appserviceIRC: "matrixdotorg/matrix-appservice-irc:release-0.17.1",
39 # That's v0.8.2 - we just don't trust that host to not re-tag images.
40 appserviceTelegram: "dock.mau.dev/tulir/mautrix-telegram@sha256:9e68eaa80c9e4a75d9a09ec92dc4898b12d48390e01efa4de40ce882a6f7e330",
41 },
42
43 cas: {
44 # whether to enable the CAS proxy (ie. connect to hswaw sso via OAuth)
45 enable: false,
46 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020047 },
48
49 metadata(component):: {
Piotr Dobrowolski4b4231d2019-05-15 11:41:21 +020050 namespace: cfg.namespace,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020051 labels: {
52 "app.kubernetes.io/name": "matrix",
53 "app.kubernetes.io/managed-by": "kubecfg",
54 "app.kubernetes.io/component": component,
55 },
56 },
57
Piotr Dobrowolski4b4231d2019-05-15 11:41:21 +020058 namespace: kube.Namespace(cfg.namespace),
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020059
Serge Bazanskic0c037a2020-08-23 01:24:03 +000060 postgres3: postgres {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020061 cfg+: {
62 namespace: cfg.namespace,
63 appName: "synapse",
64 database: "synapse",
65 username: "synapse",
Serge Bazanskic0c037a2020-08-23 01:24:03 +000066 prefix: "waw3-",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020067 password: { secretKeyRef: { name: "synapse", key: "postgres_password" } },
Serge Bazanskide627512020-08-24 21:17:55 +000068 storageClassName: cfg.storageClassName,
Serge Bazanskic0c037a2020-08-23 01:24:03 +000069 storageSize: "100Gi",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020070 },
71 },
72
Serge Bazanskic0c037a2020-08-23 01:24:03 +000073 dataVolume: kube.PersistentVolumeClaim("synapse-data-waw3") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020074 metadata+: app.metadata("synapse-data"),
75 spec+: {
Serge Bazanskide627512020-08-24 21:17:55 +000076 storageClassName: cfg.storageClassName,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +020077 accessModes: [ "ReadWriteOnce" ],
78 resources: {
79 requests: {
80 storage: "50Gi",
81 },
82 },
83 },
84 },
Piotr Dobrowolskiffbb47c2019-05-16 12:18:39 +020085
Serge Bazanskiace32c02020-11-03 22:04:06 +010086 // homeserver.yaml that will be used to run synapse (in synapseConfig ConfigMap).
87 // This is based off of //app/matrix/lib/synapse/homeserver.yaml with some fields overriden per
88 // deployment.
89 // Note this is a templated yaml - {{}}/{%%} style. This templatization is consumed by the Docker
90 // container startup magic.
91 homeserverYaml:: (std.native("parseYaml"))(importstr "synapse/homeserver.yaml")[0] {
92 server_name: cfg.serverName,
93 public_baseurl: "https://%s" % [cfg.webDomain],
94 signing_key_path: "/data/%s.signing.key" % [cfg.serverName],
95 cas_config+: if cfg.cas.enable then {
96 enabled: true,
97 server_url: "https://%s/_cas" % [cfg.webDomain],
98 service_url: "https://%s" % [cfg.webDomain],
99 } else {},
100 app_service_config_files: [
101 "/data/appservices/%s.yaml" % [k]
102 for k in std.objectFields(app.appservices)
103 ],
104 },
105
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200106 synapseConfig: kube.ConfigMap("synapse") {
107 metadata+: app.metadata("synapse"),
108 data: {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100109 "homeserver.yaml": std.manifestYamlDoc(app.homeserverYaml),
Serge Bazanski60076c72020-11-03 19:17:25 +0100110 "log.config": importstr "synapse/log.config",
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200111 },
112 },
113
Serge Bazanskiace32c02020-11-03 22:04:06 +0100114 casDeployment: if cfg.cas.enable then kube.Deployment("oauth2-cas-proxy") {
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200115 metadata+: app.metadata("oauth2-cas-proxy"),
116 spec+: {
117 replicas: 1,
118 template+: {
119 spec+: {
120 containers_: {
121 proxy: kube.Container("oauth2-cas-proxy") {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100122 image: cfg.images.casProxy,
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200123 ports_: {
124 http: { containerPort: 5000 },
125 },
126 env_: {
Serge Bazanski60076c72020-11-03 19:17:25 +0100127 BASE_URL: "https://%s" % [cfg.webDomain],
128 SERVICE_URL: "https://%s" % [cfg.webDomain],
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200129 OAUTH2_CLIENT: "matrix",
130 OAUTH2_SECRET: { secretKeyRef: { name: "oauth2-cas-proxy", key: "oauth2_secret" } },
131 },
132 },
133 },
134 },
135 },
136 },
137 },
138
Serge Bazanskiace32c02020-11-03 22:04:06 +0100139 casSvc: if cfg.cas.enable then kube.Service("oauth2-cas-proxy") {
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200140 metadata+: app.metadata("oauth2-cas-proxy"),
141 target_pod:: app.casDeployment.spec.template,
142 },
143
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200144 synapseDeployment: kube.Deployment("synapse") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200145 metadata+: app.metadata("synapse"),
146 spec+: {
147 replicas: 1,
148 template+: {
149 spec+: {
150 volumes_: {
151 data: kube.PersistentVolumeClaimVolume(app.dataVolume),
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100152 config_template: kube.ConfigMapVolume(app.synapseConfig),
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200153 } + {
154 [k]: { secret: { secretName: "appservice-%s-registration" % [k] } }
155 for k in std.objectFields(app.appservices)
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200156 },
157 containers_: {
158 web: kube.Container("synapse") {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100159 image: cfg.images.synapse,
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100160 command: ["/bin/sh", "-c", "/start.py migrate_config && exec /start.py"],
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200161 ports_: {
162 http: { containerPort: 8008 },
Serge Bazanski1230ac32020-09-12 22:09:46 +0000163 metrics: { containerPort: 9092 },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200164 },
165 env_: {
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100166 SYNAPSE_CONFIG_DIR: "/config",
Piotr Dobrowolskieabbe8a2019-08-11 19:49:08 +0200167 SYNAPSE_CONFIG_PATH: "/config/homeserver.yaml",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200168
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100169 # These values are not used in a template, but
170 # are required by /start.py migrate_config
Serge Bazanski60076c72020-11-03 19:17:25 +0100171 SYNAPSE_SERVER_NAME: cfg.serverName,
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100172 SYNAPSE_REPORT_STATS: "no",
173
174 SYNAPSE_MACAROON_SECRET_KEY: { secretKeyRef: { name: "synapse", key: "macaroon_secret_key" } },
175 SYNAPSE_REGISTRATION_SHARED_SECRET: { secretKeyRef: { name: "synapse", key: "registration_shared_secret" } },
176 POSTGRES_PASSWORD: { secretKeyRef: { name: "synapse", key: "postgres_password" } },
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200177 },
178 volumeMounts_: {
179 data: { mountPath: "/data" },
Piotr Dobrowolski8ebfc1d2020-03-03 21:01:18 +0100180 config_template: {
181 mountPath: "/conf/homeserver.yaml",
182 subPath: "homeserver.yaml",
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200183 },
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200184 } + {
185 [k]: { mountPath: "/appservices/%s" % [k] }
186 for k in std.objectFields(app.appservices)
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200187 },
188 },
189 },
190 },
191 },
192 },
193 },
194
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200195 synapseSvc: kube.Service("synapse") {
Piotr Dobrowolskiffbb47c2019-05-16 12:18:39 +0200196 metadata+: app.metadata("synapse"),
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200197 target_pod:: app.synapseDeployment.spec.template,
Piotr Dobrowolskiffbb47c2019-05-16 12:18:39 +0200198 },
199
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200200 riotConfig: kube.ConfigMap("riot-web-config") {
201 metadata+: app.metadata("riot-web-config"),
202 data: {
203 "config.json": std.manifestJsonEx({
Serge Bazanski60076c72020-11-03 19:17:25 +0100204 "default_hs_url": "https://%s" % [cfg.webDomain],
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200205 "disable_custom_urls": false,
206 "disable_guests": false,
207 "disable_login_language_selector": false,
Piotr Dobrowolski4b4231d2019-05-15 11:41:21 +0200208 "disable_3pid_login": true,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200209 "brand": "Riot",
210 "integrations_ui_url": "https://scalar.vector.im/",
211 "integrations_rest_url": "https://scalar.vector.im/api",
212 "integrations_jitsi_widget_url": "https://scalar.vector.im/api/widgets/jitsi.html",
Piotr Dobrowolski4b4231d2019-05-15 11:41:21 +0200213
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200214 "bug_report_endpoint_url": "https://riot.im/bugreports/submit",
215 "features": {
216 "feature_groups": "labs",
217 "feature_pinning": "labs",
218 "feature_reactions": "labs"
219 },
220 "default_federate": true,
221 "default_theme": "light",
222 "roomDirectory": {
223 "servers": [
Serge Bazanski60076c72020-11-03 19:17:25 +0100224 cfg.serverName,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200225 ]
226 },
227 "welcomeUserId": "@riot-bot:matrix.org",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200228 "enable_presence_by_hs_url": {
229 "https://matrix.org": false
230 }
231 }, ""),
232 },
233 },
234
235 riotDeployment: kube.Deployment("riot-web") {
236 metadata+: app.metadata("riot-web"),
237 spec+: {
238 replicas: 1,
239 template+: {
240 spec+: {
241 volumes_: {
242 config: kube.ConfigMapVolume(app.riotConfig),
243 },
244 containers_: {
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200245 web: kube.Container("riot-web") {
Serge Bazanskiace32c02020-11-03 22:04:06 +0100246 image: cfg.images.riot,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200247 ports_: {
248 http: { containerPort: 80 },
249 },
250 volumeMounts_: {
251 config: {
Piotr Dobrowolskiaca7e282020-03-21 22:14:38 +0100252 mountPath: "/app/config.json",
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200253 subPath: "config.json",
254 },
255 },
256 },
257 },
258 },
259 },
260 },
261 },
262
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200263 riotSvc: kube.Service("riot-web") {
264 metadata+: app.metadata("riot-web"),
265 target_pod:: app.riotDeployment.spec.template,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200266 },
267
Serge Bazanskide627512020-08-24 21:17:55 +0000268 // Any appservice you add here will require an appservice-X-registration
269 // secret containing a registration.yaml file. Adding something to this
270 // dictionary will cause Synapse to not start until that secret is
271 // available - so change things carefully!
272 // If bootstrapping a new appservice, just keep it out of this dictionary
273 // until it spits you a registration YAML and you feed that to a secret.
Serge Bazanski60076c72020-11-03 19:17:25 +0100274 appservices: {},
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200275
276 ingress: kube.Ingress("matrix") {
277 metadata+: app.metadata("matrix") {
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200278 annotations+: {
279 "kubernetes.io/tls-acme": "true",
280 "certmanager.k8s.io/cluster-issuer": "letsencrypt-prod",
281 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
282 },
283 },
284 spec+: {
285 tls: [
286 {
Serge Bazanski60076c72020-11-03 19:17:25 +0100287 hosts: [cfg.webDomain],
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200288 secretName: "synapse-tls",
289 },
290 ],
291 rules: [
292 {
Serge Bazanski60076c72020-11-03 19:17:25 +0100293 host: cfg.webDomain,
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200294 http: {
295 paths: [
296 { path: "/", backend: app.riotSvc.name_port },
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200297 { path: "/_matrix", backend: app.synapseSvc.name_port },
Serge Bazanskiace32c02020-11-03 22:04:06 +0100298 ] + (if cfg.cas.enable then [
Piotr Dobrowolskic39fb042019-05-17 09:13:56 +0200299 { path: "/_cas", backend: app.casSvc.name_port },
Serge Bazanskiace32c02020-11-03 22:04:06 +0100300 ] else [])
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200301 },
302 }
303 ],
304 },
305 },
Piotr Dobrowolskifef4c122019-05-16 21:05:02 +0200306
Piotr Dobrowolskia2226912019-05-14 18:49:29 +0200307}