Skip to content

Commit 41ecaed

Browse files
tamilmani1989sharmasushant
authored andcommitted
Generate VethName based on podname and namespace in CNI (Azure#143)
* Generate vethname based on podname and namespace
1 parent f0f090e commit 41ecaed

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

cni/network/network.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package network
55

66
import (
7+
"fmt"
78
"net"
89
"strings"
910

@@ -162,10 +163,23 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
162163

163164
// Parse Pod arguments.
164165
podCfg, err := cni.ParseCniArgs(args.Args)
166+
if err != nil {
167+
log.Printf("Error while parsing CNI Args %v", err)
168+
return err
169+
}
170+
165171
k8sNamespace := string(podCfg.K8S_POD_NAMESPACE)
166172
if len(k8sNamespace) == 0 {
167-
err = plugin.Errorf("No k8s pod namespace provided.")
168-
return err
173+
errMsg := "Pod Namespace not specified in CNI Args"
174+
log.Printf(errMsg)
175+
return plugin.Errorf(errMsg)
176+
}
177+
178+
k8sPodName := string(podCfg.K8S_POD_NAME)
179+
if len(k8sPodName) == 0 {
180+
errMsg := "Pod Name not specified in CNI Args"
181+
log.Printf(errMsg)
182+
return plugin.Errorf(errMsg)
169183
}
170184

171185
// Parse network configuration from stdin.
@@ -334,6 +348,9 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
334348
epInfo.Routes = append(epInfo.Routes, network.RouteInfo{Dst: route.Dst, Gw: route.GW})
335349
}
336350

351+
epInfo.Data = make(map[string]interface{})
352+
epInfo.Data[network.OptVethName] = fmt.Sprintf("%s.%s", k8sNamespace, k8sPodName)
353+
337354
// Create the endpoint.
338355
log.Printf("[cni-net] Creating endpoint %v.", epInfo.Id)
339356
err = plugin.nm.CreateEndpoint(networkId, epInfo)

network/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ var (
1717
errEndpointNotFound = fmt.Errorf("Endpoint not found")
1818
errEndpointInUse = fmt.Errorf("Endpoint is already joined to a sandbox")
1919
errEndpointNotInUse = fmt.Errorf("Endpoint is not joined to a sandbox")
20+
21+
OptVethName = "vethname"
2022
)

network/endpoint_linux.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package network
77

88
import (
9+
"crypto/sha1"
10+
"encoding/hex"
911
"fmt"
1012
"net"
1113

@@ -19,28 +21,45 @@ const (
1921
commonInterfacePrefix = "az"
2022

2123
// Prefix for host virtual network interface names.
22-
hostVEthInterfacePrefix = commonInterfacePrefix + "veth"
24+
hostVEthInterfacePrefix = commonInterfacePrefix + "v"
2325

2426
// Prefix for container network interface names.
2527
containerInterfacePrefix = "eth"
2628
)
2729

30+
func generateVethName(key string) string {
31+
h := sha1.New()
32+
h.Write([]byte(key))
33+
return hex.EncodeToString(h.Sum(nil))[:11]
34+
}
35+
2836
// newEndpointImpl creates a new endpoint in the network.
2937
func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
3038
var containerIf *net.Interface
3139
var ns *Namespace
3240
var ep *endpoint
3341
var err error
42+
var hostIfName string
43+
var contIfName string
3444

3545
if nw.Endpoints[epInfo.Id] != nil {
36-
log.Printf("[net] Endpoint alreday exists.")
46+
log.Printf("[net] Endpoint alreday exists.")
3747
err = errEndpointExists
3848
return nil, err
3949
}
4050

41-
// Create a veth pair.
42-
hostIfName := fmt.Sprintf("%s%s", hostVEthInterfacePrefix, epInfo.Id[:7])
43-
contIfName := fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, epInfo.Id[:7])
51+
if _, ok := epInfo.Data[OptVethName]; ok {
52+
log.Printf("Generate veth name based on the key provided")
53+
key := epInfo.Data[OptVethName].(string)
54+
vethname := generateVethName(key)
55+
hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, vethname)
56+
contIfName = fmt.Sprintf("%s%s2", hostVEthInterfacePrefix, vethname)
57+
} else {
58+
// Create a veth pair.
59+
log.Printf("Generate veth name based on endpoint id")
60+
hostIfName = fmt.Sprintf("%s%s", hostVEthInterfacePrefix, epInfo.Id[:7])
61+
contIfName = fmt.Sprintf("%s%s-2", hostVEthInterfacePrefix, epInfo.Id[:7])
62+
}
4463

4564
log.Printf("[net] Creating veth pair %v %v.", hostIfName, contIfName)
4665

0 commit comments

Comments
 (0)