diff --git a/cni/network/network.go b/cni/network/network.go index 7afaf7bf01..7b1b570973 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -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 @@ -173,7 +179,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { // Initialize values from network config. networkId := nwCfg.Name - endpointId := network.GetEndpointID(args) + endpointId := GetEndpointID(args) nwInfo, nwInfoErr := plugin.nm.GetNetworkInfo(networkId) @@ -359,7 +365,7 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error { // Initialize values from network config. networkId := nwCfg.Name - endpointId := network.GetEndpointID(args) + endpointId := GetEndpointID(args) // Query the network. nwInfo, err := plugin.nm.GetNetworkInfo(networkId) diff --git a/cni/network/network_linux.go b/cni/network/network_linux.go index c3b0c858be..aa8110ec52 100644 --- a/cni/network/network_linux.go +++ b/cni/network/network_linux.go @@ -1,10 +1,12 @@ package network import ( + "github.com/Azure/azure-container-networking/cni" + "github.com/Azure/azure-container-networking/network" cniTypesCurr "github.com/containernetworking/cni/pkg/types/current" ) // handleConsecutiveAdd is a dummy function for Linux platform. -func handleConsecutiveAdd(containerId, endpointId string, nwInfo *NetworkInfo, nwCfg *NetworkConfig) (*cniTypesCurr.Result, error) { +func handleConsecutiveAdd(containerId, endpointId string, nwInfo *network.NetworkInfo, nwCfg *cni.NetworkConfig) (*cniTypesCurr.Result, error) { return nil, nil } diff --git a/network/endpoint.go b/network/endpoint.go index 8631006c2d..a1d2f1c753 100644 --- a/network/endpoint.go +++ b/network/endpoint.go @@ -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. @@ -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. diff --git a/network/endpoint_windows.go b/network/endpoint_windows.go index cd1e309f89..80a57e47f3 100644 --- a/network/endpoint_windows.go +++ b/network/endpoint_windows.go @@ -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) @@ -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, diff --git a/network/policy/policy.go b/network/policy/policy.go new file mode 100644 index 0000000000..3143184a62 --- /dev/null +++ b/network/policy/policy.go @@ -0,0 +1,23 @@ +package policy + +import ( + "encoding/json" +) + +type CNIPolicyType string + +type Policy struct { + Type CNIPolicyType + Data json.RawMessage +} + +// SerializePolicies serializes policies to json. +func SerializePolicies(policyType CNIPolicyType, policies []Policy) []json.RawMessage { + var jsonPolicies []json.RawMessage + for _, policy := range policies { + if policy.Type == policyType { + jsonPolicies = append(jsonPolicies, policy.Data) + } + } + return jsonPolicies +} diff --git a/network/policy/policy_linux.go b/network/policy/policy_linux.go deleted file mode 100644 index 207f2e7b44..0000000000 --- a/network/policy/policy_linux.go +++ /dev/null @@ -1,3 +0,0 @@ -package policy - -type Policy struct{} diff --git a/network/policy/policy_windows.go b/network/policy/policy_windows.go index 90cc65cf6c..9121d45927 100644 --- a/network/policy/policy_windows.go +++ b/network/policy/policy_windows.go @@ -1,29 +1,7 @@ package policy -import ( - "encoding/json" -) - -type CNIPolicyType string - const ( NetworkPolicy CNIPolicyType = "NetworkPolicy" EndpointPolicy CNIPolicyType = "EndpointPolicy" OutBoundNatPolicy CNIPolicyType = "OutBoundNatPolicy" ) - -type Policy struct { - Type CNIPolicyType - Data json.RawMessage -} - -// SerializePolicies serializes policies to json. -func SerializePolicies(policyType CNIPolicyType, policies []Policy) []json.RawMessage { - var jsonPolicies []json.RawMessage - for _, policy := range policies { - if policy.Type == policyType { - jsonPolicies = append(jsonPolicies, policy.Data) - } - } - return jsonPolicies -}