blob: e83d216bf0857bcc901e9825429341a2df317164 [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"))) {
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,
127 },
128 resources: {
129 requests: {
130 cpu: "500m",
131 memory: "2Gi",
132 },
133 limits: {
134 cpu: "1000m",
135 memory: "4Gi",
136 },
137 },
138 },
139 },
140 securityContext: {
141 runAsUser: 1000,
142 runAsGroup: 1000,
143 fsGroup: 1000,
144 },
145 volumes_: {
146 backups: kube.PersistentVolumeClaimVolume(game.pvcs.backups),
147 saves: kube.PersistentVolumeClaimVolume(game.pvcs.saves),
148 server: kube.PersistentVolumeClaimVolume(game.pvcs.server),
149 scripts: kube.ConfigMapVolume(game.scripts),
150 },
151 },
152 },
153 },
154 },
155 },
156 },
157
Serge Bazanski2371ca92021-02-25 12:05:58 +0100158 # Make namespace for valheim.
159 ns: kube.Namespace("valheim"),
160
161 # Allow patryk and palid to administer this namespace via the namespace-admin clusterrole.
162 adminRB: top.ns.Contain(kube.RoleBinding("sso:admins")) {
163 subjects: [
164 { apiGroup: "rbac.authorization.k8s.io", kind: "User", name: "%s@hackerspace.pl" % [u] }
165 for u in ["patryk", "palid"]
166 ],
167 roleRef: {
168 apiGroup: "rbac.authorization.k8s.io",
169 kind: "ClusterRole",
170 name: "system:admin-namespace",
171 },
Serge Bazanski34d39cc2021-02-23 23:03:31 +0000172 },
173
174 q3k: top.env(top.ns, "q3k") {
175 cfg+: {
176 ns: "valheim",
177 password: (std.split(importstr "secrets/plain/q3k-public", "\n"))[0],
178 displayName: "wypierdol z polski xD",
179 },
180 },
181}