Skip to content

Commit 4bc51cc

Browse files
saiyan86sharmasushant
authored andcommitted
Update endpoint creation in CNI for Windows containers. (#86)
Enable outboundNAT for Windows containers. Handle consecutive ADD calls for the same container. Handle attach endpoint for workload containers.
1 parent 86496fb commit 4bc51cc

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

network/endpoint.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,13 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
5151
}
5252
}()
5353

54-
if nw.Endpoints[epInfo.Id] != nil {
55-
err = errEndpointExists
56-
return nil, err
57-
}
58-
5954
// Call the platform implementation.
6055
ep, err = nw.newEndpointImpl(epInfo)
6156
if err != nil {
6257
return nil, err
6358
}
6459

6560
nw.Endpoints[epInfo.Id] = ep
66-
6761
log.Printf("[net] Created endpoint %+v.", ep)
6862

6963
return ep, nil

network/endpoint_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
3232
var ep *endpoint
3333
var err error
3434

35+
if nw.Endpoints[epInfo.Id] != nil {
36+
log.Printf("[net] Endpoint alreday exists.")
37+
err = errEndpointExists
38+
return nil, err
39+
}
40+
3541
// Create a veth pair.
3642
hostIfName := fmt.Sprintf("%s%s", hostVEthInterfacePrefix, epInfo.Id[:7])
3743
contIfName := fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, epInfo.Id[:7])

network/endpoint_windows.go

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1843
func (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

Comments
 (0)