Skip to content

Commit

Permalink
fix: [CNI] NAT hostPort mapping for HNSv2 (#1922)
Browse files Browse the repository at this point in the history
* NAT update

* NAT update

* Cleaning up and unit test

* Fix Lint

* Fix Lint

* Fix Lint - Addr. comments

---------

Co-authored-by: jpayne3506 <[email protected]>
  • Loading branch information
jpayne3506 and jpayne3506 authored May 1, 2023
1 parent 2df9d27 commit 4f76b19
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cni/network/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,14 @@ func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig) []policy.Policy {
protocol = policy.ProtocolUdp
}

// To support hostport policy mapping
// uint32 NatFlagsLocalRoutedVip = 1
rawPolicy, _ := json.Marshal(&hnsv2.PortMappingPolicySetting{
ExternalPort: uint16(mapping.HostPort),
InternalPort: uint16(mapping.ContainerPort),
VIP: mapping.HostIp,
Protocol: protocol,
Flags: hnsv2.NatFlagsLocalRoutedVip,
})

hnsv2Policy, _ := json.Marshal(&hnsv2.EndpointPolicy{
Expand Down
15 changes: 14 additions & 1 deletion cni/network/network_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,21 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
},
},
},
{
name: "Runtime hostPort mapping polices",
nwCfg: cni.NetworkConfig{
RuntimeConfig: cni.RuntimeConfig{
PortMappings: []cni.PortMapping{
{
Protocol: "tcp",
HostPort: 44000,
ContainerPort: 80,
},
},
},
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 4f76b19

Please sign in to comment.