blob: 7a3a06f64eb7fa3a63111be72fd30ed63d17b815 [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,
radex36964dc2023-11-24 11:19:46 +010086 storage:: "1Gi",
87 storageClass:: cfg.storageClassName,
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010088 },
89
90 volumeClaimData: kube.PersistentVolumeClaim(factorio.makeName("factorio")) {
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020091 metadata+: factorio.metadata,
radex36964dc2023-11-24 11:19:46 +010092 storage:: "5Gi",
93 storageClass:: cfg.storageClassName,
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020094 },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010095
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020096 configMap: kube.ConfigMap(factorio.makeName("config")) {
97 metadata+: factorio.metadata,
98 data: {
99 "mods.pb.text": std.join("\n", [
100 "mod { name: \"%s\" version: \"%s\" }" % [m.name, m.version],
101 for m in cfg.mods
102 ]),
Serge Bazanski08080342021-06-21 14:05:56 +0000103 "server-settings.json": std.manifestJson(cfg.serverSettings),
104 "map-settings.json": std.manifestJson(cfg.mapSettings),
105 "map-gen-settings.json": std.manifestJson(cfg.mapGenSettings),
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +0200106 },
107 },
108
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200109 deployment: kube.Deployment(factorio.makeName("factorio")) {
110 metadata+: factorio.metadata,
111 spec+: {
112 replicas: 1,
113 template+: {
114 spec+: {
115 volumes_: {
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100116 data: kube.PersistentVolumeClaimVolume(factorio.volumeClaimData),
117 mods: kube.PersistentVolumeClaimVolume(factorio.volumeClaimMods),
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +0200118 config: kube.ConfigMapVolume(factorio.configMap),
119 },
120 initContainers_: {
121 modproxy: kube.Container("modproxy") {
122 image: cfg.proxyImage,
123 command: [
124 "/games/factorio/modproxy/client",
125 "-hspki_disable",
126 "-factorio_path", "/factorio",
127 "-proxy", "proxy.factorio.svc.cluster.local:4200",
128 "-config_path", "/factorio/mods.pb.text",
129 ],
130 volumeMounts_: {
131 mods: { mountPath: "/factorio/mods" },
132 config: { mountPath: "/factorio/mods.pb.text", subPath: "mods.pb.text" },
133 },
134 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200135 },
136 containers_: {
137 factorio: kube.Container(factorio.makeName("factorio")) {
138 image: cfg.image,
139 args: [
140 "/entrypoint.sh",
141 "--rcon-port", std.toString(cfg.rconPort),
142 "--rcon-password", cfg.rconPassword,
Serge Bazanski08080342021-06-21 14:05:56 +0000143 "--server-settings", "/factorio/config/server-settings.json",
144 "--map-settings", "/factorio/config/map-settings.json",
145 "--map-gen-settings", "/factorio/config/map-gen-settings.json",
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200146 ],
147 ports_: {
148 client: { containerPort: 34197 },
149 rcon: { containerPort: cfg.rconPort },
150 },
151 volumeMounts_: {
152 data: { mountPath: "/data" },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100153 mods: { mountPath: "/factorio/mods" },
Serge Bazanski08080342021-06-21 14:05:56 +0000154 config: { mountPath: "/factorio/config" },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200155 },
156 resources: cfg.resources,
157 },
158 },
159 },
160 },
161 },
162 },
163 svc: kube.Service(factorio.makeName("factorio")) {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +0200164 metadata+: factorio.metadata {
165 // hack - have to keep existing naming scheme otherwise we'd lose addresses
166 labels: {
167 "app.kubernetes.io/name": cfg.appName,
168 },
169 },
radex8b8f3872023-11-24 11:09:46 +0100170 target:: factorio.deployment,
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200171 spec+: {
172 ports: [
173 { name: "client", port: 34197, targetPort: 34197, protocol: "UDP" },
174 ],
175 type: "LoadBalancer",
176 },
177 },
178}