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

179 lines
6.0 KiB
C#
Raw Normal View History

2023-04-10 08:09:41 +00:00
using CodexDistTestCore.Marketplace;
using CodexDistTestCore.Metrics;
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-31 08:00:44 +00:00
private readonly MetricsAggregator metricsAggregator;
2023-04-10 08:09:41 +00:00
private readonly MarketplaceController marketplaceController;
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-31 08:00:44 +00:00
metricsAggregator = new MetricsAggregator(log, this);
2023-04-10 08:09:41 +00:00
marketplaceController = new MarketplaceController(log, this);
}
2023-03-22 08:22:18 +00:00
public ICodexNodeGroup BringOnline(OfflineCodexNodes offline)
{
var group = CreateOnlineCodexNodes(offline);
2023-04-10 08:09:41 +00:00
if (offline.MarketplaceConfig != null)
{
2023-04-11 10:06:33 +00:00
group.GethCompanionGroup = marketplaceController.BringOnlineMarketplace(offline);
2023-04-10 08:09:41 +00:00
}
K8s(k => k.BringOnline(group, offline));
2023-03-21 14:17:48 +00:00
2023-03-31 08:00:44 +00:00
if (offline.MetricsEnabled)
{
BringOnlineMetrics(group);
}
if (offline.MarketplaceConfig != null)
{
ConnectMarketplace(group);
2023-03-31 08:00:44 +00:00
}
log.Log($"{group.Describe()} online.");
return group;
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;
}
2023-04-10 08:09:41 +00:00
public string ExecuteCommand(PodInfo pod, string containerName, string command, params string[] arguments)
{
2023-04-10 08:09:41 +00:00
return K8s(k => k.ExecuteCommand(pod, containerName, command, arguments));
}
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-04-10 08:09:41 +00:00
return K8s(k => k.BringOnlinePrometheus(spec));
}
2023-04-11 10:06:33 +00:00
public K8sGethBoostrapSpecs CreateGethBootstrapNodeSpec()
2023-04-10 08:09:41 +00:00
{
2023-04-11 10:06:33 +00:00
return new K8sGethBoostrapSpecs(codexGroupNumberSource.GetNextServicePort());
}
2023-04-10 12:00:12 +00:00
2023-04-11 10:06:33 +00:00
public PodInfo BringOnlineGethBootstrapNode(K8sGethBoostrapSpecs spec)
{
2023-04-10 12:00:12 +00:00
return K8s(k => k.BringOnlineGethBootstrapNode(spec));
2023-03-27 12:49:34 +00:00
}
2023-04-11 10:06:33 +00:00
public PodInfo BringOnlineGethCompanionGroup(GethBootstrapInfo info, GethCompanionGroup group)
{
return K8s(k => k.BringOnlineGethCompanionGroup(info, group));
}
2023-03-31 08:00:44 +00:00
public void DownloadAllMetrics()
{
metricsAggregator.DownloadAllMetrics();
}
private void BringOnlineMetrics(CodexNodeGroup group)
{
2023-04-10 08:09:41 +00:00
metricsAggregator.BeginCollectingMetricsFor(DowncastNodes(group));
}
2023-03-31 08:00:44 +00:00
private void ConnectMarketplace(CodexNodeGroup group)
{
2023-04-11 10:06:33 +00:00
for (var i = 0; i < group.Nodes.Length; i++)
{
2023-04-11 10:06:33 +00:00
ConnectMarketplace(group, group.Nodes[i], group.GethCompanionGroup!.Containers[i]);
}
}
2023-04-11 10:06:33 +00:00
private void ConnectMarketplace(CodexNodeGroup group, OnlineCodexNode node, GethCompanionNodeContainer container)
{
2023-04-11 10:06:33 +00:00
var access = new MarketplaceAccess(this, marketplaceController, log, group, container);
access.Initialize();
node.Marketplace = access;
}
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
}
2023-04-10 08:09:41 +00:00
private T K8s<T>(Func<K8sOperations, T> action)
{
var k8s = new K8sOperations(knownPods);
var result = action(k8s);
k8s.Close();
return result;
}
private static OnlineCodexNode[] DowncastNodes(CodexNodeGroup group)
{
return group.Nodes.Cast<OnlineCodexNode>().ToArray();
}
}
}