From 69fde1c3422e0d9829e2b53ea6c2f62a4abeffd3 Mon Sep 17 00:00:00 2001 From: tamilmani1989 Date: Wed, 9 Jan 2019 18:29:22 -0800 Subject: [PATCH] changed vethnaming logic for transparent mode (#286) --- cni/network/network.go | 15 +++++++++++---- network/endpoint_linux.go | 12 +++++++++++- network/transparent_endpointclient_linux.go | 9 +++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cni/network/network.go b/cni/network/network.go index 0166acf228..c8e7376008 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -25,7 +25,7 @@ const ( // Plugin name. name = "azure-vnet" dockerNetworkOption = "com.docker.network.generic" - + opModeTransparent = "transparent" // Supported IP version. Currently support only IPv4 ipVersion = "4" ) @@ -454,9 +454,16 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { SetupRoutingForMultitenancy(nwCfg, cnsNetworkConfig, azIpamResult, epInfo, result) - // A runtime must not call ADD twice (without a corresponding DEL) for the same - // (network name, container id, name of the interface inside the container) - vethName = fmt.Sprintf("%s%s%s", networkId, k8sContainerID, k8sIfName) + if nwCfg.Mode == opModeTransparent { + // this mechanism of using only namespace and name is not unique for different incarnations of POD/container. + // IT will result in unpredictable behavior if API server decides to + // reorder DELETE and ADD call for new incarnation of same POD. + vethName = fmt.Sprintf("%s.%s", k8sNamespace, k8sPodName) + } else { + // A runtime must not call ADD twice (without a corresponding DEL) for the same + // (network name, container id, name of the interface inside the container) + vethName = fmt.Sprintf("%s%s%s", networkId, k8sContainerID, k8sIfName) + } setEndpointOptions(cnsNetworkConfig, epInfo, vethName) // Create the endpoint. diff --git a/network/endpoint_linux.go b/network/endpoint_linux.go index a8e64e4dac..0c39d0de15 100644 --- a/network/endpoint_linux.go +++ b/network/endpoint_linux.go @@ -68,8 +68,8 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) { } if _, ok := epInfo.Data[OptVethName]; ok { - log.Printf("Generate veth name based on the key provided") key := epInfo.Data[OptVethName].(string) + log.Printf("Generate veth name based on the key provided %v", key) vethname := generateVethName(key) hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, vethname) contIfName = fmt.Sprintf("%s%s2", hostVEthInterfacePrefix, vethname) @@ -270,8 +270,18 @@ func deleteRoutes(interfaceName string, routes []RouteInfo) error { if route.DevName != "" { devIf, _ := net.InterfaceByName(route.DevName) + if devIf == nil { + log.Printf("[net] Not deleting route. Interface %v doesn't exist", interfaceName) + continue + } + ifIndex = devIf.Index } else { + if interfaceIf == nil { + log.Printf("[net] Not deleting route. Interface %v doesn't exist", interfaceName) + continue + } + ifIndex = interfaceIf.Index } diff --git a/network/transparent_endpointclient_linux.go b/network/transparent_endpointclient_linux.go index e41c51d5fe..f0a48099bd 100644 --- a/network/transparent_endpointclient_linux.go +++ b/network/transparent_endpointclient_linux.go @@ -52,6 +52,15 @@ func setArpProxy(ifName string) error { } func (client *TransparentEndpointClient) AddEndpoints(epInfo *EndpointInfo) error { + + if _, err := net.InterfaceByName(client.hostVethName); err == nil { + log.Printf("Deleting old host veth %v", client.hostVethName) + if err = netlink.DeleteLink(client.hostVethName); err != nil { + log.Printf("[net] Failed to delete old hostveth %v: %v.", client.hostVethName, err) + return err + } + } + if err := epcommon.CreateEndpoint(client.hostVethName, client.containerVethName); err != nil { return err }