Initial working prototype
This commit is contained in:
parent
3f6ff87370
commit
ccc5337dc1
17 changed files with 506 additions and 57 deletions
|
|
@ -6,6 +6,6 @@ public interface IContainerHandler
|
|||
{
|
||||
public Task<Container[]> GetContainers();
|
||||
public Task<Container> GetContainer(string containerName, string? containerNamespace);
|
||||
public Task StartContainer();
|
||||
public Task StopContainer();
|
||||
public Task StartContainer(string containerName, string? containerNamespace);
|
||||
public Task StopContainer(string containerName, string? containerNamespace);
|
||||
}
|
||||
|
|
@ -36,11 +36,14 @@ public class KubernetesHandler : IContainerHandler
|
|||
|
||||
foreach (var item in list.Items)
|
||||
{
|
||||
if (item.Namespace() == "kube-system")
|
||||
continue;
|
||||
|
||||
var c = new Container
|
||||
{
|
||||
containerNamespace = item.Namespace(),
|
||||
ContainerNamespace = item.Namespace(),
|
||||
Name = item.Name(),
|
||||
Running = item.Status.Replicas > 0
|
||||
Running = item.Status.ReadyReplicas > 0
|
||||
};
|
||||
|
||||
containers.Add(c);
|
||||
|
|
@ -49,13 +52,57 @@ public class KubernetesHandler : IContainerHandler
|
|||
return containers.ToArray();
|
||||
}
|
||||
|
||||
public Task StartContainer()
|
||||
public async Task StartContainer(string containerName, string? containerNamespace)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var container = await _client.AppsV1.ReadNamespacedDeploymentAsync(containerName, containerNamespace);
|
||||
|
||||
if (container == null)
|
||||
{
|
||||
throw new Exception($"Could not get container {containerName} in namespace {containerNamespace}");
|
||||
}
|
||||
|
||||
var patchStr = @"
|
||||
{
|
||||
""spec"": {
|
||||
""replicas"": 1
|
||||
}
|
||||
}
|
||||
";
|
||||
|
||||
var patch = new V1Patch(patchStr, V1Patch.PatchType.MergePatch);
|
||||
|
||||
var result = await _client.AppsV1.PatchNamespacedDeploymentAsync(patch, containerName, containerNamespace, fieldManager: "ContainerDashboard");
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
throw new Exception($"Could not update container {containerName} in namespace {containerNamespace}");
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopContainer()
|
||||
public async Task StopContainer(string containerName, string? containerNamespace)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var container = await _client.AppsV1.ReadNamespacedDeploymentAsync(containerName, containerNamespace);
|
||||
|
||||
if (container == null)
|
||||
{
|
||||
throw new Exception($"Could not get container {containerName} in namespace {containerNamespace}");
|
||||
}
|
||||
|
||||
var patchStr = @"
|
||||
{
|
||||
""spec"": {
|
||||
""replicas"": 0
|
||||
}
|
||||
}
|
||||
";
|
||||
|
||||
var patch = new V1Patch(patchStr, V1Patch.PatchType.MergePatch);
|
||||
|
||||
var result = await _client.AppsV1.PatchNamespacedDeploymentAsync(patch, containerName, containerNamespace, fieldManager: "ContainerDashboard");
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
throw new Exception($"Could not update container {containerName} in namespace {containerNamespace}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue