blob: 7d08ad7d5cf125b013df738d703d00cc4935aa69 [file] [log] [blame]
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +02001# Factorio on Kubernetes.
2
Dariusz Niemczykb3799c82021-06-12 23:02:58 +02003local kube = import "../../../kube/kube.libsonnet";
Serge Bazanskic6cc5612021-06-13 21:55:18 +00004local proxy = import "proxy.libsonnet";
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +02005
6{
7 local factorio = self,
8 local cfg = factorio.cfg,
9
10 cfg:: {
11 namespace: error "namespace must be set",
12 appName: "factorio",
Serge Bazanski50a041d2021-06-13 21:04:54 +000013 storageClassName: "waw-hdd-redundant-3",
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020014 prefix: "", # if set, should be 'foo-'
Serge Bazanskic6cc5612021-06-13 21:55:18 +000015 proxyImage: proxy.cfg.image,
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020016
17 rconPort: 2137,
18 rconPassword: "farts",
19
Serge Bazanski08080342021-06-21 14:05:56 +000020 // Configuration options that will get serialized into
21 // --config config.ini
22 factorioConfig: {
23 // There is no documentation for this file, but you can check
24 // config.ini in any installed Factorio data directory for a
25 // sample.
26 //
27 // This uses std.manifestIni, so to create a file containing:
28 // version=9
29 // [other]
30 // verbose-logging=true
31 // You would set the following:
32 // main: {
33 // version: "9",
34 // },
35 // other: {
36 // "verbose-logging": "true",
37 // },
38 },
39 // Configuration options that will get serialized into
40 // --server-settings server-settings.json.
41 serverSettings: (import "config/server-settings.libsonnet") {
42 visibility+: {
43 public: false,
44 lan: false,
45 },
46 },
47 // Configuration options that will get serialized into
48 // --map-settings map-settings.json.
49 mapSettings: (import "config/map-settings.libsonnet") {
50 },
51 // Configuration options that will get serialized into
52 // --map-gen-settings map-gen-settings.json.
53 mapGenSettings: (import "config/map-gen-settings.libsonnet") {
54 },
55
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020056 tag: "latest",
Serge Bazanski970b7682020-08-04 20:33:17 +020057 image: "registry.k0.hswaw.net/q3k/factorio:" + cfg.tag,
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020058 resources: {
59 requests: {
60 cpu: "500m",
61 memory: "500Mi",
62 },
63 limits: {
Serge Bazanski9c7e5392021-06-18 21:24:07 +020064 cpu: "2",
65 memory: "2Gi",
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020066 },
67 },
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020068
69 mods: [],
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020070 },
71
72
73 makeName(suffix):: cfg.prefix + suffix,
74
75 metadata:: {
76 namespace: cfg.namespace,
77 labels: {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +020078 "app.kubernetes.io/name": factorio.makeName("factorio"),
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020079 "app.kubernetes.io/managed-by": "kubecfg",
80 "app.kubernetes.io/component": "factorio",
81 },
82 },
83
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010084 volumeClaimMods: kube.PersistentVolumeClaim(factorio.makeName("factorio-mods")) {
85 metadata+: factorio.metadata,
86 spec+: {
87 storageClassName: cfg.storageClassName,
88 accessModes: [ "ReadWriteOnce" ],
89 resources: {
90 requests: {
91 storage: "1Gi",
92 },
93 },
94 },
95 },
96
97 volumeClaimData: kube.PersistentVolumeClaim(factorio.makeName("factorio")) {
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020098 metadata+: factorio.metadata,
99 spec+: {
100 storageClassName: cfg.storageClassName,
101 accessModes: [ "ReadWriteOnce" ],
102 resources: {
103 requests: {
104 storage: "5Gi",
105 },
106 },
107 },
108 },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100109
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +0200110 configMap: kube.ConfigMap(factorio.makeName("config")) {
111 metadata+: factorio.metadata,
112 data: {
113 "mods.pb.text": std.join("\n", [
114 "mod { name: \"%s\" version: \"%s\" }" % [m.name, m.version],
115 for m in cfg.mods
116 ]),
Serge Bazanski08080342021-06-21 14:05:56 +0000117 "server-settings.json": std.manifestJson(cfg.serverSettings),
118 "map-settings.json": std.manifestJson(cfg.mapSettings),
119 "map-gen-settings.json": std.manifestJson(cfg.mapGenSettings),
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +0200120 },
121 },
122
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200123 deployment: kube.Deployment(factorio.makeName("factorio")) {
124 metadata+: factorio.metadata,
125 spec+: {
126 replicas: 1,
127 template+: {
128 spec+: {
129 volumes_: {
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100130 data: kube.PersistentVolumeClaimVolume(factorio.volumeClaimData),
131 mods: kube.PersistentVolumeClaimVolume(factorio.volumeClaimMods),
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +0200132 config: kube.ConfigMapVolume(factorio.configMap),
133 },
134 initContainers_: {
135 modproxy: kube.Container("modproxy") {
136 image: cfg.proxyImage,
137 command: [
138 "/games/factorio/modproxy/client",
139 "-hspki_disable",
140 "-factorio_path", "/factorio",
141 "-proxy", "proxy.factorio.svc.cluster.local:4200",
142 "-config_path", "/factorio/mods.pb.text",
143 ],
144 volumeMounts_: {
145 mods: { mountPath: "/factorio/mods" },
146 config: { mountPath: "/factorio/mods.pb.text", subPath: "mods.pb.text" },
147 },
148 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200149 },
150 containers_: {
151 factorio: kube.Container(factorio.makeName("factorio")) {
152 image: cfg.image,
153 args: [
154 "/entrypoint.sh",
155 "--rcon-port", std.toString(cfg.rconPort),
156 "--rcon-password", cfg.rconPassword,
Serge Bazanski08080342021-06-21 14:05:56 +0000157 "--server-settings", "/factorio/config/server-settings.json",
158 "--map-settings", "/factorio/config/map-settings.json",
159 "--map-gen-settings", "/factorio/config/map-gen-settings.json",
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200160 ],
161 ports_: {
162 client: { containerPort: 34197 },
163 rcon: { containerPort: cfg.rconPort },
164 },
165 volumeMounts_: {
166 data: { mountPath: "/data" },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100167 mods: { mountPath: "/factorio/mods" },
Serge Bazanski08080342021-06-21 14:05:56 +0000168 config: { mountPath: "/factorio/config" },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200169 },
170 resources: cfg.resources,
171 },
172 },
173 },
174 },
175 },
176 },
177 svc: kube.Service(factorio.makeName("factorio")) {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +0200178 metadata+: factorio.metadata {
179 // hack - have to keep existing naming scheme otherwise we'd lose addresses
180 labels: {
181 "app.kubernetes.io/name": cfg.appName,
182 },
183 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200184 target_pod:: factorio.deployment.spec.template,
185 spec+: {
186 ports: [
187 { name: "client", port: 34197, targetPort: 34197, protocol: "UDP" },
188 ],
189 type: "LoadBalancer",
190 },
191 },
192}