/
KubernetesClient.cs
69 lines (61 loc) · 2.62 KB
/
KubernetesClient.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
using KubeClient;
using KubeClient.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace AlexaOpenKoreAzFunc
{
public class KubernetesClient
{
ILogger log;
public KubernetesClient (ILogger log)
{
this.log = log;
}
public async Task<int> Scale(int numberOfPods, string k8sApiUri, string k8sBearerToken, string k8sDeploymentName)
{
try
{
KubeClientOptions options = new KubeClientOptions()
{
ApiEndPoint = new Uri(k8sApiUri), //Change this for your own API URI
AuthStrategy = KubeAuthStrategy.BearerToken,
AccessToken = k8sBearerToken, //Change this for your own Bearer
AllowInsecure = true
};
using (KubeApiClient client = KubeApiClient.Create(options))
{
var deployments = await client.DeploymentsV1().List();
var currentDeployment = deployments.First(i=>i.Metadata.Name == k8sDeploymentName); //change first for the real one (rathena-openkore-cognitive)
DeploymentV1 updatedDeployment = await UpdateDeployment(client, currentDeployment, numberOfPods);
}
return ExitCodes.Success;
}
catch (Exception unexpectedError)
{
log.LogError("[ERROR] K8s unable to scale deployment: " + unexpectedError.ToString());
return ExitCodes.UnexpectedError;
}
}
static async Task<DeploymentV1> UpdateDeployment(IKubeApiClient client, DeploymentV1 existingDeployment, int numberOfPods)
{
DeploymentV1 updatedDeployment = await client.DeploymentsV1().Update(existingDeployment.Metadata.Name, kubeNamespace: existingDeployment.Metadata.Namespace, patchAction: patch =>
{
patch.Replace(
path: deployment => deployment.Spec.Replicas,
value: numberOfPods
);
});
updatedDeployment = await client.DeploymentsV1().Get(updatedDeployment.Metadata.Name, updatedDeployment.Metadata.Namespace);
return updatedDeployment;
}
public static class ExitCodes
{
public const int Success = 0;
public const int InvalidArguments = 1;
public const int NotFound = 10;
public const int UnexpectedError = 50;
}
}
}