Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions network/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,13 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
}
}()

if nw.Endpoints[epInfo.Id] != nil {
err = errEndpointExists
return nil, err
}

// Call the platform implementation.
ep, err = nw.newEndpointImpl(epInfo)
if err != nil {
return nil, err
}

nw.Endpoints[epInfo.Id] = ep

log.Printf("[net] Created endpoint %+v.", ep)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line also. not needed

return ep, nil
Expand Down
6 changes: 6 additions & 0 deletions network/endpoint_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
var ep *endpoint
var err error

if nw.Endpoints[epInfo.Id] != nil {
log.Printf("[net] Endpoint alreday exists.")
err = errEndpointExists
return nil, err
}

// Create a veth pair.
hostIfName := fmt.Sprintf("%s%s", hostVEthInterfacePrefix, epInfo.Id[:7])
contIfName := fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, epInfo.Id[:7])
Expand Down
63 changes: 59 additions & 4 deletions network/endpoint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,71 @@ import (
"github.com/Microsoft/hcsshim"
)

// ConstructEpName constructs endpoint name from netNsPath.
func ConstructEpName(containerID string, netNsPath string, ifName string) (string, string) {
infraEpName, workloadEpName := "", ""

if len(containerID) > 8 {
containerID = containerID[:8]
}

if netNsPath != "" {
splits := strings.Split(netNsPath, ":")
// For workload containers, we extract its linking infrastructure container ID.
if len(splits) == 2 {
if len(splits[1]) > 8 {
splits[1] = splits[1][:8]
}
infraEpName = splits[1] + "-" + ifName
workloadEpName = containerID + "-" + ifName
} else {
// For infrastructure containers, we just use its container ID.
infraEpName = containerID + "-" + ifName
}
}
return infraEpName, workloadEpName
}

// newEndpointImpl creates a new endpoint in the network.
func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
// Initialize HNS endpoint.
hnsEndpoint := &hcsshim.HNSEndpoint{
Name: epInfo.Id,
// Get Infrastructure containerID. Handle ADD calls for workload container.
infraEpName, workloadEpName := ConstructEpName(epInfo.ContainerID, epInfo.NetNsPath, epInfo.IfName)

/* Handle consecutive ADD calls for infrastructure containers.
* This is a temporary work around for issue #57253 of Kubernetes.
* We can delete this if statement once they fix it.
* Issue link: https://github.com/kubernetes/kubernetes/issues/57253
*/
if workloadEpName == "" {
if nw.Endpoints[infraEpName] != nil {
log.Printf("[net] Found existing endpoint %v, return immediately.", infraEpName)
return nw.Endpoints[infraEpName], nil
}
}

log.Printf("[net] infraEpName: %v", infraEpName)

hnsEndpoint, _ := hcsshim.GetHNSEndpointByName(infraEpName)
if hnsEndpoint != nil {
log.Printf("[net] Found existing endpoint through hcsshim%v", infraEpName)
log.Printf("[net] Attaching ep %v to container %v", hnsEndpoint.Id, epInfo.ContainerID)
if err := hcsshim.HotAttachEndpoint(epInfo.ContainerID, hnsEndpoint.Id); err != nil {
return nil, err
}
return nw.Endpoints[infraEpName], nil
}

hnsEndpoint = &hcsshim.HNSEndpoint{
Name: infraEpName,
VirtualNetwork: nw.HnsId,
DNSSuffix: epInfo.DNS.Suffix,
DNSServerList: strings.Join(epInfo.DNS.Servers, ","),
}

//enable outbound NAT
var enableOutBoundNat = json.RawMessage(`{"Type": "OutBoundNAT"}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have unit test for testing this function? If its there, can we add one to test this property is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't write unit test for this. We can discuss this.

hnsEndpoint.Policies = append(hnsEndpoint.Policies, enableOutBoundNat)

// HNS currently supports only one IP address per endpoint.
if epInfo.IPAddresses != nil {
hnsEndpoint.IPAddress = epInfo.IPAddresses[0].IP
Expand Down Expand Up @@ -55,7 +110,7 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {

// Create the endpoint object.
ep := &endpoint{
Id: epInfo.Id,
Id: infraEpName,
HnsId: hnsResponse.Id,
SandboxKey: epInfo.ContainerID,
IfName: epInfo.IfName,
Expand Down