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" |
| 91 | cd /home/steam/valheim || exit 1 |
| 92 | exec "$@" |
| 93 | ||| |
| 94 | }, |
| 95 | }, |
| 96 | secret: ns.Contain(kube.Secret(named("game"))) { |
| 97 | data_: { |
| 98 | # public game password |
| 99 | public: cfg.password, |
| 100 | }, |
| 101 | }, |
| 102 | deployment: ns.Contain(kube.Deployment(named("game"))) { |
| 103 | spec+: { |
| 104 | template+: { |
| 105 | spec+: { |
| 106 | containers_: { |
| 107 | default: kube.Container("default") { |
| 108 | image: cfg.image, |
| 109 | command: [ |
| 110 | "/bin/bash", "/scripts/entrypoint.sh", "/home/steam/scripts/start_valheim.sh", |
| 111 | ], |
| 112 | volumeMounts_: { |
| 113 | backups: { mountPath: "/home/steam/backups" }, |
| 114 | saves: { mountPath: "/home/steam/.config/unity3d/IronGate/Valheim" }, |
| 115 | server: { mountPath: "/home/steam/valheim" }, |
| 116 | scripts: { mountPath: "/scripts" }, |
| 117 | }, |
| 118 | ports_: { |
| 119 | zero: { containerPort: cfg.port }, |
| 120 | one: { containerPort: cfg.port + 1 }, |
| 121 | two: { containerPort: cfg.port + 2 }, |
| 122 | }, |
| 123 | env_: { |
| 124 | PUBLIC: "1", |
| 125 | PASSWORD: kube.SecretKeyRef(game.secret, "public"), |
| 126 | NAME: cfg.displayName, |
Serge Bazanski | b4de3f2 | 2021-02-25 13:22:27 +0100 | [diff] [blame] | 127 | # Always attempt to update valheim on startup. |
| 128 | FORCE_INSTALL: "1", |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 129 | }, |
| 130 | resources: { |
| 131 | requests: { |
| 132 | cpu: "500m", |
| 133 | memory: "2Gi", |
| 134 | }, |
| 135 | limits: { |
| 136 | cpu: "1000m", |
| 137 | memory: "4Gi", |
| 138 | }, |
| 139 | }, |
| 140 | }, |
| 141 | }, |
| 142 | securityContext: { |
| 143 | runAsUser: 1000, |
| 144 | runAsGroup: 1000, |
| 145 | fsGroup: 1000, |
| 146 | }, |
| 147 | volumes_: { |
| 148 | backups: kube.PersistentVolumeClaimVolume(game.pvcs.backups), |
| 149 | saves: kube.PersistentVolumeClaimVolume(game.pvcs.saves), |
| 150 | server: kube.PersistentVolumeClaimVolume(game.pvcs.server), |
| 151 | scripts: kube.ConfigMapVolume(game.scripts), |
| 152 | }, |
| 153 | }, |
| 154 | }, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | |
Serge Bazanski | 2371ca9 | 2021-02-25 12:05:58 +0100 | [diff] [blame] | 160 | # Make namespace for valheim. |
| 161 | ns: kube.Namespace("valheim"), |
| 162 | |
| 163 | # Allow patryk and palid to administer this namespace via the namespace-admin clusterrole. |
| 164 | adminRB: top.ns.Contain(kube.RoleBinding("sso:admins")) { |
| 165 | subjects: [ |
| 166 | { apiGroup: "rbac.authorization.k8s.io", kind: "User", name: "%s@hackerspace.pl" % [u] } |
| 167 | for u in ["patryk", "palid"] |
| 168 | ], |
| 169 | roleRef: { |
| 170 | apiGroup: "rbac.authorization.k8s.io", |
| 171 | kind: "ClusterRole", |
| 172 | name: "system:admin-namespace", |
| 173 | }, |
Serge Bazanski | 34d39cc | 2021-02-23 23:03:31 +0000 | [diff] [blame] | 174 | }, |
| 175 | |
| 176 | q3k: top.env(top.ns, "q3k") { |
| 177 | cfg+: { |
| 178 | ns: "valheim", |
| 179 | password: (std.split(importstr "secrets/plain/q3k-public", "\n"))[0], |
| 180 | displayName: "wypierdol z polski xD", |
| 181 | }, |
| 182 | }, |
| 183 | } |