Sets up support for UDP ports and applies them to discovery ports.

This commit is contained in:
benbierens 2023-10-25 14:23:07 +02:00
parent 4102ce0a04
commit 3914d58a6a
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
6 changed files with 82 additions and 42 deletions

View File

@ -59,14 +59,26 @@
public class Port
{
public Port(int number, string tag)
public Port(int number, string tag, PortProtocol protocol)
{
Number = number;
Tag = tag;
Protocol = protocol;
}
public int Number { get; }
public string Tag { get; }
public PortProtocol Protocol { get; }
public bool IsTcp()
{
return Protocol == PortProtocol.TCP;
}
public bool IsUdp()
{
return Protocol == PortProtocol.UDP;
}
public override string ToString()
{
@ -75,6 +87,12 @@
}
}
public enum PortProtocol
{
TCP,
UDP
}
public class EnvVar
{
public EnvVar(string name, string value)

View File

@ -50,31 +50,31 @@ namespace KubernetesWorkflow
protected int Index { get; private set; } = 0;
protected abstract void Initialize(StartupConfig config);
protected Port AddExposedPort(string tag)
protected Port AddExposedPort(string tag, PortProtocol protocol = PortProtocol.TCP)
{
return AddExposedPort(factory.CreatePort(tag));
return AddExposedPort(factory.CreatePort(tag, protocol));
}
protected Port AddExposedPort(int number, string tag)
protected Port AddExposedPort(int number, string tag, PortProtocol protocol = PortProtocol.TCP)
{
return AddExposedPort(factory.CreatePort(number, tag));
return AddExposedPort(factory.CreatePort(number, tag, protocol));
}
protected Port AddInternalPort(string tag = "")
protected Port AddInternalPort(string tag = "", PortProtocol protocol = PortProtocol.TCP)
{
var p = factory.CreatePort(tag);
var p = factory.CreatePort(tag, protocol);
internalPorts.Add(p);
return p;
}
protected void AddExposedPortAndVar(string name, string tag)
protected void AddExposedPortAndVar(string name, string tag, PortProtocol protocol = PortProtocol.TCP)
{
AddEnvVar(name, AddExposedPort(tag));
AddEnvVar(name, AddExposedPort(tag, protocol));
}
protected void AddInternalPortAndVar(string name, string tag = "")
protected void AddInternalPortAndVar(string name, string tag = "", PortProtocol protocol = PortProtocol.TCP)
{
AddEnvVar(name, AddInternalPort(tag));
AddEnvVar(name, AddInternalPort(tag, protocol));
}
protected void AddEnvVar(string name, string value)

View File

@ -506,23 +506,42 @@ namespace KubernetesWorkflow
private List<V1ContainerPort> CreateContainerPorts(ContainerRecipe recipe)
{
var exposedPorts = recipe.ExposedPorts.Select(p => CreateContainerPort(recipe, p));
var internalPorts = recipe.InternalPorts.Select(p => CreateContainerPort(recipe, p));
var exposedPorts = recipe.ExposedPorts.SelectMany(p => CreateContainerPort(recipe, p));
var internalPorts = recipe.InternalPorts.SelectMany(p => CreateContainerPort(recipe, p));
return exposedPorts.Concat(internalPorts).ToList();
}
private V1ContainerPort CreateContainerPort(ContainerRecipe recipe, Port port)
private List<V1ContainerPort> CreateContainerPort(ContainerRecipe recipe, Port port)
{
var result = new List<V1ContainerPort>();
if (port.IsTcp()) CreateTcpContainerPort(result, recipe, port);
if (port.IsUdp()) CreateUdpContainerPort(result, recipe, port);
return result;
}
private void CreateUdpContainerPort(List<V1ContainerPort> result, ContainerRecipe recipe, Port port)
{
result.Add(CreateContainerPort(recipe, port, "UDP"));
}
private void CreateTcpContainerPort(List<V1ContainerPort> result, ContainerRecipe recipe, Port port)
{
result.Add(CreateContainerPort(recipe, port, "TCP"));
}
private V1ContainerPort CreateContainerPort(ContainerRecipe recipe, Port port, string protocol)
{
return new V1ContainerPort
{
Name = GetNameForPort(recipe, port),
ContainerPort = port.Number
ContainerPort = port.Number,
Protocol = protocol
};
}
private string GetNameForPort(ContainerRecipe recipe, Port port)
{
return $"p{workflowNumberSource.WorkflowNumber}-{recipe.Number}-{port.Number}";
return $"p{workflowNumberSource.WorkflowNumber}-{recipe.Number}-{port.Number}-{port.Protocol.ToString().ToLowerInvariant()}";
}
#endregion
@ -575,7 +594,7 @@ namespace KubernetesWorkflow
if (matchingServicePorts.Any())
{
// These service ports belongs to this recipe.
var optionals = matchingServicePorts.Select(p => MapNodePortIfAble(p, port.Tag));
var optionals = matchingServicePorts.Select(p => MapNodePortIfAble(p, port.Tag, port.Protocol));
var ports = optionals.Where(p => p != null).Select(p => p!).ToArray();
result.Add(new ContainerRecipePortMapEntry(r.Number, ports));
@ -584,10 +603,10 @@ namespace KubernetesWorkflow
}
}
private Port? MapNodePortIfAble(V1ServicePort p, string tag)
private Port? MapNodePortIfAble(V1ServicePort p, string tag, PortProtocol protocol)
{
if (p.NodePort == null) return null;
return new Port(p.NodePort.Value, tag);
return new Port(p.NodePort.Value, tag, protocol);
}
private void DeleteService(string serviceName)
@ -619,18 +638,24 @@ namespace KubernetesWorkflow
var result = new List<V1ServicePort>();
foreach (var port in recipe.ExposedPorts)
{
result.Add(new V1ServicePort
{
Name = GetNameForPort(recipe, port),
Protocol = "TCP",
Port = port.Number,
TargetPort = GetNameForPort(recipe, port),
});
if (port.IsTcp()) CreateServicePort(result, recipe, port, "TCP");
if (port.IsUdp()) CreateServicePort(result, recipe, port, "UDP");
}
return result;
}
private void CreateServicePort(List<V1ServicePort> result, ContainerRecipe recipe, Port port, string protocol)
{
result.Add(new V1ServicePort
{
Name = GetNameForPort(recipe, port),
Protocol = "TCP",
Port = port.Number,
TargetPort = GetNameForPort(recipe, port),
});
}
#endregion
#region Waiting

