cs-codex-dist-tests/CodexDistTestCore/K8sManager.cs

87 lines
2.8 KiB
C#
Raw Permalink Normal View History

2023-03-21 15:09:41 +00:00
namespace CodexDistTestCore
{
public interface IK8sManager
{
2023-03-22 08:22:18 +00:00
ICodexNodeGroup BringOnline(OfflineCodexNodes node);
IOfflineCodexNodes BringOffline(ICodexNodeGroup node);
}
public class K8sManager : IK8sManager
{
2023-03-22 13:49:01 +00:00
private readonly CodexGroupNumberSource codexGroupNumberSource = new CodexGroupNumberSource();
2023-03-22 08:22:18 +00:00
private readonly List<CodexNodeGroup> onlineCodexNodes = new List<CodexNodeGroup>();
2023-03-21 15:09:41 +00:00
private readonly KnownK8sPods knownPods = new KnownK8sPods();
2023-03-22 08:22:18 +00:00
private readonly TestLog log;
2023-03-21 07:23:15 +00:00
private readonly IFileManager fileManager;
2023-03-22 08:22:18 +00:00
public K8sManager(TestLog log, IFileManager fileManager)
{
2023-03-22 08:22:18 +00:00
this.log = log;
this.fileManager = fileManager;
}
2023-03-22 08:22:18 +00:00
public ICodexNodeGroup BringOnline(OfflineCodexNodes offline)
{
2023-03-21 15:09:41 +00:00
var online = CreateOnlineCodexNodes(offline);
2023-03-23 11:35:03 +00:00
K8s(k => k.BringOnline(online, offline));
2023-03-22 08:22:18 +00:00
log.Log($"{online.Describe()} online.");
2023-03-21 14:17:48 +00:00
2023-03-21 15:09:41 +00:00
return online;
2023-03-21 14:44:21 +00:00
}
2023-03-22 08:22:18 +00:00
public IOfflineCodexNodes BringOffline(ICodexNodeGroup node)
{
2023-03-21 15:09:41 +00:00
var online = GetAndRemoveActiveNodeFor(node);
2023-03-23 11:35:03 +00:00
K8s(k => k.BringOffline(online));
2023-03-22 08:22:18 +00:00
log.Log($"{online.Describe()} offline.");
2023-03-21 15:09:41 +00:00
return online.Origin;
}
public void DeleteAllResources()
{
2023-03-23 11:35:03 +00:00
K8s(k => k.DeleteAllResources());
2023-03-21 14:17:48 +00:00
}
2023-03-21 15:09:41 +00:00
public void FetchAllPodsLogs(IPodLogsHandler logHandler)
2023-03-21 14:17:48 +00:00
{
2023-03-23 11:35:03 +00:00
K8s(k => k.FetchAllPodsLogs(onlineCodexNodes.ToArray(), logHandler));
}
2023-03-22 08:22:18 +00:00
private CodexNodeGroup CreateOnlineCodexNodes(OfflineCodexNodes offline)
{
2023-03-21 15:09:41 +00:00
var containers = CreateContainers(offline.NumberOfNodes);
2023-03-22 08:22:18 +00:00
var online = containers.Select(c => new OnlineCodexNode(log, fileManager, c)).ToArray();
2023-03-22 13:49:01 +00:00
var result = new CodexNodeGroup(codexGroupNumberSource.GetNextCodexNodeGroupNumber(), offline, this, online);
2023-03-21 15:09:41 +00:00
onlineCodexNodes.Add(result);
2023-03-21 14:17:48 +00:00
return result;
}
2023-03-21 15:09:41 +00:00
private CodexNodeContainer[] CreateContainers(int number)
{
2023-03-22 13:49:01 +00:00
var factory = new CodexNodeContainerFactory(codexGroupNumberSource);
2023-03-21 15:09:41 +00:00
var containers = new List<CodexNodeContainer>();
for (var i = 0; i < number; i++) containers.Add(factory.CreateNext());
return containers.ToArray();
}
2023-03-22 08:22:18 +00:00
private CodexNodeGroup GetAndRemoveActiveNodeFor(ICodexNodeGroup node)
2023-03-19 10:40:05 +00:00
{
2023-03-22 08:22:18 +00:00
var n = (CodexNodeGroup)node;
2023-03-21 15:09:41 +00:00
onlineCodexNodes.Remove(n);
2023-03-21 14:44:21 +00:00
return n;
2023-03-19 10:40:05 +00:00
}
2023-03-21 15:09:41 +00:00
2023-03-23 11:35:03 +00:00
private void K8s(Action<K8sOperations> action)
2023-03-21 15:09:41 +00:00
{
2023-03-23 11:35:03 +00:00
var k8s = new K8sOperations(knownPods);
action(k8s);
k8s.Close();
2023-03-21 15:09:41 +00:00
}
}
}