61 lines
No EOL
1.3 KiB
C#
61 lines
No EOL
1.3 KiB
C#
using k8s;
|
|
using ContainerDashboard.Models;
|
|
using k8s.Models;
|
|
|
|
namespace ContainerDashboard.Repositories;
|
|
|
|
public class KubernetesHandler : IContainerHandler
|
|
{
|
|
private readonly KubernetesClientConfiguration _config;
|
|
private readonly Kubernetes _client;
|
|
|
|
public KubernetesHandler()
|
|
{
|
|
try
|
|
{
|
|
var configPath = Environment.GetEnvironmentVariable("KUBECONFIG");
|
|
_config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Environment.GetEnvironmentVariable("KUBECONFIG"));
|
|
}
|
|
catch (ArgumentNullException)
|
|
{
|
|
_config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
}
|
|
|
|
_client = new Kubernetes(_config);
|
|
}
|
|
|
|
public Task<Container> GetContainer(string containerName, string? containerNamespace)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<Container[]> GetContainers()
|
|
{
|
|
var list = await _client.AppsV1.ListDeploymentForAllNamespacesAsync();
|
|
var containers = new List<Container>();
|
|
|
|
foreach (var item in list.Items)
|
|
{
|
|
var c = new Container
|
|
{
|
|
containerNamespace = item.Namespace(),
|
|
Name = item.Name(),
|
|
Running = item.Status.Replicas > 0
|
|
};
|
|
|
|
containers.Add(c);
|
|
}
|
|
|
|
return containers.ToArray();
|
|
}
|
|
|
|
public Task StartContainer()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public Task StopContainer()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
} |