View File

@ -7,14 +7,14 @@ namespace KubernetesWorkflow
{
private NumberSource portNumberSource = new NumberSource(8080);
public Port CreatePort(int number, string tag)
public Port CreatePort(int number, string tag, PortProtocol protocol)
{
return new Port(number, tag);
return new Port(number, tag, protocol);
}
public Port CreatePort(string tag)
public Port CreatePort(string tag, PortProtocol protocol)
{
return new Port(portNumberSource.GetNextNumber(), tag);
return new Port(portNumberSource.GetNextNumber(), tag, protocol);
}
public EnvVar CreateEnvVar(string name, int value)

View File

@ -119,6 +119,12 @@ namespace CodexPlugin
}
}
private Port CreateApiPort(CodexStartupConfig config, string tag)
{
if (config.PublicTestNet == null) return AddExposedPort(tag);
return AddInternalPort(tag);
}
private Port CreateListenPort(CodexStartupConfig config)
{
if (config.PublicTestNet == null) return AddInternalPort(ListenPortTag);
@ -128,9 +134,9 @@ namespace CodexPlugin
private Port CreateDiscoveryPort(CodexStartupConfig config)
{
if (config.PublicTestNet == null) return AddInternalPort(DiscoveryPortTag);
if (config.PublicTestNet == null) return AddInternalPort(DiscoveryPortTag, PortProtocol.UDP);
return AddExposedPort(config.PublicTestNet.PublicDiscoveryPort, DiscoveryPortTag);
return AddExposedPort(config.PublicTestNet.PublicDiscoveryPort, DiscoveryPortTag, PortProtocol.UDP);
}
private ByteSize GetVolumeCapacity(CodexStartupConfig config)
@ -147,14 +153,5 @@ namespace CodexPlugin
if (!string.IsNullOrEmpty(DockerImageOverride)) return DockerImageOverride;
return DefaultDockerImage;
}
private Port CreateApiPort(CodexStartupConfig config, string tag)
{
if (config.PublicTestNet == null)
{
return AddExposedPort(tag);
}
return AddInternalPort(tag);
}
}
}

View File

@ -70,7 +70,7 @@ namespace GethPlugin
{
if (config.IsPublicTestNet == null) return AddInternalPort(DiscoveryPortTag);
return AddExposedPort(config.IsPublicTestNet.DiscoveryPort, DiscoveryPortTag);
return AddExposedPort(config.IsPublicTestNet.DiscoveryPort, DiscoveryPortTag, PortProtocol.UDP);
}
private Port CreateP2pPort(GethStartupConfig config, string tag)