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

102 lines
3.4 KiB
C#
Raw 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);
void FetchPodLog(OnlineCodexNode node, IPodLogHandler logHandler);
}
public class K8sManager : IK8sManager
{
2023-03-22 13:49:01 +00:00
private readonly CodexGroupNumberSource codexGroupNumberSource = new CodexGroupNumberSource();
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-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
}
public void ForEachOnlineGroup(Action<CodexNodeGroup> action)
2023-03-21 14:17:48 +00:00
{
foreach (var group in onlineCodexNodeGroups) action(group);
}
public void FetchPodLog(OnlineCodexNode node, IPodLogHandler logHandler)
{
K8s(k => k.FetchPodLog(node, logHandler));
}
public PrometheusInfo BringOnlinePrometheus(string config, int prometheusNumber)
2023-03-27 12:49:34 +00:00
{
var spec = new K8sPrometheusSpecs(codexGroupNumberSource.GetNextServicePort(), prometheusNumber, config);
2023-03-27 12:49:34 +00:00
PrometheusInfo? info = null;
K8s(k => info = k.BringOnlinePrometheus(spec));
2023-03-27 12:49:34 +00:00
return info!;
}
2023-03-22 08:22:18 +00:00
private CodexNodeGroup CreateOnlineCodexNodes(OfflineCodexNodes offline)
{
var containers = CreateContainers(offline);
2023-03-22 08:22:18 +00:00
var online = containers.Select(c => new OnlineCodexNode(log, fileManager, c)).ToArray();
var result = new CodexNodeGroup(log, codexGroupNumberSource.GetNextCodexNodeGroupNumber(), offline, this, online);
onlineCodexNodeGroups.Add(result);
2023-03-21 14:17:48 +00:00
return result;
}
private CodexNodeContainer[] CreateContainers(OfflineCodexNodes offline)
{
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 < offline.NumberOfNodes; i++) containers.Add(factory.CreateNext(offline));
2023-03-21 15:09:41 +00:00
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;
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
}
}
}