blob: 6700fbc5e2ec67844922a19caaa1dd29255dea5f [file] [log] [blame]
Serge Bazanski60076c72020-11-03 19:17:25 +01001local kube = import "../../../kube/kube.libsonnet";
Serge Bazanskide627512020-08-24 21:17:55 +00002
3{
4 AppServiceTelegram(name):: {
5 local bridge = self,
6 local cfg = bridge.cfg,
7 cfg:: {
8 metadata: {},
9 image: error "image must be set",
10 storageClassName: error "storageClassName must be set",
11
12 // Data that will be serialized into the appservice's config.yaml.
13 // This is taken straight from a YAML that was generated by
14 // dock.mau.dev/tulir/mautrix-telegram:v0.8.2. We override here
15 // fields that we know are strictly necessary to be configured when
16 // instantiating this template.
Serge Bazanski60076c72020-11-03 19:17:25 +010017 config: (std.native("parseYaml")(importstr "appservice/appservice-telegram.yaml")[0]) + {
Serge Bazanskide627512020-08-24 21:17:55 +000018 homeserver+: {
19 address: error "homeserver.address must be set",
20 domain: error "homeserver.domain must be set",
21 },
22 appservice+: {
23 address: bridge.svc.http_url,
24 // We disable this. I have no idea what it does, but it
25 // wants a secret. ~q3k
26 provisioning+: {
27 enabled: false,
28 shared_secret: if self.enabled then error "appservice.provisioning.shared_secret must be set" else "hackme",
29 },
30 id: error "appservice.id must be set",
31 as_token: "This value is generated when generating the registration",
32 hs_token: "This value is generated when generating the registration",
33 },
34 telegram+: {
35 api_id: error "telegram.api_id must be set",
36 api_hash: error "telegram.api_hash must be set",
37 bot_token: error "telegram.bot_token must be set",
38 },
39 bridge+: {
40 permissions: {
41 '*': "relaybot",
42 },
43 },
44 },
45 },
46
47 config: kube.Secret("appservice-telegram-%s" % [name]) {
48 metadata+: cfg.metadata,
49 data: {
50 "config.yaml": std.base64(std.manifestYamlDoc(cfg.config)),
51 },
52 },
53
54 dataVolume: kube.PersistentVolumeClaim("appservice-telegram-%s" % [name]) {
55 metadata+: cfg.metadata,
56 spec+: {
57 storageClassName: cfg.storageClassName,
58 accessModes: [ "ReadWriteOnce" ],
59 resources: {
60 requests: {
61 storage: "10Gi",
62 },
63 },
64 },
65 },
66
67 bootstrapJob: kube.Job("appservice-telegram-%s-bootstrap" % [name]) {
68 metadata+: cfg.metadata {
69 labels: {
70 "job-name": "appservice-telegram-%s-bootstrap" % [name],
71 },
72 },
73 spec+: {
74 template+: {
75 spec+: {
76 volumes_: {
77 config: kube.SecretVolume(bridge.config),
78 },
79 containers_: {
80 bootstrap: kube.Container("appservice-telegram-%s-bootstrap" % [name]) {
81 image: cfg.image,
82 command: [
83 "sh", "-c",
84 "python3 -m mautrix_telegram -g -c /config/config.yaml -r /tmp/registration.yaml && echo SNIPSNIP && cat /tmp/registration.yaml",
85 ],
86 volumeMounts_: {
87 config: { mountPath: "/config" },
88 },
89 },
90 },
91 },
92 },
93 },
94 },
95
96 deployment: kube.Deployment("appservice-telegram-%s" % [name]) {
97 metadata+: cfg.metadata,
98 spec+: {
99 replicas: 1,
100 template+: {
101 spec+: {
102 volumes_: {
103 config: kube.SecretVolume(bridge.config),
104 data: kube.PersistentVolumeClaimVolume(bridge.dataVolume),
105 registration: { secret: { secretName: "appservice-telegram-%s-registration" % [name] } },
106 },
107 initContainers: [
108 // This container takes the stateless config from the Secret, and
109 // updates it with the registration secrets from the registration token.
110 kube.Container("generate-config") {
111 volumeMounts_: {
112 config: { mountPath: "/config", },
113 registration: { mountPath: "/registration", },
114 data: { mountPath: "/data" },
115 },
Serge Bazanski34f56932021-02-08 17:53:44 +0100116 image: "alpine:3.13",
Serge Bazanskide627512020-08-24 21:17:55 +0000117 command: [
118 "sh", "-c", |||
119 set -e -x
120 apk add --no-cache yq
121 cp /config/config.yaml /data/config.yaml
122 yq w -i /data/config.yaml appservice.as_token $(yq r /registration/registration.yaml as_token)
123 yq w -i /data/config.yaml appservice.hs_token $(yq r /registration/registration.yaml hs_token)
124 |||
125 ],
126 },
127 ],
128 containers_: {
129 appserviceIrc: kube.Container("appservice-telegram-%s" % [name]) {
130 image: cfg.image,
131 command: [
132 "sh", "-c", |||
133 alembic -x config=/data/config.yaml upgrade head
134 python3 -m mautrix_telegram -n -c /data/config.yaml
135 |||
136 ],
137 ports_: {
138 http: { containerPort: 29317 },
139 },
140 volumeMounts_: {
141 data: { mountPath: "/data" },
142 },
143 },
144 },
145 },
146 },
147 },
148 },
149
150 svc: kube.Service("appservice-telegram-%s" % [name]) {
151 metadata+: cfg.metadata,
152 target_pod:: bridge.deployment.spec.template,
153 },
154 },
155}