Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 1 | local kube = import "../../kube/kube.libsonnet"; |
| 2 | |
| 3 | { |
| 4 | local top = self, |
| 5 | env(ns, name):: { |
| 6 | local env = self, |
| 7 | local cfg = env.cfg, |
| 8 | cfg:: { |
| 9 | name: name, |
| 10 | displayName: name, |
| 11 | image: "mbround18/valheim:latest", |
| 12 | password: error "password must be set", |
| 13 | storageClassName: "waw-hdd-redundant-3", |
| 14 | port: 2456, |
| 15 | }, |
| 16 | |
| 17 | local named = function(component) "%s-%s" % [name, component], |
| 18 | |
| 19 | game: { |
| 20 | local game = self, |
| 21 | pvcs: { |
| 22 | backups: ns.Contain(kube.PersistentVolumeClaim(named("backups"))) { |
radex | 36964dc | 2023-11-24 11:19:46 +0100 | [diff] [blame] | 23 | storage:: "10Gi", |
| 24 | storageClass:: cfg.storageClassName, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 25 | }, |
| 26 | saves: ns.Contain(kube.PersistentVolumeClaim(named("saves"))) { |
radex | 36964dc | 2023-11-24 11:19:46 +0100 | [diff] [blame] | 27 | storage:: "10Gi", |
| 28 | storageClass:: cfg.storageClassName, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 29 | }, |
| 30 | server: ns.Contain(kube.PersistentVolumeClaim(named("server"))) { |
radex | 36964dc | 2023-11-24 11:19:46 +0100 | [diff] [blame] | 31 | storage:: "10Gi", |
| 32 | storageClass:: cfg.storageClassName, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 33 | }, |
| 34 | }, |
| 35 | svc: ns.Contain(kube.Service(named("external"))) { |
radex | 8b8f387 | 2023-11-24 11:09:46 +0100 | [diff] [blame] | 36 | target:: game.deployment, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 37 | spec+: { |
| 38 | ports: kube.mapToNamedList({ |
| 39 | zero: { port: cfg.port, targetPort: cfg.port, protocol: "UDP" }, |
| 40 | one: { port: cfg.port+1, targetPort: cfg.port+1, protocol: "UDP" }, |
| 41 | two: { port: cfg.port+2, targetPort: cfg.port+2, protocol: "UDP" }, |
| 42 | }), |
| 43 | type: "LoadBalancer", |
| 44 | }, |
| 45 | }, |
| 46 | |
Serge Bazanski | 3c9b825 | 2022-11-27 14:48:07 +0000 | [diff] [blame] | 47 | // Given to some external users/systems which manage a given valheim server in a namespace. |
| 48 | // TODO(q3k): only grant privileges to the same server |
| 49 | controlAccount: { |
| 50 | svcAccount: ns.Contain(kube.ServiceAccount(named("control"))), |
| 51 | role: ns.Contain(kube.Role("control")) { |
| 52 | rules: [ |
| 53 | { |
| 54 | apiGroups: [""], |
| 55 | resources: ["pods"], |
| 56 | verbs: ["get", "list", "watch", "delete"], |
| 57 | }, |
| 58 | { |
| 59 | apiGroups: [""], |
| 60 | resources: ["pods/log"], |
| 61 | verbs: ["get"], |
| 62 | }, |
| 63 | { |
| 64 | apiGroups: ["apps"], |
| 65 | resources: ["deployments"], |
| 66 | verbs: ["get", "list", "watch"], |
| 67 | }, |
| 68 | ], |
| 69 | }, |
| 70 | roleBinding: ns.Contain(kube.RoleBinding(named("control"))) { |
| 71 | subjects_: [ |
| 72 | game.controlAccount.svcAccount, |
| 73 | ], |
| 74 | roleRef_: game.controlAccount.role, |
| 75 | }, |
| 76 | }, |
| 77 | |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 78 | scripts: ns.Contain(kube.ConfigMap(named("scripts"))) { |
| 79 | data: { |
| 80 | # Based on https://github.com/mbround18/valheim-docker , |
| 81 | # removed all reliance on running as root (thus removed |
| 82 | # autoupdater/autobackups). |
| 83 | "entrypoint.sh": ||| |
| 84 | #!/usr/bin/env bash |
| 85 | log() { |
| 86 | PREFIX="[entrypoint]" |
| 87 | printf "%-16s: %s\n" "${PREFIX}" "$1" |
| 88 | } |
| 89 | line() { |
| 90 | log "===========================================================================" |
| 91 | } |
| 92 | setup_filesystem() { |
| 93 | log "Setting up file systems" |
| 94 | mkdir -p /home/steam/valheim |
| 95 | mkdir -p /home/steam/valheim/logs |
| 96 | mkdir -p /home/steam/backups |
| 97 | mkdir -p /home/steam/scripts |
| 98 | mkdir -p /home/steam/valheim |
| 99 | cp /home/steam/steamcmd/linux64/steamclient.so /home/steam/valheim |
| 100 | } |
| 101 | line |
| 102 | log "Valheim Server - $(date)" |
| 103 | log "Initializing your container..." |
| 104 | line |
| 105 | setup_filesystem |
| 106 | log "Launching the rest of the fucking owl" |
Serge Bazanski | 5edcf58 | 2021-12-13 16:51:14 +0000 | [diff] [blame] | 107 | export HOME=/home/steam |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 108 | cd /home/steam/valheim || exit 1 |
| 109 | exec "$@" |
| 110 | ||| |
| 111 | }, |
| 112 | }, |
| 113 | secret: ns.Contain(kube.Secret(named("game"))) { |
| 114 | data_: { |
| 115 | # public game password |
| 116 | public: cfg.password, |
| 117 | }, |
| 118 | }, |
| 119 | deployment: ns.Contain(kube.Deployment(named("game"))) { |
| 120 | spec+: { |
| 121 | template+: { |
| 122 | spec+: { |
| 123 | containers_: { |
| 124 | default: kube.Container("default") { |
| 125 | image: cfg.image, |
| 126 | command: [ |
| 127 | "/bin/bash", "/scripts/entrypoint.sh", "/home/steam/scripts/start_valheim.sh", |
| 128 | ], |
| 129 | volumeMounts_: { |
| 130 | backups: { mountPath: "/home/steam/backups" }, |
| 131 | saves: { mountPath: "/home/steam/.config/unity3d/IronGate/Valheim" }, |
| 132 | server: { mountPath: "/home/steam/valheim" }, |
| 133 | scripts: { mountPath: "/scripts" }, |
| 134 | }, |
| 135 | ports_: { |
| 136 | zero: { containerPort: cfg.port }, |
| 137 | one: { containerPort: cfg.port + 1 }, |
| 138 | two: { containerPort: cfg.port + 2 }, |
| 139 | }, |
| 140 | env_: { |
| 141 | PUBLIC: "1", |
| 142 | PASSWORD: kube.SecretKeyRef(game.secret, "public"), |
| 143 | NAME: cfg.displayName, |
Serge Bazanski | b4de3f2 | 2021-02-25 13:22:27 +0100 | [diff] [blame] | 144 | # Always attempt to update valheim on startup. |
| 145 | FORCE_INSTALL: "1", |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 146 | }, |
| 147 | resources: { |
| 148 | requests: { |
| 149 | cpu: "500m", |
| 150 | memory: "2Gi", |
| 151 | }, |
| 152 | limits: { |
| 153 | cpu: "1000m", |
| 154 | memory: "4Gi", |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | securityContext: { |
| 160 | runAsUser: 1000, |
| 161 | runAsGroup: 1000, |
| 162 | fsGroup: 1000, |
| 163 | }, |
| 164 | volumes_: { |
radex | 4ffc64d | 2023-11-24 13:28:57 +0100 | [diff] [blame] | 165 | backups: game.pvcs.backups.volume, |
| 166 | saves: game.pvcs.saves.volume, |
| 167 | server: game.pvcs.server.volume, |
| 168 | scripts: game.scripts.volume, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 169 | }, |
| 170 | }, |
| 171 | }, |
| 172 | }, |
| 173 | }, |
| 174 | }, |
| 175 | }, |
| 176 | |
Serge Bazanski | 2371ca9 | 2021-02-25 12:05:58 +0100 | [diff] [blame] | 177 | # Make namespace for valheim. |
| 178 | ns: kube.Namespace("valheim"), |
| 179 | |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 180 | q3k: top.env(top.ns, "q3k") { |
| 181 | cfg+: { |
| 182 | ns: "valheim", |
| 183 | password: (std.split(importstr "secrets/plain/q3k-public", "\n"))[0], |
| 184 | displayName: "wypierdol z polski xD", |
| 185 | }, |
| 186 | }, |
Serge Bazanski | 5edcf58 | 2021-12-13 16:51:14 +0000 | [diff] [blame] | 187 | q3k2: top.env(top.ns, "q3k2") { |
| 188 | cfg+: { |
| 189 | ns: "valheim", |
| 190 | password: (std.split(importstr "secrets/plain/q3k2-public", "\n"))[0], |
| 191 | displayName: "walhajm", |
| 192 | }, |
| 193 | }, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 194 | } |