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();
|
2024-04-09 07:30:45 +00:00
|
|
|
|
FutureContainers Start(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig);
|
|
|
|
|
FutureContainers Start(int numberOfContainers, ILocation location, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
PodInfo GetPodInfo(RunningContainer container);
|
2024-04-13 14:09:17 +00:00
|
|
|
|
PodInfo GetPodInfo(RunningPod pod);
|
2023-09-20 10:55:09 +00:00
|
|
|
|
CrashWatcher CreateCrashWatcher(RunningContainer container);
|
2024-04-13 14:09:17 +00:00
|
|
|
|
void Stop(RunningPod pod, bool waitTillStopped);
|
2024-06-19 08:39:14 +00:00
|
|
|
|
void DownloadContainerLog(RunningContainer container, ILogHandler logHandler, int? tailLines = null, bool? previous = null);
|
2024-08-01 08:39:06 +00:00
|
|
|
|
IDownloadedLog DownloadContainerLog(RunningContainer container, int? tailLines = null, bool? previous = null);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
string ExecuteCommand(RunningContainer container, string command, params string[] args);
|
2024-06-06 13:09:52 +00:00
|
|
|
|
void DeleteNamespace(bool wait);
|
|
|
|
|
void DeleteNamespacesStartingWith(string namespacePrefix, bool wait);
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 07:30:45 +00:00
|
|
|
|
public FutureContainers Start(int numberOfContainers, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
|
2023-09-25 06:47:19 +00:00
|
|
|
|
{
|
|
|
|
|
return Start(numberOfContainers, KnownLocations.UnspecifiedLocation, recipeFactory, startupConfig);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 07:30:45 +00:00
|
|
|
|
public FutureContainers 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
|
|
|
|
|
2024-08-01 08:39:06 +00:00
|
|
|
|
var rc = new RunningPod(Guid.NewGuid().ToString(), 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);
|
|
|
|
|
}
|
2024-04-09 07:30:45 +00:00
|
|
|
|
return new FutureContainers(rc, this);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
public void WaitUntilOnline(RunningPod rc)
|
2024-04-09 07:30:45 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in rc.Containers)
|
|
|
|
|
{
|
|
|
|
|
controller.WaitUntilOnline(c);
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2024-04-13 14:09:17 +00:00
|
|
|
|
return K8s(c => c.GetPodInfo(container.RunningPod.StartResult.Deployment));
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
public PodInfo GetPodInfo(RunningPod pod)
|
2023-11-06 13:33:47 +00:00
|
|
|
|
{
|
2024-04-13 14:09:17 +00:00
|
|
|
|
return K8s(c => c.GetPodInfo(pod.StartResult.Deployment));
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 10:55:09 +00:00
|
|
|
|
public CrashWatcher CreateCrashWatcher(RunningContainer container)
|
|
|
|
|
{
|
|
|
|
|
return K8s(c => c.CreateCrashWatcher(container));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
public void Stop(RunningPod runningPod, bool waitTillStopped)
|
2023-04-13 09:07:36 +00:00
|
|
|
|
{
|
2024-08-01 08:39:06 +00:00
|
|
|
|
if (runningPod.IsStopped) return;
|
|
|
|
|
foreach (var c in runningPod.Containers)
|
|
|
|
|
{
|
|
|
|
|
c.StopLog = DownloadContainerLog(c);
|
|
|
|
|
}
|
|
|
|
|
runningPod.IsStopped = true;
|
|
|
|
|
|
2023-04-13 09:07:36 +00:00
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2024-04-13 14:09:17 +00:00
|
|
|
|
controller.Stop(runningPod.StartResult, waitTillStopped);
|
2023-04-13 09:07:36 +00:00
|
|
|
|
});
|
2024-08-01 08:39:06 +00:00
|
|
|
|
|
|
|
|
|
cluster.Configuration.Hooks.OnContainersStopped(runningPod);
|
2023-04-13 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 08:39:14 +00:00
|
|
|
|
public void DownloadContainerLog(RunningContainer container, ILogHandler logHandler, int? tailLines = null, bool? previous = null)
|
2023-04-13 09:30:19 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2024-06-19 08:39:14 +00:00
|
|
|
|
controller.DownloadPodLog(container, logHandler, tailLines, previous);
|
2023-04-13 09:30:19 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 08:39:06 +00:00
|
|
|
|
public IDownloadedLog DownloadContainerLog(RunningContainer container, int? tailLines = null, bool? previous = null)
|
|
|
|
|
{
|
|
|
|
|
var msg = $"Downloading container log for '{container.Name}'";
|
|
|
|
|
log.Log(msg);
|
|
|
|
|
var logHandler = new WriteToFileLogHandler(log, msg);
|
|
|
|
|
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
|
|
|
|
controller.DownloadPodLog(container, logHandler, tailLines, previous);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new DownloadedLog(logHandler, container.Name);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 13:09:52 +00:00
|
|
|
|
public void DeleteNamespace(bool wait)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2024-06-06 13:09:52 +00:00
|
|
|
|
controller.DeleteNamespace(wait);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
});
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 13:09:52 +00:00
|
|
|
|
public void DeleteNamespacesStartingWith(string namespacePrefix, bool wait)
|
2023-05-03 12:18:37 +00:00
|
|
|
|
{
|
|
|
|
|
K8s(controller =>
|
|
|
|
|
{
|
2024-06-06 13:09:52 +00:00
|
|
|
|
controller.DeleteAllNamespacesStartingWith(namespacePrefix, wait);
|
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
|
|
|
|
|
2024-08-01 08:39:06 +00:00
|
|
|
|
return new RunningContainer(Guid.NewGuid().ToString(), 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
|
|
|
|
}
|