2023-04-12 11:53:55 +00:00
|
|
|
|
using System.Globalization;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
using Utils;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-11-12 09:07:23 +00:00
|
|
|
|
namespace KubernetesWorkflow.Recipe
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
public class RecipeComponentFactory
|
|
|
|
|
{
|
2023-11-14 09:49:14 +00:00
|
|
|
|
private readonly NumberSource internalNumberSource = new NumberSource(8080);
|
|
|
|
|
private static readonly NumberSource externalNumberSource = new NumberSource(30000);
|
|
|
|
|
private static int[] usedExternalPorts = Array.Empty<int>();
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-11-14 09:49:14 +00:00
|
|
|
|
public void Update(K8sController controller)
|
2023-08-11 07:37:30 +00:00
|
|
|
|
{
|
2023-11-14 09:49:14 +00:00
|
|
|
|
usedExternalPorts = controller.GetUsedExternalPorts();
|
2023-08-11 07:37:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 14:27:52 +00:00
|
|
|
|
public Port CreateInternalPort(string tag, PortProtocol protocol)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-11-13 14:27:52 +00:00
|
|
|
|
return new Port(internalNumberSource.GetNextNumber(), tag, protocol);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 09:49:14 +00:00
|
|
|
|
public Port CreateExternalPort(int number, string tag, PortProtocol protocol)
|
|
|
|
|
{
|
|
|
|
|
if (usedExternalPorts.Contains(number)) throw new Exception($"External port number {number} is already in use by the cluster.");
|
|
|
|
|
return new Port(number, tag, protocol);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 14:27:52 +00:00
|
|
|
|
public Port CreateExternalPort(string tag, PortProtocol protocol)
|
|
|
|
|
{
|
2023-11-14 09:49:14 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
var number = externalNumberSource.GetNextNumber();
|
|
|
|
|
if (!usedExternalPorts.Contains(number))
|
|
|
|
|
{
|
|
|
|
|
return new Port(number, tag, protocol);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EnvVar CreateEnvVar(string name, int value)
|
|
|
|
|
{
|
|
|
|
|
return CreateEnvVar(name, value.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EnvVar CreateEnvVar(string name, string value)
|
|
|
|
|
{
|
|
|
|
|
return new EnvVar(name, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|