@@ -14,16 +14,71 @@ import (
1414 "github.com/Microsoft/hcsshim"
1515)
1616
17+ // ConstructEpName constructs endpoint name from netNsPath.
18+ func ConstructEpName (containerID string , netNsPath string , ifName string ) (string , string ) {
19+ infraEpName , workloadEpName := "" , ""
20+
21+ if len (containerID ) > 8 {
22+ containerID = containerID [:8 ]
23+ }
24+
25+ if netNsPath != "" {
26+ splits := strings .Split (netNsPath , ":" )
27+ // For workload containers, we extract its linking infrastructure container ID.
28+ if len (splits ) == 2 {
29+ if len (splits [1 ]) > 8 {
30+ splits [1 ] = splits [1 ][:8 ]
31+ }
32+ infraEpName = splits [1 ] + "-" + ifName
33+ workloadEpName = containerID + "-" + ifName
34+ } else {
35+ // For infrastructure containers, we just use its container ID.
36+ infraEpName = containerID + "-" + ifName
37+ }
38+ }
39+ return infraEpName , workloadEpName
40+ }
41+
1742// newEndpointImpl creates a new endpoint in the network.
1843func (nw * network ) newEndpointImpl (epInfo * EndpointInfo ) (* endpoint , error ) {
19- // Initialize HNS endpoint.
20- hnsEndpoint := & hcsshim.HNSEndpoint {
21- Name : epInfo .Id ,
44+ // Get Infrastructure containerID. Handle ADD calls for workload container.
45+ infraEpName , workloadEpName := ConstructEpName (epInfo .ContainerID , epInfo .NetNsPath , epInfo .IfName )
46+
47+ /* Handle consecutive ADD calls for infrastructure containers.
48+ * This is a temporary work around for issue #57253 of Kubernetes.
49+ * We can delete this if statement once they fix it.
50+ * Issue link: https://github.com/kubernetes/kubernetes/issues/57253
51+ */
52+ if workloadEpName == "" {
53+ if nw .Endpoints [infraEpName ] != nil {
54+ log .Printf ("[net] Found existing endpoint %v, return immediately." , infraEpName )
55+ return nw .Endpoints [infraEpName ], nil
56+ }
57+ }
58+
59+ log .Printf ("[net] infraEpName: %v" , infraEpName )
60+
61+ hnsEndpoint , _ := hcsshim .GetHNSEndpointByName (infraEpName )
62+ if hnsEndpoint != nil {
63+ log .Printf ("[net] Found existing endpoint through hcsshim%v" , infraEpName )
64+ log .Printf ("[net] Attaching ep %v to container %v" , hnsEndpoint .Id , epInfo .ContainerID )
65+ if err := hcsshim .HotAttachEndpoint (epInfo .ContainerID , hnsEndpoint .Id ); err != nil {
66+ return nil , err
67+ }
68+ return nw .Endpoints [infraEpName ], nil
69+ }
70+
71+ hnsEndpoint = & hcsshim.HNSEndpoint {
72+ Name : infraEpName ,
2273 VirtualNetwork : nw .HnsId ,
2374 DNSSuffix : epInfo .DNS .Suffix ,
2475 DNSServerList : strings .Join (epInfo .DNS .Servers , "," ),
2576 }
2677
78+ //enable outbound NAT
79+ var enableOutBoundNat = json .RawMessage (`{"Type": "OutBoundNAT"}` )
80+ hnsEndpoint .Policies = append (hnsEndpoint .Policies , enableOutBoundNat )
81+
2782 // HNS currently supports only one IP address per endpoint.
2883 if epInfo .IPAddresses != nil {
2984 hnsEndpoint .IPAddress = epInfo .IPAddresses [0 ].IP
@@ -55,7 +110,7 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
55110
56111 // Create the endpoint object.
57112 ep := & endpoint {
58- Id : epInfo . Id ,
113+ Id : infraEpName ,
59114 HnsId : hnsResponse .Id ,
60115 SandboxKey : epInfo .ContainerID ,
61116 IfName : epInfo .IfName ,
0 commit comments