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: 6 additions & 0 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func (plugin *netPlugin) findMasterInterface(nwCfg *cni.NetworkConfig, subnetPre
return ""
}

// GetEndpointID returns a unique endpoint ID based on the CNI args.
func GetEndpointID(args *cniSkel.CmdArgs) string {
infraEpId, _ := network.ConstructEndpointID(args.ContainerID, args.Netns, args.IfName)
return infraEpId
}

//
// CNI implementation
// https://github.com/containernetworking/cni/blob/master/SPEC.md
Expand Down
29 changes: 24 additions & 5 deletions network/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package network

import (
"net"
"strings"

"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/network/policy"
cniSkel "github.com/containernetworking/cni/pkg/skel"
)

// Endpoint represents a container network interface.
Expand Down Expand Up @@ -42,10 +42,29 @@ type RouteInfo struct {
Gw net.IP
}

// GetEndpointID returns a unique endpoint ID based on the CNI args.
func GetEndpointID(args *cniSkel.CmdArgs) string {
infraEpId, _ := ConstructEpName(args.ContainerID, args.Netns, args.IfName)
return infraEpId
// ConstructEndpointID constructs endpoint name from netNsPath.
func ConstructEndpointID(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
}

// NewEndpoint creates a new endpoint in the network.
Expand Down
27 changes: 1 addition & 26 deletions network/endpoint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,6 @@ 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
}

// HotAttachEndpoint is a wrapper of hcsshim's HotAttachEndpoint.
func (endpoint *EndpointInfo) HotAttachEndpoint(containerID string) error {
return hcsshim.HotAttachEndpoint(containerID, endpoint.Id)
Expand All @@ -48,7 +23,7 @@ func (endpoint *EndpointInfo) HotAttachEndpoint(containerID string) error {
// newEndpointImpl creates a new endpoint in the network.
func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
// Get Infrastructure containerID. Handle ADD calls for workload container.
infraEpName, _ := ConstructEpName(epInfo.ContainerID, epInfo.NetNsPath, epInfo.IfName)
infraEpName, _ := ConstructEndpointID(epInfo.ContainerID, epInfo.NetNsPath, epInfo.IfName)

hnsEndpoint := &hcsshim.HNSEndpoint{
Name: infraEpName,
Expand Down