From 41ecaedb0df0e4a66237facea86fcaf3f440a4c6 Mon Sep 17 00:00:00 2001 From: tamilmani1989 Date: Wed, 16 May 2018 17:02:09 -0700 Subject: [PATCH] Generate VethName based on podname and namespace in CNI (#143) * Generate vethname based on podname and namespace --- cni/network/network.go | 21 +++++++++++++++++++-- network/api.go | 2 ++ network/endpoint_linux.go | 29 ++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/cni/network/network.go b/cni/network/network.go index 7b1b570973..185e2d4def 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -4,6 +4,7 @@ package network import ( + "fmt" "net" "strings" @@ -162,10 +163,23 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { // Parse Pod arguments. podCfg, err := cni.ParseCniArgs(args.Args) + if err != nil { + log.Printf("Error while parsing CNI Args %v", err) + return err + } + k8sNamespace := string(podCfg.K8S_POD_NAMESPACE) if len(k8sNamespace) == 0 { - err = plugin.Errorf("No k8s pod namespace provided.") - return err + errMsg := "Pod Namespace not specified in CNI Args" + log.Printf(errMsg) + return plugin.Errorf(errMsg) + } + + k8sPodName := string(podCfg.K8S_POD_NAME) + if len(k8sPodName) == 0 { + errMsg := "Pod Name not specified in CNI Args" + log.Printf(errMsg) + return plugin.Errorf(errMsg) } // Parse network configuration from stdin. @@ -334,6 +348,9 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error { epInfo.Routes = append(epInfo.Routes, network.RouteInfo{Dst: route.Dst, Gw: route.GW}) } + epInfo.Data = make(map[string]interface{}) + epInfo.Data[network.OptVethName] = fmt.Sprintf("%s.%s", k8sNamespace, k8sPodName) + // Create the endpoint. log.Printf("[cni-net] Creating endpoint %v.", epInfo.Id) err = plugin.nm.CreateEndpoint(networkId, epInfo) diff --git a/network/api.go b/network/api.go index 4b6b93ba81..9ce77c4dde 100644 --- a/network/api.go +++ b/network/api.go @@ -17,4 +17,6 @@ var ( errEndpointNotFound = fmt.Errorf("Endpoint not found") errEndpointInUse = fmt.Errorf("Endpoint is already joined to a sandbox") errEndpointNotInUse = fmt.Errorf("Endpoint is not joined to a sandbox") + + OptVethName = "vethname" ) diff --git a/network/endpoint_linux.go b/network/endpoint_linux.go index 0019c370cd..a33c5741cf 100644 --- a/network/endpoint_linux.go +++ b/network/endpoint_linux.go @@ -6,6 +6,8 @@ package network import ( + "crypto/sha1" + "encoding/hex" "fmt" "net" @@ -19,28 +21,45 @@ const ( commonInterfacePrefix = "az" // Prefix for host virtual network interface names. - hostVEthInterfacePrefix = commonInterfacePrefix + "veth" + hostVEthInterfacePrefix = commonInterfacePrefix + "v" // Prefix for container network interface names. containerInterfacePrefix = "eth" ) +func generateVethName(key string) string { + h := sha1.New() + h.Write([]byte(key)) + return hex.EncodeToString(h.Sum(nil))[:11] +} + // newEndpointImpl creates a new endpoint in the network. func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) { var containerIf *net.Interface var ns *Namespace var ep *endpoint var err error + var hostIfName string + var contIfName string if nw.Endpoints[epInfo.Id] != nil { - log.Printf("[net] Endpoint alreday exists.") + 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]) + if _, ok := epInfo.Data[OptVethName]; ok { + log.Printf("Generate veth name based on the key provided") + key := epInfo.Data[OptVethName].(string) + vethname := generateVethName(key) + hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, vethname) + contIfName = fmt.Sprintf("%s%s2", hostVEthInterfacePrefix, vethname) + } else { + // Create a veth pair. + log.Printf("Generate veth name based on endpoint id") + hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, epInfo.Id[:7]) + contIfName = fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, epInfo.Id[:7]) + } log.Printf("[net] Creating veth pair %v %v.", hostIfName, contIfName)