blob: 4f2ff3949b6133bd5701e94ccabfd71dc345adb8 [file] [log] [blame]
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +02001# Factorio on Kubernetes.
2
Sergiusz Bazanski7fb4acb2019-07-21 17:02:12 +02003local kube = import "../../../../kube/kube.libsonnet";
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +02004
5{
6 local factorio = self,
7 local cfg = factorio.cfg,
8
9 cfg:: {
10 namespace: error "namespace must be set",
11 appName: "factorio",
Sergiusz Bazanskiddfd6592019-08-09 18:24:00 +020012 storageClassName: "waw-hdd-redundant-2",
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020013 prefix: "", # if set, should be 'foo-'
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020014 proxyImage: error "proxyImage must be set",
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020015
16 rconPort: 2137,
17 rconPassword: "farts",
18
19 tag: "latest",
Serge Bazanski970b7682020-08-04 20:33:17 +020020 image: "registry.k0.hswaw.net/q3k/factorio:" + cfg.tag,
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020021 resources: {
22 requests: {
23 cpu: "500m",
24 memory: "500Mi",
25 },
26 limits: {
27 cpu: "1",
28 memory: "1Gi",
29 },
30 },
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020031
32 mods: [],
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020033 },
34
35
36 makeName(suffix):: cfg.prefix + suffix,
37
38 metadata:: {
39 namespace: cfg.namespace,
40 labels: {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +020041 "app.kubernetes.io/name": factorio.makeName("factorio"),
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020042 "app.kubernetes.io/managed-by": "kubecfg",
43 "app.kubernetes.io/component": "factorio",
44 },
45 },
46
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010047 volumeClaimMods: kube.PersistentVolumeClaim(factorio.makeName("factorio-mods")) {
48 metadata+: factorio.metadata,
49 spec+: {
50 storageClassName: cfg.storageClassName,
51 accessModes: [ "ReadWriteOnce" ],
52 resources: {
53 requests: {
54 storage: "1Gi",
55 },
56 },
57 },
58 },
59
60 volumeClaimData: kube.PersistentVolumeClaim(factorio.makeName("factorio")) {
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020061 metadata+: factorio.metadata,
62 spec+: {
63 storageClassName: cfg.storageClassName,
64 accessModes: [ "ReadWriteOnce" ],
65 resources: {
66 requests: {
67 storage: "5Gi",
68 },
69 },
70 },
71 },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010072
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020073 configMap: kube.ConfigMap(factorio.makeName("config")) {
74 metadata+: factorio.metadata,
75 data: {
76 "mods.pb.text": std.join("\n", [
77 "mod { name: \"%s\" version: \"%s\" }" % [m.name, m.version],
78 for m in cfg.mods
79 ]),
80 },
81 },
82
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +020083 deployment: kube.Deployment(factorio.makeName("factorio")) {
84 metadata+: factorio.metadata,
85 spec+: {
86 replicas: 1,
87 template+: {
88 spec+: {
89 volumes_: {
Sergiusz Bazanski083b1762020-01-22 21:48:22 +010090 data: kube.PersistentVolumeClaimVolume(factorio.volumeClaimData),
91 mods: kube.PersistentVolumeClaimVolume(factorio.volumeClaimMods),
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +020092 config: kube.ConfigMapVolume(factorio.configMap),
93 },
94 initContainers_: {
95 modproxy: kube.Container("modproxy") {
96 image: cfg.proxyImage,
97 command: [
98 "/games/factorio/modproxy/client",
99 "-hspki_disable",
100 "-factorio_path", "/factorio",
101 "-proxy", "proxy.factorio.svc.cluster.local:4200",
102 "-config_path", "/factorio/mods.pb.text",
103 ],
104 volumeMounts_: {
105 mods: { mountPath: "/factorio/mods" },
106 config: { mountPath: "/factorio/mods.pb.text", subPath: "mods.pb.text" },
107 },
108 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200109 },
110 containers_: {
111 factorio: kube.Container(factorio.makeName("factorio")) {
112 image: cfg.image,
113 args: [
114 "/entrypoint.sh",
115 "--rcon-port", std.toString(cfg.rconPort),
116 "--rcon-password", cfg.rconPassword,
117 ],
118 ports_: {
119 client: { containerPort: 34197 },
120 rcon: { containerPort: cfg.rconPort },
121 },
122 volumeMounts_: {
123 data: { mountPath: "/data" },
Sergiusz Bazanski083b1762020-01-22 21:48:22 +0100124 mods: { mountPath: "/factorio/mods" },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200125 },
126 resources: cfg.resources,
127 },
128 },
129 },
130 },
131 },
132 },
133 svc: kube.Service(factorio.makeName("factorio")) {
Sergiusz Bazanski6e985c42020-04-01 02:21:45 +0200134 metadata+: factorio.metadata {
135 // hack - have to keep existing naming scheme otherwise we'd lose addresses
136 labels: {
137 "app.kubernetes.io/name": cfg.appName,
138 },
139 },
Sergiusz Bazanskicb5c3212019-05-19 03:10:17 +0200140 target_pod:: factorio.deployment.spec.template,
141 spec+: {
142 ports: [
143 { name: "client", port: 34197, targetPort: 34197, protocol: "UDP" },
144 ],
145 type: "LoadBalancer",
146 },
147 },
148}