Skip to content

Commit dbf2164

Browse files
jiayoukunliangkr
and
liangkr
authored
Add the Pod's UID to the external_ids field of OVSDB (#311)
**What this PR does / why we need it**: By adding the UID of a Pod to the external_ids field of the corresponding veth interface in the OVSDB, it becomes convenient for the Pod object to query the OVSDB using the UID to find the vethName of the Pod on the host machine, and then deliver OVS flow tables accordingly. Currently, the host machine's veth interface in the OVSDB only stores the Netns field, which is useful, but for Pod objects, their netns is not publicly accessible. This change addresses this limitation by allowing Pod objects to query the OVSDB directly using their UID. **Special notes for your reviewer**: **Release note**: ```release-note Added the Pod UID to the external_ids field of the corresponding veth interface in the OVSDB. This change allows querying the vethName of a Pod on the host machine using its UID, improving the ability to deliver OVS flow tables for Pod objects. ``` Signed-off-by: jiayoukun <[email protected]> Co-authored-by: liangkr <[email protected]>
1 parent 641e634 commit dbf2164

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() {
@@ -185,8 +186,8 @@ func getBridgeName(driver *ovsdb.OvsDriver, bridgeName, ovnPort, deviceID string
185186
return "", fmt.Errorf("failed to get bridge name")
186187
}
187188

188-
func attachIfaceToBridge(ovsDriver *ovsdb.OvsBridgeDriver, hostIfaceName string, contIfaceName string, ofportRequest uint, vlanTag uint, trunks []uint, portType string, intfType string, contNetnsPath string, ovnPortName string) error {
189-
err := ovsDriver.CreatePort(hostIfaceName, contNetnsPath, contIfaceName, ovnPortName, ofportRequest, vlanTag, trunks, portType, intfType)
189+
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 {
190+
err := ovsDriver.CreatePort(hostIfaceName, contNetnsPath, contIfaceName, ovnPortName, ofportRequest, vlanTag, trunks, portType, intfType, contPodUid)
190191
if err != nil {
191192
return err
192193
}
@@ -264,9 +265,11 @@ func CmdAdd(args *skel.CmdArgs) error {
264265

265266
var mac string
266267
var ovnPort string
268+
var contPodUid string
267269
if envArgs != nil {
268270
mac = string(envArgs.MAC)
269271
ovnPort = string(envArgs.OvnPort)
272+
contPodUid = string(envArgs.K8S_POD_UID)
270273
}
271274

272275
netconf, err := config.LoadConf(args.StdinData)
@@ -356,7 +359,7 @@ func CmdAdd(args *skel.CmdArgs) error {
356359
}
357360
}
358361

359-
if err = attachIfaceToBridge(ovsBridgeDriver, hostIface.Name, contIface.Name, netconf.OfportRequest, vlanTagNum, trunks, portType, netconf.InterfaceType, args.Netns, ovnPort); err != nil {
362+
if err = attachIfaceToBridge(ovsBridgeDriver, hostIface.Name, contIface.Name, netconf.OfportRequest, vlanTagNum, trunks, portType, netconf.InterfaceType, args.Netns, ovnPort, contPodUid); err != nil {
360363
return err
361364
}
362365
defer func() {

0 commit comments

Comments
 (0)