Generate VethName based on podname and namespace in CNI (#143)
* Generate vethname based on podname and namespace
This commit is contained in:
Родитель
f0f090e525
Коммит
41ecaedb0d
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче