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 | |
Serge Bazanski | 0808034 | 2021-06-21 14:05:56 +0000 | [diff] [blame] | 20 | // 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 56 | tag: "latest", |
Serge Bazanski | 970b768 | 2020-08-04 20:33:17 +0200 | [diff] [blame] | 57 | image: "registry.k0.hswaw.net/q3k/factorio:" + cfg.tag, |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 58 | resources: { |
| 59 | requests: { |
| 60 | cpu: "500m", |
| 61 | memory: "500Mi", |
| 62 | }, |
| 63 | limits: { |
Serge Bazanski | 9c7e539 | 2021-06-18 21:24:07 +0200 | [diff] [blame] | 64 | cpu: "2", |
| 65 | memory: "2Gi", |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 66 | }, |
| 67 | }, |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 68 | |
| 69 | mods: [], |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 70 | }, |
| 71 | |
| 72 | |
| 73 | makeName(suffix):: cfg.prefix + suffix, |
| 74 | |
| 75 | metadata:: { |
| 76 | namespace: cfg.namespace, |
| 77 | labels: { |
Sergiusz Bazanski | 6e985c4 | 2020-04-01 02:21:45 +0200 | [diff] [blame] | 78 | "app.kubernetes.io/name": factorio.makeName("factorio"), |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 79 | "app.kubernetes.io/managed-by": "kubecfg", |
| 80 | "app.kubernetes.io/component": "factorio", |
| 81 | }, |
| 82 | }, |
| 83 | |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 84 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 98 | metadata+: factorio.metadata, |
| 99 | spec+: { |
| 100 | storageClassName: cfg.storageClassName, |
| 101 | accessModes: [ "ReadWriteOnce" ], |
| 102 | resources: { |
| 103 | requests: { |
| 104 | storage: "5Gi", |
| 105 | }, |
| 106 | }, |
| 107 | }, |
| 108 | }, |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 109 | |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 110 | 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 Bazanski | 0808034 | 2021-06-21 14:05:56 +0000 | [diff] [blame] | 117 | "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 Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 120 | }, |
| 121 | }, |
| 122 | |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 123 | deployment: kube.Deployment(factorio.makeName("factorio")) { |
| 124 | metadata+: factorio.metadata, |
| 125 | spec+: { |
| 126 | replicas: 1, |
| 127 | template+: { |
| 128 | spec+: { |
| 129 | volumes_: { |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 130 | data: kube.PersistentVolumeClaimVolume(factorio.volumeClaimData), |
| 131 | mods: kube.PersistentVolumeClaimVolume(factorio.volumeClaimMods), |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 132 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 149 | }, |
| 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 Bazanski | 0808034 | 2021-06-21 14:05:56 +0000 | [diff] [blame] | 157 | "--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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 160 | ], |
| 161 | ports_: { |
| 162 | client: { containerPort: 34197 }, |
| 163 | rcon: { containerPort: cfg.rconPort }, |
| 164 | }, |
| 165 | volumeMounts_: { |
| 166 | data: { mountPath: "/data" }, |
Sergiusz Bazanski | 083b176 | 2020-01-22 21:48:22 +0100 | [diff] [blame] | 167 | mods: { mountPath: "/factorio/mods" }, |
Serge Bazanski | 0808034 | 2021-06-21 14:05:56 +0000 | [diff] [blame] | 168 | config: { mountPath: "/factorio/config" }, |
Sergiusz Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 169 | }, |
| 170 | resources: cfg.resources, |
| 171 | }, |
| 172 | }, |
| 173 | }, |
| 174 | }, |
| 175 | }, |
| 176 | }, |
| 177 | svc: kube.Service(factorio.makeName("factorio")) { |
Sergiusz Bazanski | 6e985c4 | 2020-04-01 02:21:45 +0200 | [diff] [blame] | 178 | 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 Bazanski | cb5c321 | 2019-05-19 03:10:17 +0200 | [diff] [blame] | 184 | 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 | } |