This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
126 lines (109 loc) · 4.69 KB
/
Program.cs
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
using HiveMP.GameServer.Api;
using k8s;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace HiveMP.GameServer.RemoteAgent
{
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("This is the HiveMP game server remote agent.");
Console.WriteLine("Obtaining cluster credentials...");
var projectId = Environment.GetEnvironmentVariable("PROJECT_ID");
var clusterId = Environment.GetEnvironmentVariable("CLUSTER_ID");
var clusterHost = Environment.GetEnvironmentVariable("CLUSTER_HOST");
var clusterNamespace = Environment.GetEnvironmentVariable("CLUSTER_NAMESPACE");
var apiKey = Environment.GetEnvironmentVariable("API_KEY");
var hiveEndpoint = Environment.GetEnvironmentVariable("HIVEMP_ENDPOINT");
if (hiveEndpoint == null)
{
hiveEndpoint = "https://game-server-api.hivemp.com/v1";
}
Kubernetes client;
if (clusterHost != null)
{
var config = new KubernetesClientConfiguration { Host = clusterHost };
client = new Kubernetes(config);
}
else
{
var config = KubernetesClientConfiguration.InClusterConfig();
client = new Kubernetes(config);
}
var exitSemaphore = new SemaphoreSlim(0);
var cancellationTokenSource = new CancellationTokenSource();
try
{
Console.WriteLine("Connecting to HiveMP notification endpoint (" + hiveEndpoint + ")...");
var notifyClient = new GameServerNotifyClient
{
BaseUrl = hiveEndpoint
};
var protocol = await notifyClient.KubernetesNotifyGETAsync(new KubernetesNotifyGETRequest
{
ProjectId = projectId,
ClusterId = clusterId,
ClusterSecretToken = apiKey,
});
#pragma warning disable CS4014
Task.Run(async () =>
{
await protocol.WaitForDisconnect(cancellationTokenSource.Token);
Console.WriteLine("Disconnected from HiveMP.");
exitSemaphore.Release();
});
#pragma warning restore CS4014
Console.WriteLine("Connecting to Kubernetes pod watch endpoint...");
var watcher = await client.WatchNamespacedPodAsync(null, clusterNamespace);
watcher.OnError += (error) =>
{
Console.Error.WriteLine(error);
};
watcher.OnEvent += (eventType, pod) =>
{
var labels = pod?.Metadata?.Labels;
if (labels == null)
{
return;
}
if (!labels.ContainsKey("projectId") ||
!labels.ContainsKey("gameServerClusterId") ||
!labels.ContainsKey("gameServerId"))
{
return;
}
var podProjectId = labels["projectId"];
var podGameServerClusterId = labels["gameServerClusterId"];
var podGameServerId = labels["gameServerId"];
if (!string.IsNullOrWhiteSpace(podProjectId) &&
!string.IsNullOrWhiteSpace(podGameServerClusterId) &&
!string.IsNullOrWhiteSpace(podGameServerId) &&
podProjectId == projectId &&
podGameServerClusterId == clusterId)
{
Console.WriteLine("Forwarding notification for " + podProjectId + "/" + podGameServerClusterId + "/" + podGameServerId + "...");
Task.Run(async () => await protocol.SendPn(new PodNotifyMessage
{
Gsid = podGameServerId
}));
}
};
watcher.OnClosed += () =>
{
Console.WriteLine("Disconnected from Kubernetes.");
exitSemaphore.Release();
};
Console.WriteLine("Now listening for Kubernetes pod events.");
await exitSemaphore.WaitAsync();
cancellationTokenSource.Cancel();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
exitSemaphore.Release();
}
}
}
}