Skip to content

Commit

Permalink
Add the Pod's UID to the external_ids field of OVSDB (#311)
Browse files Browse the repository at this point in the history
**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]>
  • Loading branch information
jiayoukun and liangkr authored Dec 13, 2024
1 parent 641e634 commit dbf2164
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
15 changes: 8 additions & 7 deletions pkg/ovsdb/ovsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ func (ovsd *OvsDriver) ovsdbTransact(ops []ovsdb.Operation) ([]ovsdb.OperationRe
// **************** OVS driver API ********************

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

portUUID, portOp, err := createPortOperation(intfName, contNetnsPath, contIfaceName, vlanTag, trunks, portType, intfUUID)
portUUID, portOp, err := createPortOperation(intfName, contNetnsPath, contIfaceName, vlanTag, trunks, portType, intfUUID, contPodUid)
if err != nil {
return err
}
Expand Down Expand Up @@ -658,7 +658,7 @@ func (ovsd *OvsDriver) GetOvsPortForContIface(contIface, contNetnsPath string) (
return "", false, err
}

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

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

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

oMap, err := ovsdb.NewOvsMap(map[string]string{
"contNetns": contNetnsPath,
"contIface": contIfaceName,
"owner": ovsPortOwner,
"contPodUid": contPodUid,
"contNetns": contNetnsPath,
"contIface": contIfaceName,
"owner": ovsPortOwner,
})
if err != nil {
return ovsdb.UUID{}, nil, err
Expand Down
13 changes: 8 additions & 5 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ import (
// EnvArgs args containing common, desired mac and ovs port name
type EnvArgs struct {
cnitypes.CommonArgs
MAC cnitypes.UnmarshallableString `json:"mac,omitempty"`
OvnPort cnitypes.UnmarshallableString `json:"ovnPort,omitempty"`
MAC cnitypes.UnmarshallableString `json:"mac,omitempty"`
OvnPort cnitypes.UnmarshallableString `json:"ovnPort,omitempty"`
K8S_POD_UID cnitypes.UnmarshallableString
}

func init() {
Expand Down Expand Up @@ -185,8 +186,8 @@ func getBridgeName(driver *ovsdb.OvsDriver, bridgeName, ovnPort, deviceID string
return "", fmt.Errorf("failed to get bridge name")
}

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

var mac string
var ovnPort string
var contPodUid string
if envArgs != nil {
mac = string(envArgs.MAC)
ovnPort = string(envArgs.OvnPort)
contPodUid = string(envArgs.K8S_POD_UID)
}

netconf, err := config.LoadConf(args.StdinData)
Expand Down Expand Up @@ -356,7 +359,7 @@ func CmdAdd(args *skel.CmdArgs) error {
}
}

if err = attachIfaceToBridge(ovsBridgeDriver, hostIface.Name, contIface.Name, netconf.OfportRequest, vlanTagNum, trunks, portType, netconf.InterfaceType, args.Netns, ovnPort); err != nil {
if err = attachIfaceToBridge(ovsBridgeDriver, hostIface.Name, contIface.Name, netconf.OfportRequest, vlanTagNum, trunks, portType, netconf.InterfaceType, args.Netns, ovnPort, contPodUid); err != nil {
return err
}
defer func() {
Expand Down

0 comments on commit dbf2164

Please sign in to comment.