2023-03-21 15:09:41 +00:00
|
|
|
|
namespace CodexDistTestCore
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
|
|
|
|
public interface IK8sManager
|
|
|
|
|
{
|
2023-03-22 08:22:18 +00:00
|
|
|
|
ICodexNodeGroup BringOnline(OfflineCodexNodes node);
|
|
|
|
|
IOfflineCodexNodes BringOffline(ICodexNodeGroup node);
|
2023-03-26 08:45:01 +00:00
|
|
|
|
void FetchPodLog(OnlineCodexNode node, IPodLogHandler logHandler);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class K8sManager : IK8sManager
|
|
|
|
|
{
|
2023-03-22 13:49:01 +00:00
|
|
|
|
private readonly CodexGroupNumberSource codexGroupNumberSource = new CodexGroupNumberSource();
|
2023-03-26 08:45:01 +00:00
|
|
|
|
private readonly List<CodexNodeGroup> onlineCodexNodeGroups = 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-19 09:49:03 +00:00
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
public K8sManager(TestLog log, IFileManager fileManager)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-22 08:22:18 +00:00
|
|
|
|
this.log = log;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
this.fileManager = fileManager;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
public ICodexNodeGroup BringOnline(OfflineCodexNodes offline)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-21 15:09:41 +00:00
|
|
|
|
var online = CreateOnlineCodexNodes(offline);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
2023-03-23 11:35:03 +00:00
|
|
|
|
K8s(k => k.BringOnline(online, offline));
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
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-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-21 15:09:41 +00:00
|
|
|
|
var online = GetAndRemoveActiveNodeFor(node);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
2023-03-23 11:35:03 +00:00
|
|
|
|
K8s(k => k.BringOffline(online));
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
log.Log($"{online.Describe()} offline.");
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
2023-03-21 15:09:41 +00:00
|
|
|
|
return online.Origin;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-26 08:45:01 +00:00
|
|
|
|
public void ForEachOnlineGroup(Action<CodexNodeGroup> action)
|
2023-03-21 14:17:48 +00:00
|
|
|
|
{
|
2023-03-26 08:45:01 +00:00
|
|
|
|
foreach (var group in onlineCodexNodeGroups) action(group);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FetchPodLog(OnlineCodexNode node, IPodLogHandler logHandler)
|
|
|
|
|
{
|
|
|
|
|
K8s(k => k.FetchPodLog(node, logHandler));
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
private CodexNodeGroup CreateOnlineCodexNodes(OfflineCodexNodes offline)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
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-26 08:45:01 +00:00
|
|
|
|
var result = new CodexNodeGroup(log, codexGroupNumberSource.GetNextCodexNodeGroupNumber(), offline, this, online);
|
|
|
|
|
onlineCodexNodeGroups.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-21 08:20:09 +00:00
|
|
|
|
{
|
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-21 08:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-26 08:45:01 +00:00
|
|
|
|
onlineCodexNodeGroups.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
|
|
|
|
}
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|