blob: 6684fc0dcfe5444ecd77df19a8a3f6e2f7aefec4 [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
20 tag: "latest",
Serge Bazanski970b7682020-08-04 20:33:17 +020021 image: "registry.k0.hswaw.net/q3k/factorio:" + cfg.tag,
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020022 resources: {
23 requests: {
24 cpu: "500m",
25 memory: "500Mi",
26 },
27 limits: {
28 cpu: "1",
29 memory: "1Gi",
30 },
31 },
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020032
33 mods: [],
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020034 },
35
36
37 makeName(suffix):: cfg.prefix + suffix,
38
39 metadata:: {
40 namespace: cfg.namespace,
41 labels: {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +020042 "app.kubernetes.io/name": factorio.makeName("factorio"),
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020043 "app.kubernetes.io/managed-by": "kubecfg",
44 "app.kubernetes.io/component": "factorio",
45 },
46 },
47
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010048 volumeClaimMods: kube.PersistentVolumeClaim(factorio.makeName("factorio-mods")) {
49 metadata+: factorio.metadata,
50 spec+: {
51 storageClassName: cfg.storageClassName,
52 accessModes: [ "ReadWriteOnce" ],
53 resources: {
54 requests: {
55 storage: "1Gi",
56 },
57 },
58 },
59 },
60
61 volumeClaimData: kube.PersistentVolumeClaim(factorio.makeName("factorio")) {
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020062 metadata+: factorio.metadata,
63 spec+: {
64 storageClassName: cfg.storageClassName,
65 accessModes: [ "ReadWriteOnce" ],
66 resources: {
67 requests: {
68 storage: "5Gi",
69 },
70 },
71 },
72 },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010073
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020074 configMap: kube.ConfigMap(factorio.makeName("config")) {
75 metadata+: factorio.metadata,
76 data: {
77 "mods.pb.text": std.join("\n", [
78 "mod { name: \"%s\" version: \"%s\" }" % [m.name, m.version],
79 for m in cfg.mods
80 ]),
81 },
82 },
83
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020084 deployment: kube.Deployment(factorio.makeName("factorio")) {
85 metadata+: factorio.metadata,
86 spec+: {
87 replicas: 1,
88 template+: {
89 spec+: {
90 volumes_: {
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010091 data: kube.PersistentVolumeClaimVolume(factorio.volumeClaimData),
92 mods: kube.PersistentVolumeClaimVolume(factorio.volumeClaimMods),
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020093 config: kube.ConfigMapVolume(factorio.configMap),
94 },
95 initContainers_: {
96 modproxy: kube.Container("modproxy") {
97 image: cfg.proxyImage,
98 command: [
99 "/games/factorio/modproxy/client",
100 "-hspki_disable",
101 "-factorio_path", "/factorio",
102 "-proxy", "proxy.factorio.svc.cluster.local:4200",
103 "-config_path", "/factorio/mods.pb.text",
104 ],
105 volumeMounts_: {
106 mods: { mountPath: "/factorio/mods" },
107 config: { mountPath: "/factorio/mods.pb.text", subPath: "mods.pb.text" },
108 },
109 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200110 },
111 containers_: {
112 factorio: kube.Container(factorio.makeName("factorio")) {
113 image: cfg.image,
114 args: [
115 "/entrypoint.sh",
116 "--rcon-port", std.toString(cfg.rconPort),
117 "--rcon-password", cfg.rconPassword,
118 ],
119 ports_: {
120 client: { containerPort: 34197 },
121 rcon: { containerPort: cfg.rconPort },
122 },
123 volumeMounts_: {
124 data: { mountPath: "/data" },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100125 mods: { mountPath: "/factorio/mods" },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200126 },
127 resources: cfg.resources,
128 },
129 },
130 },
131 },
132 },
133 },
134 svc: kube.Service(factorio.makeName("factorio")) {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +0200135 metadata+: factorio.metadata {
136 // hack - have to keep existing naming scheme otherwise we'd lose addresses
137 labels: {
138 "app.kubernetes.io/name": cfg.appName,
139 },
140 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200141 target_pod:: factorio.deployment.spec.template,
142 spec+: {
143 ports: [
144 { name: "client", port: 34197, targetPort: 34197, protocol: "UDP" },
145 ],
146 type: "LoadBalancer",
147 },
148 },
149}