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"))) { |
| 23 | spec+: { |
| 24 | storageClassName: cfg.storageClassName, |
| 25 | accessModes: ["ReadWriteOnce"], |
| 26 | resources: { |
| 27 | requests: { storage: "10Gi" }, |
| 28 | }, |
| 29 | }, |
| 30 | }, |
| 31 | saves: ns.Contain(kube.PersistentVolumeClaim(named("saves"))) { |
| 32 | spec+: { |
| 33 | storageClassName: cfg.storageClassName, |
| 34 | accessModes: ["ReadWriteOnce"], |
| 35 | resources: { |
| 36 | requests: { storage: "10Gi" }, |
| 37 | }, |
| 38 | }, |
| 39 | }, |
| 40 | server: ns.Contain(kube.PersistentVolumeClaim(named("server"))) { |
| 41 | spec+: { |
| 42 | storageClassName: cfg.storageClassName, |
| 43 | accessModes: ["ReadWriteOnce"], |
| 44 | resources: { |
| 45 | requests: { storage: "10Gi" }, |
| 46 | }, |
| 47 | }, |
| 48 | }, |
| 49 | }, |
| 50 | svc: ns.Contain(kube.Service(named("external"))) { |
| 51 | target_pod:: game.deployment.spec.template, |
| 52 | spec+: { |
| 53 | ports: kube.mapToNamedList({ |
| 54 | zero: { port: cfg.port, targetPort: cfg.port, protocol: "UDP" }, |
| 55 | one: { port: cfg.port+1, targetPort: cfg.port+1, protocol: "UDP" }, |
| 56 | two: { port: cfg.port+2, targetPort: cfg.port+2, protocol: "UDP" }, |
| 57 | }), |
| 58 | type: "LoadBalancer", |
| 59 | }, |
| 60 | }, |
| 61 | |
| 62 | scripts: ns.Contain(kube.ConfigMap(named("scripts"))) { |
| 63 | data: { |
| 64 | # Based on https://github.com/mbround18/valheim-docker , |
| 65 | # removed all reliance on running as root (thus removed |
| 66 | # autoupdater/autobackups). |
| 67 | "entrypoint.sh": ||| |
| 68 | #!/usr/bin/env bash |
| 69 | log() { |
| 70 | PREFIX="[entrypoint]" |
| 71 | printf "%-16s: %s\n" "${PREFIX}" "$1" |
| 72 | } |
| 73 | line() { |
| 74 | log "===========================================================================" |
| 75 | } |
| 76 | setup_filesystem() { |
| 77 | log "Setting up file systems" |
| 78 | mkdir -p /home/steam/valheim |
| 79 | mkdir -p /home/steam/valheim/logs |
| 80 | mkdir -p /home/steam/backups |
| 81 | mkdir -p /home/steam/scripts |
| 82 | mkdir -p /home/steam/valheim |
| 83 | cp /home/steam/steamcmd/linux64/steamclient.so /home/steam/valheim |
| 84 | } |
| 85 | line |
| 86 | log "Valheim Server - $(date)" |
| 87 | log "Initializing your container..." |
| 88 | line |
| 89 | setup_filesystem |
| 90 | log "Launching the rest of the fucking owl" |
Serge Bazanski | 5edcf58 | 2021-12-13 16:51:14 +0000 | [diff] [blame] | 91 | export HOME=/home/steam |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 92 | cd /home/steam/valheim || exit 1 |
| 93 | exec "$@" |
| 94 | ||| |
| 95 | }, |
| 96 | }, |
| 97 | secret: ns.Contain(kube.Secret(named("game"))) { |
| 98 | data_: { |
| 99 | # public game password |
| 100 | public: cfg.password, |
| 101 | }, |
| 102 | }, |
| 103 | deployment: ns.Contain(kube.Deployment(named("game"))) { |
| 104 | spec+: { |
| 105 | template+: { |
| 106 | spec+: { |
| 107 | containers_: { |
| 108 | default: kube.Container("default") { |
| 109 | image: cfg.image, |
| 110 | command: [ |
| 111 | "/bin/bash", "/scripts/entrypoint.sh", "/home/steam/scripts/start_valheim.sh", |
| 112 | ], |
| 113 | volumeMounts_: { |
| 114 | backups: { mountPath: "/home/steam/backups" }, |
| 115 | saves: { mountPath: "/home/steam/.config/unity3d/IronGate/Valheim" }, |
| 116 | server: { mountPath: "/home/steam/valheim" }, |
| 117 | scripts: { mountPath: "/scripts" }, |
| 118 | }, |
| 119 | ports_: { |
| 120 | zero: { containerPort: cfg.port }, |
| 121 | one: { containerPort: cfg.port + 1 }, |
| 122 | two: { containerPort: cfg.port + 2 }, |
| 123 | }, |
| 124 | env_: { |
| 125 | PUBLIC: "1", |
| 126 | PASSWORD: kube.SecretKeyRef(game.secret, "public"), |
| 127 | NAME: cfg.displayName, |
Serge Bazanski | b4de3f2 | 2021-02-25 13:22:27 +0100 | [diff] [blame] | 128 | # Always attempt to update valheim on startup. |
| 129 | FORCE_INSTALL: "1", |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 130 | }, |
| 131 | resources: { |
| 132 | requests: { |
| 133 | cpu: "500m", |
| 134 | memory: "2Gi", |
| 135 | }, |
| 136 | limits: { |
| 137 | cpu: "1000m", |
| 138 | memory: "4Gi", |
| 139 | }, |
| 140 | }, |
| 141 | }, |
| 142 | }, |
| 143 | securityContext: { |
| 144 | runAsUser: 1000, |
| 145 | runAsGroup: 1000, |
| 146 | fsGroup: 1000, |
| 147 | }, |
| 148 | volumes_: { |
| 149 | backups: kube.PersistentVolumeClaimVolume(game.pvcs.backups), |
| 150 | saves: kube.PersistentVolumeClaimVolume(game.pvcs.saves), |
| 151 | server: kube.PersistentVolumeClaimVolume(game.pvcs.server), |
| 152 | scripts: kube.ConfigMapVolume(game.scripts), |
| 153 | }, |
| 154 | }, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | }, |
| 160 | |
Serge Bazanski | 2371ca9 | 2021-02-25 12:05:58 +0100 | [diff] [blame] | 161 | # Make namespace for valheim. |
| 162 | ns: kube.Namespace("valheim"), |
| 163 | |
| 164 | # Allow patryk and palid to administer this namespace via the namespace-admin clusterrole. |
| 165 | adminRB: top.ns.Contain(kube.RoleBinding("sso:admins")) { |
| 166 | subjects: [ |
| 167 | { apiGroup: "rbac.authorization.k8s.io", kind: "User", name: "%s@hackerspace.pl" % [u] } |
| 168 | for u in ["patryk", "palid"] |
| 169 | ], |
| 170 | roleRef: { |
| 171 | apiGroup: "rbac.authorization.k8s.io", |
| 172 | kind: "ClusterRole", |
| 173 | name: "system:admin-namespace", |
| 174 | }, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 175 | }, |
| 176 | |
| 177 | q3k: top.env(top.ns, "q3k") { |
| 178 | cfg+: { |
| 179 | ns: "valheim", |
| 180 | password: (std.split(importstr "secrets/plain/q3k-public", "\n"))[0], |
| 181 | displayName: "wypierdol z polski xD", |
| 182 | }, |
| 183 | }, |
Serge Bazanski | 5edcf58 | 2021-12-13 16:51:14 +0000 | [diff] [blame] | 184 | q3k2: top.env(top.ns, "q3k2") { |
| 185 | cfg+: { |
| 186 | ns: "valheim", |
| 187 | password: (std.split(importstr "secrets/plain/q3k2-public", "\n"))[0], |
| 188 | displayName: "walhajm", |
| 189 | }, |
| 190 | }, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 191 | } |