2023-04-12 11:53:55 +00:00
|
|
|
|
namespace KubernetesWorkflow
|
|
|
|
|
{
|
|
|
|
|
public class StartupWorkflow
|
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
private readonly WorkflowNumberSource numberSource;
|
|
|
|
|
private readonly K8sCluster cluster;
|
|
|
|
|
private readonly KnownK8sPods knownK8SPods;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory();
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
internal StartupWorkflow(WorkflowNumberSource numberSource, K8sCluster cluster, KnownK8sPods knownK8SPods)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
this.numberSource = numberSource;
|
|
|
|
|
this.cluster = cluster;
|
|
|
|
|
this.knownK8SPods = knownK8SPods;
|
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-12 13:11:36 +00:00
|
|
|
|
return new RunningContainers(startupConfig, runningPod, CreateContainers(runningPod, recipes));
|
|
|
|
|
});
|
|
|
|
|
}
|
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-12 13:11:36 +00:00
|
|
|
|
public void DeleteAllResources()
|
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
controller.DeleteAllResources();
|
|
|
|
|
});
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static RunningContainer[] CreateContainers(RunningPod runningPod, ContainerRecipe[] recipes)
|
|
|
|
|
{
|
|
|
|
|
return recipes.Select(r => new RunningContainer(runningPod, r, runningPod.GetServicePortsForContainerRecipe(r))).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ContainerRecipe[] CreateRecipes(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<ContainerRecipe>();
|
|
|
|
|
for (var i = 0; i < numberOfContainers; i++)
|
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
result.Add(recipeFactory.CreateRecipe(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)
|
|
|
|
|
{
|
|
|
|
|
var controller = new K8sController(cluster, knownK8SPods, numberSource);
|
|
|
|
|
action(controller);
|
|
|
|
|
controller.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T K8s<T>(Func<K8sController, T> action)
|
|
|
|
|
{
|
|
|
|
|
var controller = new K8sController(cluster, knownK8SPods, numberSource);
|
|
|
|
|
var result = action(controller);
|
|
|
|
|
controller.Dispose();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|