Skip to content

Commit dcdf98f

Browse files
liangkrjiayoukun
liangkr
authored andcommitted
Add the Pod's UID to the external_ids field of OVSDB
By adding the Pod's UID to the external_ids field of the corresponding veth interface in OVSDB, the Pod object can easily query OVSDB through the UID, find the vethName of the Pod on the host, and then issue OVS flow tables and other operations based on the vethName. Signed-off-by: jiayoukun <[email protected]>
1 parent 6f8174b commit dcdf98f

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pkg/ovsdb/ovsdb.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ func (ovsd *OvsDriver) ovsdbTransact(ops []ovsdb.Operation) ([]ovsdb.OperationRe
158158
// **************** OVS driver API ********************
159159

160160
// CreatePort Create an internal port in OVS
161-
func (ovsd *OvsBridgeDriver) CreatePort(intfName, contNetnsPath, contIfaceName, ovnPortName string, ofportRequest uint, vlanTag uint, trunks []uint, portType string, intfType string) error {
161+
func (ovsd *OvsBridgeDriver) CreatePort(intfName, contNetnsPath, contIfaceName, ovnPortName string, ofportRequest uint, vlanTag uint, trunks []uint, portType string, intfType string, contPodUid string) error {
162162
intfUUID, intfOp, err := createInterfaceOperation(intfName, ofportRequest, ovnPortName, intfType)
163163
if err != nil {
164164
return err
165165
}
166166

167-
portUUID, portOp, err := createPortOperation(intfName, contNetnsPath, contIfaceName, vlanTag, trunks, portType, intfUUID)
167+
portUUID, portOp, err := createPortOperation(intfName, contNetnsPath, contIfaceName, vlanTag, trunks, portType, intfUUID, contPodUid)
168168
if err != nil {
169169
return err
170170
}
@@ -658,7 +658,7 @@ func (ovsd *OvsDriver) GetOvsPortForContIface(contIface, contNetnsPath string) (
658658
return "", false, err
659659
}
660660

661-
condition := ovsdb.NewCondition("external_ids", ovsdb.ConditionEqual, ovsmap)
661+
condition := ovsdb.NewCondition("external_ids", ovsdb.ConditionIncludes, ovsmap)
662662
colums := []string{"name", "external_ids"}
663663
port, err := ovsd.findByCondition("Port", condition, colums)
664664
if err != nil {
@@ -853,7 +853,7 @@ func createInterfaceOperation(intfName string, ofportRequest uint, ovnPortName s
853853
return intfUUID, &intfOp, nil
854854
}
855855

856-
func createPortOperation(intfName, contNetnsPath, contIfaceName string, vlanTag uint, trunks []uint, portType string, intfUUID ovsdb.UUID) (ovsdb.UUID, *ovsdb.Operation, error) {
856+
func createPortOperation(intfName, contNetnsPath, contIfaceName string, vlanTag uint, trunks []uint, portType string, intfUUID ovsdb.UUID, contPodUid string) (ovsdb.UUID, *ovsdb.Operation, error) {
857857
portUUIDStr := intfName
858858
portUUID := ovsdb.UUID{GoUUID: portUUIDStr}
859859

@@ -877,9 +877,10 @@ func createPortOperation(intfName, contNetnsPath, contIfaceName string, vlanTag
877877
}
878878

879879
oMap, err := ovsdb.NewOvsMap(map[string]string{
880-
"contNetns": contNetnsPath,
881-
"contIface": contIfaceName,
882-
"owner": ovsPortOwner,
880+
"contPodUid": contPodUid,
881+
"contNetns": contNetnsPath,
882+
"contIface": contIfaceName,
883+
"owner": ovsPortOwner,
883884
})
884885
if err != nil {
885886
return ovsdb.UUID{}, nil, err

pkg/plugin/plugin.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ import (
5050
// EnvArgs args containing common, desired mac and ovs port name
5151
type EnvArgs struct {
5252
cnitypes.CommonArgs
53-
MAC cnitypes.UnmarshallableString `json:"mac,omitempty"`
54-
OvnPort cnitypes.UnmarshallableString `json:"ovnPort,omitempty"`
53+
MAC cnitypes.UnmarshallableString `json:"mac,omitempty"`
54+
OvnPort cnitypes.UnmarshallableString `json:"ovnPort,omitempty"`
55+
K8S_POD_UID cnitypes.UnmarshallableString
5556
}
5657

5758
func init() {
@@ -168,8 +169,8 @@ func getBridgeName(driver *ovsdb.OvsDriver, bridgeName, ovnPort, deviceID string
168169
return "", fmt.Errorf("failed to get bridge name")
169170
}
170171

171-
func attachIfaceToBridge(ovsDriver *ovsdb.OvsBridgeDriver, hostIfaceName string, contIfaceName string, ofportRequest uint, vlanTag uint, trunks []uint, portType string, intfType string, contNetnsPath string, ovnPortName string) error {
172-
err := ovsDriver.CreatePort(hostIfaceName, contNetnsPath, contIfaceName, ovnPortName, ofportRequest, vlanTag, trunks, portType, intfType)
172+
func attachIfaceToBridge(ovsDriver *ovsdb.OvsBridgeDriver, hostIfaceName string, contIfaceName string, ofportRequest uint, vlanTag uint, trunks []uint, portType string, intfType string, contNetnsPath string, ovnPortName string, contPodUid string) error {
173+
err := ovsDriver.CreatePort(hostIfaceName, contNetnsPath, contIfaceName, ovnPortName, ofportRequest, vlanTag, trunks, portType, intfType, contPodUid)
173174
if err != nil {
174175
return err
175176
}
@@ -247,9 +248,11 @@ func CmdAdd(args *skel.CmdArgs) error {
247248

248249
var mac string
249250
var ovnPort string
251+
var contPodUid string
250252
if envArgs != nil {
251253
mac = string(envArgs.MAC)
252254
ovnPort = string(envArgs.OvnPort)
255+
contPodUid = string(envArgs.K8S_POD_UID)
253256
}
254257

255258
netconf, err := config.LoadConf(args.StdinData)
@@ -329,7 +332,7 @@ func CmdAdd(args *skel.CmdArgs) error {
329332
}
330333
}
331334

332-
if err = attachIfaceToBridge(ovsBridgeDriver, hostIface.Name, contIface.Name, netconf.OfportRequest, vlanTagNum, trunks, portType, netconf.InterfaceType, args.Netns, ovnPort); err != nil {
335+
if err = attachIfaceToBridge(ovsBridgeDriver, hostIface.Name, contIface.Name, netconf.OfportRequest, vlanTagNum, trunks, portType, netconf.InterfaceType, args.Netns, ovnPort, contPodUid); err != nil {
333336
return err
334337
}
335338
defer func() {

0 commit comments

Comments
 (0)