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
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a const file to add this to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are following the same thing in ipam/api.go

)
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