cs-codex-dist-tests/KubernetesWorkflow/ContainerRecipeFactory.cs

66 lines
2.0 KiB
C#
Raw Normal View History

2023-04-12 11:53:55 +00:00
namespace KubernetesWorkflow
{
public abstract class ContainerRecipeFactory
{
private readonly List<Port> exposedPorts = new List<Port>();
private readonly List<Port> internalPorts = new List<Port>();
private readonly List<EnvVar> envVars = new List<EnvVar>();
private RecipeComponentFactory factory = null!;
public ContainerRecipe CreateRecipe(int containerNumber, RecipeComponentFactory factory, StartupConfig config)
{
this.factory = factory;
ContainerNumber = containerNumber;
Initialize(config);
2023-04-13 08:11:33 +00:00
var recipe = new ContainerRecipe(containerNumber, Image, exposedPorts.ToArray(), internalPorts.ToArray(), envVars.ToArray());
exposedPorts.Clear();
internalPorts.Clear();
envVars.Clear();
this.factory = null!;
return recipe;
2023-04-12 11:53:55 +00:00
}
protected abstract string Image { get; }
protected int ContainerNumber { get; private set; } = 0;
protected abstract void Initialize(StartupConfig config);
2023-04-13 12:36:17 +00:00
protected Port AddExposedPort(string tag = "")
2023-04-12 11:53:55 +00:00
{
2023-04-13 12:36:17 +00:00
var p = factory.CreatePort(tag);
2023-04-12 11:53:55 +00:00
exposedPorts.Add(p);
return p;
}
2023-04-13 12:36:17 +00:00
protected Port AddInternalPort(string tag = "")
2023-04-12 11:53:55 +00:00
{
2023-04-13 12:36:17 +00:00
var p = factory.CreatePort(tag);
2023-04-12 11:53:55 +00:00
internalPorts.Add(p);
return p;
}
2023-04-13 12:36:17 +00:00
protected void AddExposedPortAndVar(string name, string tag = "")
2023-04-12 11:53:55 +00:00
{
2023-04-13 12:36:17 +00:00
AddEnvVar(name, AddExposedPort(tag));
2023-04-12 11:53:55 +00:00
}
2023-04-13 12:36:17 +00:00
protected void AddInternalPortAndVar(string name, string tag = "")
2023-04-12 11:53:55 +00:00
{
2023-04-13 12:36:17 +00:00
AddEnvVar(name, AddInternalPort(tag));
2023-04-12 11:53:55 +00:00
}
protected void AddEnvVar(string name, string value)
{
envVars.Add(factory.CreateEnvVar(name, value));
}
protected void AddEnvVar(string name, Port value)
{
envVars.Add(factory.CreateEnvVar(name, value.Number));
}
}
}