2023-04-25 09:31:15 +00:00
|
|
|
|
using Logging;
|
2023-04-17 08:31:14 +00:00
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
public class StartupWorkflow
|
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
private readonly BaseLog log;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
private readonly WorkflowNumberSource numberSource;
|
|
|
|
|
private readonly K8sCluster cluster;
|
|
|
|
|
private readonly KnownK8sPods knownK8SPods;
|
2023-05-03 12:18:37 +00:00
|
|
|
|
private readonly string testNamespace;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory();
|
|
|
|
|
|
2023-05-03 12:18:37 +00:00
|
|
|
|
internal StartupWorkflow(BaseLog log, WorkflowNumberSource numberSource, K8sCluster cluster, KnownK8sPods knownK8SPods, string testNamespace)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
this.log = log;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
this.numberSource = numberSource;
|
|
|
|
|
this.cluster = cluster;
|
|
|
|
|
this.knownK8SPods = knownK8SPods;
|
2023-05-03 12:18:37 +00:00
|
|
|
|
this.testNamespace = testNamespace;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
public RunningContainers Start(int numberOfContainers, Location location, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
var recipes = CreateRecipes(numberOfContainers, recipeFactory, startupConfig);
|
|
|
|
|
|
|
|
|
|
var runningPod = controller.BringOnline(recipes, location);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-04-30 08:56:19 +00:00
|
|
|
|
return new RunningContainers(startupConfig, runningPod, CreateContainers(runningPod, recipes, startupConfig));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-04-13 09:07:36 +00:00
|
|
|
|
public void Stop(RunningContainers runningContainers)
|
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
controller.Stop(runningContainers.RunningPod);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 09:30:19 +00:00
|
|
|
|
public void DownloadContainerLog(RunningContainer container, ILogHandler logHandler)
|
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
controller.DownloadPodLog(container.Pod, container.Recipe, logHandler);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 07:54:07 +00:00
|
|
|
|
public string ExecuteCommand(RunningContainer container, string command, params string[] args)
|
|
|
|
|
{
|
|
|
|
|
return K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
return controller.ExecuteCommand(container.Pod, container.Recipe.Name, command, args);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
public void DeleteAllResources()
|
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
controller.DeleteAllResources();
|
|
|
|
|
});
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 12:18:37 +00:00
|
|
|
|
public void DeleteTestResources()
|
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
controller.DeleteTestNamespace();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 08:56:19 +00:00
|
|
|
|
private RunningContainer[] CreateContainers(RunningPod runningPod, ContainerRecipe[] recipes, StartupConfig startupConfig)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-05-04 09:34:43 +00:00
|
|
|
|
return recipes.Select(r =>
|
|
|
|
|
{
|
|
|
|
|
var servicePorts = runningPod.GetServicePortsForContainerRecipe(r);
|
|
|
|
|
log.Debug($"{r} -> service ports: {string.Join(",", servicePorts.Select(p => p.Number))}");
|
|
|
|
|
|
2023-06-01 07:35:18 +00:00
|
|
|
|
return new RunningContainer(runningPod, r, servicePorts, startupConfig,
|
|
|
|
|
GetContainerExternalAddress(runningPod, servicePorts),
|
2023-06-01 10:31:14 +00:00
|
|
|
|
GetContainerInternalAddress(r));
|
2023-06-01 07:35:18 +00:00
|
|
|
|
|
2023-05-04 09:34:43 +00:00
|
|
|
|
}).ToArray();
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 07:35:18 +00:00
|
|
|
|
private RunningContainerAddress GetContainerExternalAddress(RunningPod pod, Port[] servicePorts)
|
|
|
|
|
{
|
|
|
|
|
return new RunningContainerAddress(
|
|
|
|
|
pod.Cluster.HostAddress,
|
|
|
|
|
GetServicePort(servicePorts));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 10:31:14 +00:00
|
|
|
|
private RunningContainerAddress GetContainerInternalAddress(ContainerRecipe recipe)
|
2023-06-01 07:35:18 +00:00
|
|
|
|
{
|
2023-06-01 10:31:14 +00:00
|
|
|
|
var serviceName = "service-" + numberSource.WorkflowNumber;
|
|
|
|
|
var namespaceName = cluster.Configuration.K8sNamespacePrefix + testNamespace;
|
2023-06-01 08:14:32 +00:00
|
|
|
|
var port = recipe.ExposedPorts.First().Number;
|
2023-06-01 07:35:18 +00:00
|
|
|
|
|
|
|
|
|
return new RunningContainerAddress(
|
2023-06-01 10:31:14 +00:00
|
|
|
|
$"http://{serviceName}.{namespaceName}.svc.cluster.local",
|
2023-06-01 08:14:32 +00:00
|
|
|
|
port);
|
2023-06-01 07:35:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int GetServicePort(Port[] servicePorts)
|
|
|
|
|
{
|
|
|
|
|
if (servicePorts.Any()) return servicePorts.First().Number;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private ContainerRecipe[] CreateRecipes(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
|
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-04-12 11:53:55 +00:00
|
|
|
|
var result = new List<ContainerRecipe>();
|
|
|
|
|
for (var i = 0; i < numberOfContainers; i++)
|
|
|
|
|
{
|
2023-05-04 09:34:43 +00:00
|
|
|
|
result.Add(recipeFactory.CreateRecipe(i, numberSource.GetContainerNumber(), componentFactory, startupConfig));
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
|
|
|
|
private void K8s(Action<K8sController> action)
|
|
|
|
|
{
|
2023-05-03 12:18:37 +00:00
|
|
|
|
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, testNamespace);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
action(controller);
|
|
|
|
|
controller.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T K8s<T>(Func<K8sController, T> action)
|
|
|
|
|
{
|
2023-05-03 12:18:37 +00:00
|
|
|
|
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, testNamespace);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
var result = action(controller);
|
|
|
|
|
controller.Dispose();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-04-13 09:30:19 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
2023-04-13 09:30:19 +00:00
|
|
|
|
public interface ILogHandler
|
|
|
|
|
{
|
|
|
|
|
void Log(Stream log);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
2023-04-17 08:31:14 +00:00
|
|
|
|
|
|
|
|
|
public abstract class LogHandler : ILogHandler
|
|
|
|
|
{
|
|
|
|
|
public void Log(Stream log)
|
|
|
|
|
{
|
|
|
|
|
using var reader = new StreamReader(log);
|
|
|
|
|
var line = reader.ReadLine();
|
|
|
|
|
while (line != null)
|
|
|
|
|
{
|
|
|
|
|
ProcessLine(line);
|
|
|
|
|
line = reader.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void ProcessLine(string line);
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|