11package network
22
33import (
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