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