Skip to content

Commit

Permalink
[k8s] Default Service protocol to "TCP" (Azure#2929)
Browse files Browse the repository at this point in the history
If you create a module with Docker createOptions like so:
```json
{
  "ExposedPorts":{
    "35000":{}
  },
  "HostConfig":{
    "PortBindings":{
      "35000":[{"HostPort":"35000"}]
    }
  }
}
```

The Docker API will default the port to be "TCP".  Therefore, the Docker->K8s translation should do the same.
  • Loading branch information
darobs authored May 7, 2020
1 parent 412afc3 commit 368ffb9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Kubernetes.EdgeDeployment.Service

public class PortAndProtocol
{
static Option<PortAndProtocol> empty = Option.None<PortAndProtocol>();
static string defaultProtocol = "TCP";

public int Port { get; }

public string Protocol { get; }
Expand All @@ -21,18 +24,28 @@ public static Option<PortAndProtocol> Parse(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return Option.None<PortAndProtocol>();
return empty;
}

string[] portProtocol = name.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (portProtocol.Length != 2)
if (portProtocol.Length > 2)
{
return empty;
}

if (!int.TryParse(portProtocol[0], out int port))
{
return Option.None<PortAndProtocol>();
return empty;
}

if (!int.TryParse(portProtocol[0], out int port) || !SupportedProtocols.TryGetValue(portProtocol[1], out string protocol))
// Docker defaults to TCP if not specified.
string protocol = defaultProtocol;
if (portProtocol.Length > 1)
{
return Option.None<PortAndProtocol>();
if (!SupportedProtocols.TryGetValue(portProtocol[1], out protocol))
{
return empty;
}
}

return Option.Some(new PortAndProtocol(port, protocol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ public void UnableToParseUnsupportedProtocol()
[Fact]
public void UnableToParseInvalidPort()
{
var result = PortAndProtocol.Parse("1a23/HTTP");
var result = PortAndProtocol.Parse("1a23/TCP");

Assert.Equal(Option.None<PortAndProtocol>(), result);
}

[Fact]
public void UnableToParseTooManyParts()
{
var result = PortAndProtocol.Parse("10/tcp/udp");

Assert.Equal(Option.None<PortAndProtocol>(), result);
}

[Fact]
public void DefaultProtocolIsTCP()
{
var result = PortAndProtocol.Parse("3434").OrDefault();

Assert.Equal(3434, result.Port);
Assert.Equal("TCP", result.Protocol);
}

[Theory]
[InlineData("123/TCP", 123, "TCP")]
[InlineData("456/UDP", 456, "UDP")]
Expand Down

0 comments on commit 368ffb9

Please sign in to comment.