blob: 0793e510b0e62340ef6ebb454e540d18f1cb36de [file] [log] [blame]
Serge Bazanski34d39cc2021-02-23 23:03:31 +00001local 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"))) {
radex36964dc2023-11-24 11:19:46 +010023 storage:: "10Gi",
24 storageClass:: cfg.storageClassName,
Serge Bazanski34d39cc2021-02-23 23:03:31 +000025 },
26 saves: ns.Contain(kube.PersistentVolumeClaim(named("saves"))) {
radex36964dc2023-11-24 11:19:46 +010027 storage:: "10Gi",
28 storageClass:: cfg.storageClassName,
Serge Bazanski34d39cc2021-02-23 23:03:31 +000029 },
30 server: ns.Contain(kube.PersistentVolumeClaim(named("server"))) {
radex36964dc2023-11-24 11:19:46 +010031 storage:: "10Gi",
32 storageClass:: cfg.storageClassName,
Serge Bazanski34d39cc2021-02-23 23:03:31 +000033 },
34 },
35 svc: ns.Contain(kube.Service(named("external"))) {
radex8b8f3872023-11-24 11:09:46 +010036 target:: game.deployment,
Serge Bazanski34d39cc2021-02-23 23:03:31 +000037 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 Bazanski3c9b8252022-11-27 14:48:07 +000047 // 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 Bazanski34d39cc2021-02-23 23:03:31 +000078 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 Bazanski5edcf582021-12-13 16:51:14 +0000107 export HOME=/home/steam
Serge Bazanski34d39cc2021-02-23 23:03:31 +0000108 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 Bazanskib4de3f22021-02-25 13:22:27 +0100144 # Always attempt to update valheim on startup.
145 FORCE_INSTALL: "1",
Serge Bazanski34d39cc2021-02-23 23:03:31 +0000146 },
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_: {
165 backups: kube.PersistentVolumeClaimVolume(game.pvcs.backups),
166 saves: kube.PersistentVolumeClaimVolume(game.pvcs.saves),
167 server: kube.PersistentVolumeClaimVolume(game.pvcs.server),
168 scripts: kube.ConfigMapVolume(game.scripts),
169 },
170 },
171 },
172 },
173 },
174 },
175 },
176
Serge Bazanski2371ca92021-02-25 12:05:58 +0100177 # Make namespace for valheim.
178 ns: kube.Namespace("valheim"),
179
Serge Bazanski34d39cc2021-02-23 23:03:31 +0000180 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 Bazanski5edcf582021-12-13 16:51:14 +0000187 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 Bazanski34d39cc2021-02-23 23:03:31 +0000194}