Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 1 | # Factorio on Kubernetes. |
| 2 | |
Dariusz Niemczyk | b3799c8 | 2021-06-12 23:02:58 +0200 | [diff] [blame] | 3 | local kube = import "../../../kube/kube.libsonnet"; |
Serge Bazanski | c6cc561 | 2021-06-13 21:55:18 +0000 | [diff] [blame^] | 4 | local proxy = import "proxy.libsonnet"; |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 5 | |
| 6 | { |
| 7 | local factorio = self, |
| 8 | local cfg = factorio.cfg, |
| 9 | |
| 10 | cfg:: { |
| 11 | namespace: error "namespace must be set", |
| 12 | appName: "factorio", |
Serge Bazanski | 50a041d | 2021-06-13 21:04:54 +0000 | [diff] [blame] | 13 | storageClassName: "waw-hdd-redundant-3", |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 14 | prefix: "", # if set, should be 'foo-' |
Serge Bazanski | c6cc561 | 2021-06-13 21:55:18 +0000 | [diff] [blame^] | 15 | proxyImage: proxy.cfg.image, |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 16 | |
| 17 | rconPort: 2137, |
| 18 | rconPassword: "farts", |
| 19 | |
| 20 | tag: "latest", |
Serge Bazanski | 970b768 | 2020-08-04 20:33:17 +0200 | [diff] [blame] | 21 | image: "registry.k0.hswaw.net/q3k/factorio:" + cfg.tag, |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 22 | resources: { |
| 23 | requests: { |
| 24 | cpu: "500m", |
| 25 | memory: "500Mi", |
| 26 | }, |
| 27 | limits: { |
| 28 | cpu: "1", |
| 29 | memory: "1Gi", |
| 30 | }, |
| 31 | }, |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 32 | |
| 33 | mods: [], |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 34 | }, |
| 35 | |
| 36 | |
| 37 | makeName(suffix):: cfg.prefix + suffix, |
| 38 | |
| 39 | metadata:: { |
| 40 | namespace: cfg.namespace, |
| 41 | labels: { |
Sergiusz Bazanski | 6e985c4 | 2020-04-01 02:21:45 +0200 | [diff] [blame] | 42 | "app.kubernetes.io/name": factorio.makeName("factorio"), |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 43 | "app.kubernetes.io/managed-by": "kubecfg", |
| 44 | "app.kubernetes.io/component": "factorio", |
| 45 | }, |
| 46 | }, |
| 47 | |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 48 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 62 | metadata+: factorio.metadata, |
| 63 | spec+: { |
| 64 | storageClassName: cfg.storageClassName, |
| 65 | accessModes: [ "ReadWriteOnce" ], |
| 66 | resources: { |
| 67 | requests: { |
| 68 | storage: "5Gi", |
| 69 | }, |
| 70 | }, |
| 71 | }, |
| 72 | }, |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 73 | |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 74 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 84 | deployment: kube.Deployment(factorio.makeName("factorio")) { |
| 85 | metadata+: factorio.metadata, |
| 86 | spec+: { |
| 87 | replicas: 1, |
| 88 | template+: { |
| 89 | spec+: { |
| 90 | volumes_: { |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 91 | data: kube.PersistentVolumeClaimVolume(factorio.volumeClaimData), |
| 92 | mods: kube.PersistentVolumeClaimVolume(factorio.volumeClaimMods), |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 93 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 110 | }, |
| 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 Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 125 | mods: { mountPath: "/factorio/mods" }, |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 126 | }, |
| 127 | resources: cfg.resources, |
| 128 | }, |
| 129 | }, |
| 130 | }, |
| 131 | }, |
| 132 | }, |
| 133 | }, |
| 134 | svc: kube.Service(factorio.makeName("factorio")) { |
Sergiusz Bazanski | 6e985c4 | 2020-04-01 02:21:45 +0200 | [diff] [blame] | 135 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 141 | 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 | } |