blob: 286ffa751c1b0c80d270b1d398be331ac84e59cd [file] [log] [blame]
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +02001local 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
9 homeservers: [],
10 admins: [],
11
12 s3: {
13 endpoint: error "cfg.s3.endpoint needs to be set",
14 accessKey: error "cfg.s3.accessKey needs to be set",
15 secretKey: error "cfg.s3.secretKey needs to be set",
16 bucketName: error "cfg.s3.bucketName needs to be set",
17 region: error "cfg.s3.region needs to be set",
18 },
19
20 db: {
21 username: error "cfg.db.username needs to be set",
22 password: error "cfg.db.password needs to be set",
23 database: error "cfg.db.database needs to be set",
24 host: error "cfg.db.host needs to be set",
25 },
26 },
27
28 ns:: error "ns needs to be a kube.Namespace object",
29
30 config:: {
31 repo: {
32 bindAddress: "0.0.0.0",
33 port: 8000,
Piotr Dobrowolski21c8cd62021-09-16 13:07:54 +020034 useForwardedHost: false,
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +020035 },
36 database: {
37 postgres: "postgres://%s:%s@%s/%s?sslmode=disable" % [cfg.db.username, cfg.db.password, cfg.db.host, cfg.db.database],
38 },
39 homeservers: cfg.homeservers,
40 admins: cfg.admins,
Piotr Dobrowolski21c8cd62021-09-16 13:07:54 +020041 thumbnails: {
42 maxSourceBytes: 10485760 * 3,
43 },
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +020044 datastores: [
45 {
46 type: "s3",
47 enabled: true,
48 forKinds: ["all"],
49 opts: {
50 tempPath: "/tmp/mediarepo_s3_upload",
51 endpoint: cfg.s3.endpoint,
52 accessKeyId: cfg.s3.accessKey,
53 accessSecret: cfg.s3.secretKey,
54 ssl: false,
55 bucketName: cfg.s3.bucketName,
56 region: cfg.s3.region,
57 },
58 }
59 ],
60 },
61
62 configSecret: app.ns.Contain(kube.Secret("media-repo-config")) {
63 data_: {
64 "config.yaml": std.manifestJsonEx(app.config, ""),
65 },
66 },
67
68 deployment: app.ns.Contain(kube.Deployment("media-repo")) {
69 spec+: {
70 replicas: 1,
71 template+: {
72 spec+: {
73 volumes_: {
74 config: kube.SecretVolume(app.configSecret),
75 tempdir: kube.EmptyDirVolume(),
76 },
77 containers_: {
78 repo: kube.Container("media-repo") {
79 image: cfg.image,
80 command: ["/usr/local/bin/media_repo"],
81 ports_: {
82 http: { containerPort: 8000 },
83 },
84 env_: {
85 REPO_CONFIG: "/config",
86 },
87 volumeMounts_: {
88 config: { mountPath: "/config" },
89 tempdir: { mountPath: "/tmp/mediarepo_s3_upload" },
90 },
91 },
92 },
93 },
94 },
95 },
96 },
97
Serge Bazanskiebe60752021-09-16 11:28:00 +020098 // Run //app/matrix/media-repo-proxy, if needed. This rewrites Host headers
99 // from the homeserver's serving Host to the MXID hostname (which
100 // matrix-media-repo expects).
101 //
102 // Currently we only are able to run one proxy for one homeserver config -
103 // but we don't expect to have multiple homeservers per matrix-media-repo
104 // any time soon.
105 local needProxying = [
106 h
107 for h in cfg.homeservers
108 if "https://%s" % [h.name] != h.csApi
109 ],
110 proxies: if std.length(needProxying) > 1 then error "can only proxy one homeserver" else
111 if std.length(needProxying) == 1 then {
112 local homeserver = needProxying[0],
113
114 local upstreamHost = homeserver.name,
115 local prefix = "https://",
116 local downstreamHost = std.substr(homeserver.csApi, std.length(prefix), std.length(homeserver.csApi)-std.length(prefix)),
117
118 deployment: app.ns.Contain(kube.Deployment("media-repo-proxy")) {
119 spec+: {
120 template+: {
121 spec+: {
122 containers_: {
123 default: kube.Container("default") {
124 image: "registry.k0.hswaw.net/q3k/media-repo-proxy:1631791816-18609443fffde38a055f504e80f95e44f49d2481",
125 command: [
126 "/app/matrix/media-repo-proxy",
127 "-downstream_host", downstreamHost,
128 "-upstream_host", upstreamHost,
129 "-upstream", app.internalSvc.host_colon_port,
130 "-listen", ":8080",
131 ],
132 ports_: {
133 http: { containerPort: 8080 },
134 },
135 },
136 },
137 },
138 },
139 },
140 },
141 } else {},
142
143 internalSvc: app.ns.Contain(kube.Service("media-repo-internal")) {
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +0200144 target_pod:: app.deployment.spec.template,
145 },
Serge Bazanskiebe60752021-09-16 11:28:00 +0200146
147 svc: if std.length(needProxying) > 0 then app.ns.Contain(kube.Service("media-repo")) {
148 target_pod:: app.proxies.deployment.spec.template,
149 } else app.internalSvc,
Piotr Dobrowolski122d5e52021-09-10 23:54:34 +0200150}