Skip to content

Commit ceac791

Browse files
erfrimodYongli Chen
authored andcommitted
Adding PortMapping support to Azure cni for Windows (#256)
1 parent 434ddf7 commit ceac791

File tree

5 files changed

+94
-41
lines changed

5 files changed

+94
-41
lines changed

cni/azure-windows.conflist

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
{
2-
"cniVersion":"0.3.0",
3-
"name":"azure",
4-
"plugins":[
5-
{
6-
"type":"azure-vnet",
7-
"mode":"bridge",
8-
"bridge":"azure0",
9-
"ipam":{
10-
"type":"azure-vnet-ipam"
11-
},
12-
"dns":{
13-
"Nameservers":[
14-
"10.0.0.10",
15-
"168.63.129.16"
16-
],
17-
"Search":[
18-
"svc.cluster.local"
19-
]
20-
},
21-
"AdditionalArgs":[
22-
{
23-
"Name":"EndpointPolicy",
24-
"Value":{
25-
"Type":"OutBoundNAT",
26-
"ExceptionList":[
27-
"10.240.0.0/16",
28-
"10.0.0.0/8"
29-
]
30-
}
2+
"cniVersion": "0.3.0",
3+
"name": "azure",
4+
"plugins": [
5+
{
6+
"type": "azure-vnet",
7+
"mode": "bridge",
8+
"bridge": "azure0",
9+
"capabilities": {
10+
"portMappings": true
11+
},
12+
"ipam": {
13+
"type": "azure-vnet-ipam"
3114
},
32-
{
33-
"Name":"EndpointPolicy",
34-
"Value":{
35-
"Type":"ROUTE",
36-
"DestinationPrefix":"10.0.0.0/8",
37-
"NeedEncap":true
38-
}
39-
}
40-
]
41-
}
42-
]
15+
"dns": {
16+
"Nameservers": [
17+
"10.0.0.10",
18+
"168.63.129.16"
19+
],
20+
"Search": [
21+
"svc.cluster.local"
22+
]
23+
},
24+
"AdditionalArgs": [
25+
{
26+
"Name": "EndpointPolicy",
27+
"Value": {
28+
"Type": "OutBoundNAT",
29+
"ExceptionList": [
30+
"10.240.0.0/16",
31+
"10.0.0.0/8"
32+
]
33+
}
34+
},
35+
{
36+
"Name": "EndpointPolicy",
37+
"Value": {
38+
"Type": "ROUTE",
39+
"DestinationPrefix": "10.0.0.0/8",
40+
"NeedEncap": true
41+
}
42+
}
43+
]
44+
}
45+
]
4346
}

cni/netconfig.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ type KVPair struct {
2222
Value json.RawMessage `json:"value"`
2323
}
2424

25+
type PortMapping struct {
26+
HostPort int `json:"hostPort"`
27+
ContainerPort int `json:"containerPort"`
28+
Protocol string `json:"protocol"`
29+
HostIp string `json:"hostIP,omitempty"`
30+
}
31+
32+
type RuntimeConfig struct {
33+
PortMappings []PortMapping `json:"portMappings,omitempty"`
34+
}
35+
2536
// NetworkConfig represents Azure CNI plugin network configuration.
2637
type NetworkConfig struct {
2738
CNIVersion string `json:"cniVersion"`
@@ -45,7 +56,8 @@ type NetworkConfig struct {
4556
Address string `json:"ipAddress,omitempty"`
4657
QueryInterval string `json:"queryInterval,omitempty"`
4758
}
48-
DNS cniTypes.DNS `json:"dns"`
59+
DNS cniTypes.DNS `json:"dns"`
60+
RuntimeConfig RuntimeConfig `json:"runtimeConfig"`
4961
AdditionalArgs []KVPair
5062
}
5163

cni/network/network.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
407407
DNS: epDNSInfo,
408408
Policies: policies,
409409
}
410+
411+
epPolicies := getPoliciesFromRuntimeCfg(nwCfg)
412+
for _, epPolicy := range epPolicies {
413+
epInfo.Policies = append(epInfo.Policies, epPolicy)
414+
}
410415

411416
// Populate addresses.
412417
for _, ipconfig := range result.IPs {

cni/network/network_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/Azure/azure-container-networking/cns"
99
"github.com/Azure/azure-container-networking/log"
1010
"github.com/Azure/azure-container-networking/network"
11+
"github.com/Azure/azure-container-networking/network/policy"
1112
cniTypes "github.com/containernetworking/cni/pkg/types"
1213
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
1314
)
@@ -101,3 +102,9 @@ func getNetworkDNSSettings(nwCfg *cni.NetworkConfig, result *cniTypesCurr.Result
101102
func getEndpointDNSSettings(nwCfg *cni.NetworkConfig, result *cniTypesCurr.Result, namespace string) (network.DNSInfo, error) {
102103
return getNetworkDNSSettings(nwCfg, result, namespace)
103104
}
105+
106+
// getPoliciesFromRuntimeCfg returns network policies from network config.
107+
// getPoliciesFromRuntimeCfg is a dummy function for Linux platform.
108+
func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig) []policy.Policy {
109+
return nil
110+
}

cni/network/network_windows.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package network
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net"
67
"strings"
@@ -9,6 +10,7 @@ import (
910
"github.com/Azure/azure-container-networking/cns"
1011
"github.com/Azure/azure-container-networking/log"
1112
"github.com/Azure/azure-container-networking/network"
13+
"github.com/Azure/azure-container-networking/network/policy"
1214
"github.com/Microsoft/hcsshim"
1315

1416
cniTypes "github.com/containernetworking/cni/pkg/types"
@@ -120,3 +122,27 @@ func getEndpointDNSSettings(nwCfg *cni.NetworkConfig, result *cniTypesCurr.Resul
120122

121123
return epDNS, nil
122124
}
125+
126+
// getPoliciesFromRuntimeCfg returns network policies from network config.
127+
func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig) []policy.Policy {
128+
log.Printf("[net] RuntimeConfigs: %+v", nwCfg.RuntimeConfig)
129+
var policies []policy.Policy
130+
for _, mapping := range nwCfg.RuntimeConfig.PortMappings {
131+
rawPolicy, _ := json.Marshal(&hcsshim.NatPolicy{
132+
Type: "NAT",
133+
ExternalPort: uint16(mapping.HostPort),
134+
InternalPort: uint16(mapping.ContainerPort),
135+
Protocol: mapping.Protocol,
136+
})
137+
138+
policy := policy.Policy{
139+
Type: policy.EndpointPolicy,
140+
Data: rawPolicy,
141+
}
142+
log.Printf("[net] Creating port mapping policy: %+v", policy)
143+
144+
policies = append(policies, policy)
145+
}
146+
147+
return policies
148+
}

0 commit comments

Comments
 (0)