forked from ArtheonVR/veverse-server-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment.go
More file actions
328 lines (273 loc) · 8.74 KB
/
Copy pathdeployment.go
File metadata and controls
328 lines (273 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"context"
"fmt"
"github.com/gofrs/uuid"
appsV1 "k8s.io/api/apps/v1"
apiV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes"
)
func getGameServerDeploymentClusterResource(ctx context.Context, id uuid.UUID) (*appsV1.Deployment, error) {
clientset := ctx.Value("clientset").(*kubernetes.Clientset)
namespace := ctx.Value("namespace").(string)
resourceName := getResourceName(id)
deploymentsClient := clientset.AppsV1().Deployments(namespace)
deployment, err := deploymentsClient.Get(ctx, resourceName, metaV1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to get deployment: %v", err)
}
return deployment, nil
}
func createGameServerDeploymentClusterResource(ctx context.Context, metadata unstructured.Unstructured) error {
namespace, ok := ctx.Value("namespace").(string)
if !ok {
return fmt.Errorf("namespace not found in context")
}
clientset, ok := ctx.Value("clientset").(*kubernetes.Clientset)
if !ok {
return fmt.Errorf("clientset not found in context")
}
Logger.Infof("metadata: %v", metadata)
//region Specification
spec, ok := metadata.Object["spec"].(map[string]interface{})
if !ok {
return fmt.Errorf("spec not found in game server metadata")
}
//region ID
if _, ok := spec["id"].(string); !ok {
return fmt.Errorf("id not found in game server metadata")
}
id, err := uuid.FromString(spec["id"].(string))
if err != nil {
return fmt.Errorf("failed to parse id: %v", err)
}
resourceName := getResourceName(id)
//endregion
//region Environment Variables
var envs []apiV1.EnvVar
envSpec, ok := spec["env"].([]interface{})
if !ok {
return fmt.Errorf("envs not found in game server metadata")
}
for _, envMetadata := range envSpec {
env := envMetadata.(map[string]interface{})
envs = append(envs, apiV1.EnvVar{
Name: env["name"].(string),
Value: env["value"].(string),
})
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerId, Value: resourceName})
envs = append(envs, apiV1.EnvVar{Name: EnvServerName, Value: resourceName})
//endregion
//region Settings
settingSpec, ok := spec["settings"].(map[string]interface{})
if !ok {
return fmt.Errorf("settings not found in game server metadata")
}
//region API
apiSettingSpec, ok := settingSpec["api"].(map[string]interface{})
if !ok {
return fmt.Errorf("api settings not found in game server metadata")
}
//region V1
apiV1SettingSpec, ok := apiSettingSpec["v1"].(map[string]interface{})
if !ok {
return fmt.Errorf("v1 api settings not found in game server metadata")
}
apiV1Root, ok := apiV1SettingSpec["url"].(string)
if !ok {
return fmt.Errorf("v1 api root not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvApiV1Root, Value: apiV1Root})
apiV1Key, ok := apiV1SettingSpec["key"].(string)
if !ok {
return fmt.Errorf("v1 api key not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerApiV1Key, Value: apiV1Key})
//endregion
//region V2
apiV2SettingSpec, ok := apiSettingSpec["v2"].(map[string]interface{})
if !ok {
return fmt.Errorf("v2 api settings not found in game server metadata")
}
apiV2Root, ok := apiV2SettingSpec["url"].(string)
if !ok {
return fmt.Errorf("v2 api root not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvApiV2Root, Value: apiV2Root})
apiV2Email, ok := apiV2SettingSpec["email"].(string)
if !ok {
return fmt.Errorf("v2 api email not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerApiV2Email, Value: apiV2Email})
apiV2Password, ok := apiV2SettingSpec["password"].(string)
if !ok {
return fmt.Errorf("v2 api password not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerApiV2Password, Value: apiV2Password})
//endregion
//endregion
//region App
appSpec, ok := settingSpec["app"].(map[string]interface{})
if !ok {
return fmt.Errorf("app not found in game server metadata")
}
appId, ok := appSpec["id"].(string)
if !ok {
return fmt.Errorf("app id not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerAppId, Value: appId})
//endregion
//region Release
releaseSpec, ok := settingSpec["release"].(map[string]interface{})
if !ok {
return fmt.Errorf("release not found in game server metadata")
}
releaseId, ok := releaseSpec["id"].(string)
if !ok {
return fmt.Errorf("release id not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerReleaseId, Value: releaseId})
//endregion
//region Players
playerSettingSpec, ok := settingSpec["players"].(map[string]interface{})
if !ok {
return fmt.Errorf("player settings not found in game server metadata")
}
Logger.Infof("type of playerSettingSpec: %T", playerSettingSpec["max"])
maxPlayers, ok := playerSettingSpec["max"].(int64)
if !ok {
return fmt.Errorf("max players not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerMaxPlayers, Value: fmt.Sprintf("%d", maxPlayers)})
//endregion
//region World
worldSettingSpec, ok := settingSpec["world"].(map[string]interface{})
if !ok {
return fmt.Errorf("world settings not found in game server metadata")
}
worldId, ok := worldSettingSpec["id"].(string)
if !ok {
return fmt.Errorf("world id not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerWorldId, Value: worldId})
//endregion
//region Server
serverSettingSpec, ok := settingSpec["server"].(map[string]interface{})
if !ok {
return fmt.Errorf("server settings not found in game server metadata")
}
//region Host
serverHost, ok := serverSettingSpec["host"].(string)
if !ok {
return fmt.Errorf("server host not found in game server metadata")
}
envs = append(envs, apiV1.EnvVar{Name: EnvServerHost, Value: serverHost})
//endregion
//region Container Image
serverImage, ok := serverSettingSpec["image"].(string)
if !ok {
return fmt.Errorf("server image not found in game server metadata")
}
serverImagePullSecretsSpec, ok := serverSettingSpec["imagePullSecrets"].([]interface{})
if !ok {
return fmt.Errorf("server image pull secrets not found in game server metadata")
}
serverImagePullSecrets := make([]apiV1.LocalObjectReference, len(serverImagePullSecretsSpec))
for i, serverImagePullSecret := range serverImagePullSecretsSpec {
name, ok := serverImagePullSecret.(string)
if !ok {
return fmt.Errorf("server image pull secret is not a string")
}
serverImagePullSecrets[i] = apiV1.LocalObjectReference{Name: name}
}
//endregion
//endregion
//endregion
//endregion
deploymentsClient := clientset.AppsV1().Deployments(namespace)
deploymentResource := &appsV1.Deployment{
ObjectMeta: metaV1.ObjectMeta{
Name: resourceName,
Labels: map[string]string{
"app": resourceName,
},
},
Spec: appsV1.DeploymentSpec{
Replicas: int32Ptr(1),
Selector: &metaV1.LabelSelector{
MatchLabels: map[string]string{
"app": resourceName,
},
},
Template: apiV1.PodTemplateSpec{
ObjectMeta: metaV1.ObjectMeta{
Labels: map[string]string{
"app": resourceName,
},
},
Spec: apiV1.PodSpec{
ImagePullSecrets: serverImagePullSecrets,
Containers: []apiV1.Container{
{
Name: resourceName,
Env: envs,
Image: serverImage,
Ports: []apiV1.ContainerPort{
{
Name: "unreal",
ContainerPort: 7777,
Protocol: "UDP",
},
},
},
},
},
},
},
}
_, err = deploymentsClient.Create(ctx, deploymentResource, metaV1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create deployment: %v", err)
}
serviceClient := clientset.CoreV1().Services(namespace)
serviceResource := &apiV1.Service{
ObjectMeta: metaV1.ObjectMeta{
Name: resourceName,
Labels: map[string]string{
"app": resourceName,
},
},
Spec: apiV1.ServiceSpec{
Selector: map[string]string{
"app": resourceName,
},
Ports: []apiV1.ServicePort{
{
Name: "unreal",
Port: 7777,
Protocol: "UDP",
},
},
Type: "NodePort",
},
}
_, err = serviceClient.Create(ctx, serviceResource, metaV1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create service: %v", err)
}
return nil
}
func deleteGameServerDeploymentClusterResource(ctx context.Context, id uuid.UUID) error {
clientset := ctx.Value("clientset").(*kubernetes.Clientset)
namespace := ctx.Value("namespace").(string)
resourceName := getResourceName(id)
deploymentsClient := clientset.AppsV1().Deployments(namespace)
err := deploymentsClient.Delete(ctx, resourceName, metaV1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete deployment: %v", err)
}
return nil
}