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-31 08:00:44 +00:00
|
|
|
|
private readonly MetricsAggregator metricsAggregator;
|
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-31 08:00:44 +00:00
|
|
|
|
metricsAggregator = new MetricsAggregator(log, this);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-31 08:00:44 +00:00
|
|
|
|
if (offline.MetricsEnabled)
|
|
|
|
|
{
|
|
|
|
|
BringOnlineMetrics(online);
|
|
|
|
|
}
|
|
|
|
|
|
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-28 11:43:25 +00:00
|
|
|
|
public PrometheusInfo BringOnlinePrometheus(string config, int prometheusNumber)
|
2023-03-27 12:49:34 +00:00
|
|
|
|
{
|
2023-03-28 11:43:25 +00:00
|
|
|
|
var spec = new K8sPrometheusSpecs(codexGroupNumberSource.GetNextServicePort(), prometheusNumber, config);
|
2023-03-27 14:24:04 +00:00
|
|
|
|
|
2023-03-27 12:49:34 +00:00
|
|
|
|
PrometheusInfo? info = null;
|
2023-03-27 14:24:04 +00:00
|
|
|
|
K8s(k => info = k.BringOnlinePrometheus(spec));
|
2023-03-27 12:49:34 +00:00
|
|
|
|
return info!;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 08:00:44 +00:00
|
|
|
|
public void DownloadAllMetrics()
|
|
|
|
|
{
|
|
|
|
|
metricsAggregator.DownloadAllMetrics();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BringOnlineMetrics(CodexNodeGroup group)
|
|
|
|
|
{
|
|
|
|
|
var onlineNodes = group.Nodes.Cast<OnlineCodexNode>().ToArray();
|
|
|
|
|
|
|
|
|
|
metricsAggregator.BeginCollectingMetricsFor(onlineNodes);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
private CodexNodeGroup CreateOnlineCodexNodes(OfflineCodexNodes offline)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-27 08:27:08 +00:00
|
|
|
|
var containers = CreateContainers(offline);
|
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-27 08:27:08 +00:00
|
|
|
|
private CodexNodeContainer[] CreateContainers(OfflineCodexNodes offline)
|
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>();
|
2023-03-27 08:27:08 +00:00
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
}
|