diff --git a/Framework/KubernetesWorkflow/ContainerRecipe.cs b/Framework/KubernetesWorkflow/ContainerRecipe.cs index ed89d78..2ff8605 100644 --- a/Framework/KubernetesWorkflow/ContainerRecipe.cs +++ b/Framework/KubernetesWorkflow/ContainerRecipe.cs @@ -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) diff --git a/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs b/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs index 6e50a1b..5fa9980 100644 --- a/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs +++ b/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs @@ -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) diff --git a/Framework/KubernetesWorkflow/K8sController.cs b/Framework/KubernetesWorkflow/K8sController.cs index 0e06ba4..2b4c3b9 100644 --- a/Framework/KubernetesWorkflow/K8sController.cs +++ b/Framework/KubernetesWorkflow/K8sController.cs @@ -506,23 +506,42 @@ namespace KubernetesWorkflow private List 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 CreateContainerPort(ContainerRecipe recipe, Port port) + { + var result = new List(); + if (port.IsTcp()) CreateTcpContainerPort(result, recipe, port); + if (port.IsUdp()) CreateUdpContainerPort(result, recipe, port); + return result; + } + + private void CreateUdpContainerPort(List result, ContainerRecipe recipe, Port port) + { + result.Add(CreateContainerPort(recipe, port, "UDP")); + } + + private void CreateTcpContainerPort(List 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(); 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 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 diff --git a/Framework/KubernetesWorkflow/RecipeComponentFactory.cs b/Framework/KubernetesWorkflow/RecipeComponentFactory.cs index 9644515..71f37b3 100644 --- a/Framework/KubernetesWorkflow/RecipeComponentFactory.cs +++ b/Framework/KubernetesWorkflow/RecipeComponentFactory.cs @@ -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) diff --git a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs index 1397303..0a6223b 100644 --- a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs +++ b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs @@ -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); - } } } diff --git a/ProjectPlugins/GethPlugin/GethContainerRecipe.cs b/ProjectPlugins/GethPlugin/GethContainerRecipe.cs index 7df3855..79329b4 100644 --- a/ProjectPlugins/GethPlugin/GethContainerRecipe.cs +++ b/ProjectPlugins/GethPlugin/GethContainerRecipe.cs @@ -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)