2023-11-12 09:07:23 +00:00
|
|
|
|
using KubernetesWorkflow.Recipe;
|
|
|
|
|
using KubernetesWorkflow.Types;
|
|
|
|
|
using Logging;
|
2023-10-23 13:49:14 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2023-06-21 06:28:40 +00:00
|
|
|
|
using Utils;
|
2023-04-17 08:31:14 +00:00
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-09-11 14:57:57 +00:00
|
|
|
|
public interface IStartupWorkflow
|
|
|
|
|
{
|
2023-09-25 06:47:19 +00:00
|
|
|
|
IKnownLocations GetAvailableLocations();
|
|
|
|
|
RunningContainers Start(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig);
|
|
|
|
|
RunningContainers Start(int numberOfContainers, ILocation location, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
PodInfo GetPodInfo(RunningContainer container);
|
|
|
|
|
PodInfo GetPodInfo(RunningContainers containers);
|
2023-09-20 10:55:09 +00:00
|
|
|
|
CrashWatcher CreateCrashWatcher(RunningContainer container);
|
2024-03-13 09:57:26 +00:00
|
|
|
|
void Stop(RunningContainers containers, bool waitTillStopped);
|
2023-09-12 11:32:06 +00:00
|
|
|
|
void DownloadContainerLog(RunningContainer container, ILogHandler logHandler, int? tailLines = null);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
string ExecuteCommand(RunningContainer container, string command, params string[] args);
|
2023-09-12 08:31:55 +00:00
|
|
|
|
void DeleteNamespace();
|
|
|
|
|
void DeleteNamespacesStartingWith(string namespacePrefix);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class StartupWorkflow : IStartupWorkflow
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private readonly ILog log;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
private readonly WorkflowNumberSource numberSource;
|
|
|
|
|
private readonly K8sCluster cluster;
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private readonly string k8sNamespace;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory();
|
2023-09-25 06:47:19 +00:00
|
|
|
|
private readonly LocationProvider locationProvider;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-10-31 13:48:16 +00:00
|
|
|
|
internal StartupWorkflow(ILog log, WorkflowNumberSource numberSource, K8sCluster cluster, string k8sNamespace)
|
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;
|
2023-09-12 08:31:55 +00:00
|
|
|
|
this.k8sNamespace = k8sNamespace;
|
2023-09-25 06:47:19 +00:00
|
|
|
|
|
|
|
|
|
locationProvider = new LocationProvider(log, K8s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IKnownLocations GetAvailableLocations()
|
|
|
|
|
{
|
|
|
|
|
return locationProvider.GetAvailableLocations();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RunningContainers Start(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
|
|
|
|
|
{
|
|
|
|
|
return Start(numberOfContainers, KnownLocations.UnspecifiedLocation, recipeFactory, startupConfig);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 06:47:19 +00:00
|
|
|
|
public RunningContainers Start(int numberOfContainers, ILocation location, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return K8s(controller =>
|
|
|
|
|
{
|
2023-11-14 09:49:14 +00:00
|
|
|
|
componentFactory.Update(controller);
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
var recipes = CreateRecipes(numberOfContainers, recipeFactory, startupConfig);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var startResult = controller.BringOnline(recipes, location);
|
|
|
|
|
var containers = CreateContainers(startResult, recipes, startupConfig);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var rc = new RunningContainers(startupConfig, startResult, containers);
|
2023-09-13 13:10:19 +00:00
|
|
|
|
cluster.Configuration.Hooks.OnContainersStarted(rc);
|
2023-11-14 09:49:14 +00:00
|
|
|
|
|
|
|
|
|
if (startResult.ExternalService != null)
|
|
|
|
|
{
|
|
|
|
|
componentFactory.Update(controller);
|
|
|
|
|
}
|
2023-09-13 13:10:19 +00:00
|
|
|
|
return rc;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
});
|
2023-08-14 13:10:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public PodInfo GetPodInfo(RunningContainer container)
|
|
|
|
|
{
|
|
|
|
|
return K8s(c => c.GetPodInfo(container.RunningContainers.StartResult.Deployment));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PodInfo GetPodInfo(RunningContainers containers)
|
|
|
|
|
{
|
|
|
|
|
return K8s(c => c.GetPodInfo(containers.StartResult.Deployment));
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 10:55:09 +00:00
|
|
|
|
public CrashWatcher CreateCrashWatcher(RunningContainer container)
|
|
|
|
|
{
|
|
|
|
|
return K8s(c => c.CreateCrashWatcher(container));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 09:57:26 +00:00
|
|
|
|
public void Stop(RunningContainers runningContainers, bool waitTillStopped)
|
2023-04-13 09:07:36 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2024-03-13 09:57:26 +00:00
|
|
|
|
controller.Stop(runningContainers.StartResult, waitTillStopped);
|
2023-09-13 13:10:19 +00:00
|
|
|
|
cluster.Configuration.Hooks.OnContainersStopped(runningContainers);
|
2023-04-13 09:07:36 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 11:32:06 +00:00
|
|
|
|
public void DownloadContainerLog(RunningContainer container, ILogHandler logHandler, int? tailLines = null)
|
2023-04-13 09:30:19 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
controller.DownloadPodLog(container, logHandler, tailLines);
|
2023-04-13 09:30:19 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 07:54:07 +00:00
|
|
|
|
public string ExecuteCommand(RunningContainer container, string command, params string[] args)
|
|
|
|
|
{
|
|
|
|
|
return K8s(controller =>
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
return controller.ExecuteCommand(container, command, args);
|
2023-04-14 07:54:07 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
public void DeleteNamespace()
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
controller.DeleteNamespace();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
});
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
public void DeleteNamespacesStartingWith(string namespacePrefix)
|
2023-05-03 12:18:37 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
controller.DeleteAllNamespacesStartingWith(namespacePrefix);
|
2023-05-03 12:18:37 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private RunningContainer[] CreateContainers(StartResult startResult, 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 =>
|
|
|
|
|
{
|
2023-06-23 08:35:23 +00:00
|
|
|
|
var name = GetContainerName(r, startupConfig);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var addresses = CreateContainerAddresses(startResult, r);
|
|
|
|
|
log.Debug($"{r}={name} -> container addresses: {string.Join(Environment.NewLine, addresses.Select(a => a.ToString()))}");
|
2023-06-23 08:35:23 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
return new RunningContainer(name, r, addresses);
|
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-23 08:35:23 +00:00
|
|
|
|
private string GetContainerName(ContainerRecipe recipe, StartupConfig startupConfig)
|
|
|
|
|
{
|
|
|
|
|
if (startupConfig == null) return "";
|
|
|
|
|
if (!string.IsNullOrEmpty(startupConfig.NameOverride))
|
|
|
|
|
{
|
|
|
|
|
return $"<{startupConfig.NameOverride}{recipe.Number}>";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return $"<{recipe.Name}>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private ContainerAddress[] CreateContainerAddresses(StartResult startResult, ContainerRecipe recipe)
|
2023-06-01 07:35:18 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var result = new List<ContainerAddress>();
|
2023-10-19 09:08:30 +00:00
|
|
|
|
foreach (var exposedPort in recipe.ExposedPorts)
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
result.Add(new ContainerAddress(exposedPort.Tag, GetContainerExternalAddress(startResult, recipe, exposedPort.Tag), false));
|
2023-11-06 15:52:55 +00:00
|
|
|
|
result.Add(new ContainerAddress(exposedPort.Tag, GetContainerInternalAddress(startResult, recipe, exposedPort.Tag), true));
|
2023-10-19 09:08:30 +00:00
|
|
|
|
}
|
2023-10-23 12:05:08 +00:00
|
|
|
|
foreach (var internalPort in recipe.InternalPorts)
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
result.Add(new ContainerAddress(internalPort.Tag, GetContainerInternalAddress(startResult, recipe, internalPort.Tag), true));
|
2023-10-23 12:05:08 +00:00
|
|
|
|
}
|
2023-10-19 09:08:30 +00:00
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private static Address GetContainerExternalAddress(StartResult startResult, ContainerRecipe recipe, string tag)
|
2023-10-19 09:08:30 +00:00
|
|
|
|
{
|
2023-11-06 15:52:55 +00:00
|
|
|
|
var port = startResult.GetExternalServicePorts(recipe, tag);
|
2023-10-19 09:08:30 +00:00
|
|
|
|
|
2023-06-21 06:28:40 +00:00
|
|
|
|
return new Address(
|
2023-11-06 13:33:47 +00:00
|
|
|
|
startResult.Cluster.HostAddress,
|
|
|
|
|
port.Number);
|
2023-06-01 07:35:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private Address GetContainerInternalAddress(StartResult startResult, ContainerRecipe recipe, string tag)
|
2023-06-01 07:35:18 +00:00
|
|
|
|
{
|
2023-11-07 12:13:06 +00:00
|
|
|
|
var namespaceName = startResult.Cluster.Configuration.KubernetesNamespace;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var serviceName = startResult.InternalService!.Name;
|
2023-11-06 15:52:55 +00:00
|
|
|
|
var port = startResult.GetInternalServicePorts(recipe, tag);
|
2023-11-07 12:49:28 +00:00
|
|
|
|
|
2023-06-21 06:28:40 +00:00
|
|
|
|
return new Address(
|
2023-11-07 12:49:28 +00:00
|
|
|
|
$"http://{serviceName}.{namespaceName}.svc.cluster.local",
|
2023-10-23 12:05:08 +00:00
|
|
|
|
port.Number);
|
2023-06-01 07:35:18 +00:00
|
|
|
|
}
|
2023-10-19 09:08:30 +00:00
|
|
|
|
|
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-09-13 14:06:05 +00:00
|
|
|
|
var recipe = recipeFactory.CreateRecipe(i, numberSource.GetContainerNumber(), componentFactory, startupConfig);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
CheckPorts(recipe);
|
|
|
|
|
|
2023-09-13 14:06:05 +00:00
|
|
|
|
if (cluster.Configuration.AddAppPodLabel) recipe.PodLabels.Add("app", recipeFactory.AppName);
|
|
|
|
|
cluster.Configuration.Hooks.OnContainerRecipeCreated(recipe);
|
|
|
|
|
result.Add(recipe);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private void CheckPorts(ContainerRecipe recipe)
|
|
|
|
|
{
|
|
|
|
|
var allTags =
|
|
|
|
|
recipe.ExposedPorts.Concat(recipe.InternalPorts)
|
|
|
|
|
.Select(p => K8sNameUtils.Format(p.Tag)).ToArray();
|
|
|
|
|
|
|
|
|
|
if (allTags.Length != allTags.Distinct().Count())
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Duplicate port tags found in recipe for " + recipe.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
private void K8s(Action<K8sController> action)
|
|
|
|
|
{
|
2023-10-23 13:49:14 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-10-31 13:48:16 +00:00
|
|
|
|
var controller = new K8sController(log, cluster, numberSource, k8sNamespace);
|
2023-10-23 13:49:14 +00:00
|
|
|
|
action(controller);
|
|
|
|
|
controller.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (k8s.Autorest.HttpOperationException ex)
|
|
|
|
|
{
|
2023-11-13 14:11:49 +00:00
|
|
|
|
log.Error(JsonConvert.SerializeObject(ex.Response));
|
2023-10-23 13:49:14 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T K8s<T>(Func<K8sController, T> action)
|
2023-08-07 13:51:44 +00:00
|
|
|
|
{
|
2023-10-23 13:49:14 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-10-31 13:48:16 +00:00
|
|
|
|
var controller = new K8sController(log, cluster, numberSource, k8sNamespace);
|
2023-10-23 13:49:14 +00:00
|
|
|
|
var result = action(controller);
|
|
|
|
|
controller.Dispose();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (k8s.Autorest.HttpOperationException ex)
|
|
|
|
|
{
|
2023-11-13 14:11:49 +00:00
|
|
|
|
log.Error(JsonConvert.SerializeObject(ex.Response));
|
2023-10-23 13:49:14 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2023-08-10 09:47:34 +00:00
|
|
|
|
}
|
2023-04-13 09:30:19 +00:00
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|