Skip to content

Commit

Permalink
Generate VethName based on podname and namespace in CNI (#143)
Browse files Browse the repository at this point in the history
* Generate vethname based on podname and namespace
  • Loading branch information
tamilmani1989 authored and sharmasushant committed May 17, 2018
1 parent f0f090e commit 41ecaed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
21 changes: 19 additions & 2 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package network

import (
"fmt"
"net"
"strings"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions network/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
29 changes: 24 additions & 5 deletions network/endpoint_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package network

import (
"crypto/sha1"
"encoding/hex"
"fmt"
"net"

Expand All @@ -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)

Expand Down

0 comments on commit 41ecaed

Please sign in to comment.