vpcagent: ovn: use yunion.io/x/ovsdb

This commit is contained in:
Yousong Zhou
2020-05-13 14:14:48 +08:00
parent 7c3d9d7f89
commit cae9594da9
3 changed files with 58 additions and 50 deletions
+9 -6
View File
@@ -1,15 +1,18 @@
package ovn
import (
"yunion.io/x/ovsdb/schema/ovn_nb"
"yunion.io/x/ovsdb/types"
"yunion.io/x/onecloud/pkg/vpcagent/ovnutil"
)
// cmp scans the database for irows. For those present, mark them with ocver.
// If all rows are found, return true to indicate this. Otherwise return as
// 2nd value the args to destroy these found records
func cmp(db *ovnutil.OVNNorthbound, ocver string, irows ...ovnutil.IRow) (bool, []string) {
irowsFound := make([]ovnutil.IRow, 0, len(irows))
irowsDiff := make([]ovnutil.IRow, 0)
func cmp(db *ovn_nb.OVNNorthbound, ocver string, irows ...types.IRow) (bool, []string) {
irowsFound := make([]types.IRow, 0, len(irows))
irowsDiff := make([]types.IRow, 0)
for _, irow := range irows {
irowFound := db.FindOneMatchNonZeros(irow)
@@ -18,8 +21,8 @@ func cmp(db *ovnutil.OVNNorthbound, ocver string, irows ...ovnutil.IRow) (bool,
} else {
// TODO db.FindByIndex()
switch row := irow.(type) {
case *ovnutil.LogicalSwitchPort:
rowQ := &ovnutil.LogicalSwitchPort{
case *ovn_nb.LogicalSwitchPort:
rowQ := &ovn_nb.LogicalSwitchPort{
Name: row.Name,
}
if irow := db.FindOneMatchNonZeros(rowQ); irow != nil {
@@ -31,7 +34,7 @@ func cmp(db *ovnutil.OVNNorthbound, ocver string, irows ...ovnutil.IRow) (bool,
// mark them anyway even if not all found, to avoid the destroy
// call at sweep stage
for _, irowFound := range irowsFound {
irowFound.OvnSetExternalIds(externalKeyOcVersion, ocver)
irowFound.SetExternalId(externalKeyOcVersion, ocver)
}
if len(irowsFound) == len(irows) {
return true, nil
+36 -33
View File
@@ -19,6 +19,9 @@ import (
"fmt"
"strings"
"yunion.io/x/ovsdb/cli_util"
"yunion.io/x/ovsdb/schema/ovn_nb"
"yunion.io/x/ovsdb/types"
"yunion.io/x/pkg/errors"
apis "yunion.io/x/onecloud/pkg/apis/compute"
@@ -33,13 +36,13 @@ const (
)
type OVNNorthboundKeeper struct {
DB ovnutil.OVNNorthbound
DB ovn_nb.OVNNorthbound
cli *ovnutil.OvnNbCtl
}
func DumpOVNNorthbound(ctx context.Context, cli *ovnutil.OvnNbCtl) (*OVNNorthboundKeeper, error) {
db := ovnutil.OVNNorthbound{}
itbls := []ovnutil.ITable{
db := ovn_nb.OVNNorthbound{}
itbls := []types.ITable{
&db.LogicalSwitch,
&db.LogicalSwitchPort,
&db.LogicalRouter,
@@ -49,12 +52,12 @@ func DumpOVNNorthbound(ctx context.Context, cli *ovnutil.OvnNbCtl) (*OVNNorthbou
}
args := []string{"--format=json", "list", "<tbl>"}
for _, itbl := range itbls {
tbl := itbl.OvnTableName()
tbl := itbl.OvsdbTableName()
args[2] = tbl
res := cli.Must(ctx, "List "+tbl, args)
if err := ovnutil.UnmarshalJSON([]byte(res.Output), itbl); err != nil {
if err := cli_util.UnmarshalJSON([]byte(res.Output), itbl); err != nil {
return nil, errors.Wrapf(err, "Unmarshal %s:\n%s",
itbl.OvnTableName(), res.Output)
itbl.OvsdbTableName(), res.Output)
}
}
keeper := &OVNNorthboundKeeper{
@@ -64,10 +67,10 @@ func DumpOVNNorthbound(ctx context.Context, cli *ovnutil.OvnNbCtl) (*OVNNorthbou
return keeper, nil
}
func ovnCreateArgs(irow ovnutil.IRow, idRef string) []string {
func ovnCreateArgs(irow types.IRow, idRef string) []string {
args := append([]string{
"--", "--id=@" + idRef, "create", irow.OvnTableName(),
}, irow.OvnArgs()...)
"--", "--id=@" + idRef, "create", irow.OvsdbTableName(),
}, irow.OvsdbCmdArgs()...)
return args
}
@@ -77,18 +80,18 @@ func (keeper *OVNNorthboundKeeper) ClaimVpc(ctx context.Context, vpc *agentmodel
ocVersion = fmt.Sprintf("%s.%d", vpc.UpdatedAt, vpc.UpdateVersion)
)
vpcLr := &ovnutil.LogicalRouter{
vpcLr := &ovn_nb.LogicalRouter{
Name: vpcLrName(vpc.Id),
}
vpcHostLs := &ovnutil.LogicalSwitch{
vpcHostLs := &ovn_nb.LogicalSwitch{
Name: vpcHostLsName(vpc.Id),
}
vpcRhp := &ovnutil.LogicalRouterPort{
vpcRhp := &ovn_nb.LogicalRouterPort{
Name: vpcRhpName(vpc.Id),
Mac: apis.VpcMappedGatewayMac,
Networks: []string{fmt.Sprintf("%s/%d", apis.VpcMappedGatewayIP(), apis.VpcMappedIPMask)},
}
vpcHrp := &ovnutil.LogicalSwitchPort{
vpcHrp := &ovn_nb.LogicalSwitchPort{
Name: vpcHrpName(vpc.Id),
Type: "router",
Addresses: []string{"router"},
@@ -121,15 +124,15 @@ func (keeper *OVNNorthboundKeeper) ClaimNetwork(ctx context.Context, network *ag
mdMac = mac.HashMac(network.Id, "md")
mdIp = "169.254.169.254"
)
netLs := &ovnutil.LogicalSwitch{
netLs := &ovn_nb.LogicalSwitch{
Name: netLsName(network.Id),
}
netRnp := &ovnutil.LogicalRouterPort{
netRnp := &ovn_nb.LogicalRouterPort{
Name: netRnpName(network.Id),
Mac: rpMac,
Networks: []string{fmt.Sprintf("%s/%d", network.GuestGateway, network.GuestIpMask)},
}
netNrp := &ovnutil.LogicalSwitchPort{
netNrp := &ovn_nb.LogicalSwitchPort{
Name: netNrpName(network.Id),
Type: "router",
Addresses: []string{"router"},
@@ -137,7 +140,7 @@ func (keeper *OVNNorthboundKeeper) ClaimNetwork(ctx context.Context, network *ag
"router-port": netRnpName(network.Id),
},
}
netMdp := &ovnutil.LogicalSwitchPort{
netMdp := &ovn_nb.LogicalSwitchPort{
Name: netMdpName(network.Id),
Type: "localport",
Addresses: []string{fmt.Sprintf("%s %s", mdMac, mdIp)},
@@ -146,7 +149,7 @@ func (keeper *OVNNorthboundKeeper) ClaimNetwork(ctx context.Context, network *ag
mdIp, "0.0.0.0",
"0.0.0.0/0", network.GuestGateway,
}
dhcpopts := &ovnutil.DHCPOptions{
dhcpopts := &ovn_nb.DHCPOptions{
Cidr: fmt.Sprintf("%s/%d", network.GuestIpStart, network.GuestIpMask),
Options: map[string]string{
"server_id": network.GuestGateway,
@@ -193,16 +196,16 @@ func (keeper *OVNNorthboundKeeper) ClaimVpcHost(ctx context.Context, vpc *agentm
var (
ocVersion = fmt.Sprintf("%s.%d", host.UpdatedAt, host.UpdateVersion)
)
vpcHostLsp := &ovnutil.LogicalSwitchPort{
vpcHostLsp := &ovn_nb.LogicalSwitchPort{
Name: vpcHostLspName(vpc.Id, host.Id),
Addresses: []string{fmt.Sprintf("%s %s", mac.HashMac(host.Id), host.OvnMappedIpAddr)},
}
if m := keeper.DB.LogicalSwitchPort.FindOneMatchNonZeros(vpcHostLsp); m != nil {
m.OvnSetExternalIds(externalKeyOcVersion, ocVersion)
m.SetExternalId(externalKeyOcVersion, ocVersion)
return nil
} else {
args := []string{
"--bare", "--columns=_uuid", "find", vpcHostLsp.OvnTableName(),
"--bare", "--columns=_uuid", "find", vpcHostLsp.OvsdbTableName(),
fmt.Sprintf("name=%q", vpcHostLsp.Name),
}
res := keeper.cli.Must(ctx, "find vpcHostLsp", args)
@@ -230,13 +233,13 @@ func (keeper *OVNNorthboundKeeper) ClaimGuestnetwork(ctx context.Context, guestn
)
{
dhcpOptQuery := &ovnutil.DHCPOptions{
dhcpOptQuery := &ovn_nb.DHCPOptions{
ExternalIds: map[string]string{
externalKeyOcRef: guestnetwork.NetworkId,
},
}
if m := keeper.DB.DHCPOptions.FindOneMatchNonZeros(dhcpOptQuery); m != nil {
dhcpOpt = m.OvnUuid()
dhcpOpt = m.OvsdbUuid()
} else {
args := []string{
"--bare", "--columns=_uuid", "find", "DHCP_Options",
@@ -250,7 +253,7 @@ func (keeper *OVNNorthboundKeeper) ClaimGuestnetwork(ctx context.Context, guestn
}
}
gnp := &ovnutil.LogicalSwitchPort{
gnp := &ovn_nb.LogicalSwitchPort{
Name: gnpName(guestnetwork.NetworkId, guestnetwork.Ifname),
Addresses: []string{fmt.Sprintf("%s %s", guestnetwork.MacAddr, guestnetwork.IpAddr)},
PortSecurity: []string{fmt.Sprintf("%s %s/%d", guestnetwork.MacAddr, guestnetwork.IpAddr, guestnetwork.Network.GuestIpMask)},
@@ -258,7 +261,7 @@ func (keeper *OVNNorthboundKeeper) ClaimGuestnetwork(ctx context.Context, guestn
}
gnrPolicy := "src-ip"
gnr := &ovnutil.LogicalRouterStaticRoute{
gnr := &ovn_nb.LogicalRouterStaticRoute{
Policy: &gnrPolicy,
IpPrefix: guestnetwork.IpAddr + "/32",
Nexthop: host.OvnMappedIpAddr,
@@ -283,7 +286,7 @@ func (keeper *OVNNorthboundKeeper) ClaimGuestnetwork(ctx context.Context, guestn
func (keeper *OVNNorthboundKeeper) Mark(ctx context.Context) {
db := &keeper.DB
itbls := []ovnutil.ITable{
itbls := []types.ITable{
&db.LogicalSwitch,
&db.LogicalSwitchPort,
&db.LogicalRouter,
@@ -293,7 +296,7 @@ func (keeper *OVNNorthboundKeeper) Mark(ctx context.Context) {
}
for _, itbl := range itbls {
for _, irow := range itbl.Rows() {
irow.OvnRemoveExternalIds(externalKeyOcVersion)
irow.RemoveExternalId(externalKeyOcVersion)
}
}
}
@@ -301,17 +304,17 @@ func (keeper *OVNNorthboundKeeper) Mark(ctx context.Context) {
func (keeper *OVNNorthboundKeeper) Sweep(ctx context.Context) error {
db := &keeper.DB
// isRoot=false tables at the end
itbls := []ovnutil.ITable{
itbls := []types.ITable{
&db.LogicalSwitchPort,
&db.LogicalRouterPort,
&db.LogicalSwitch,
&db.LogicalRouter,
&db.DHCPOptions,
}
var irows []ovnutil.IRow
var irows []types.IRow
for _, itbl := range itbls {
for _, irow := range itbl.Rows() {
_, ok := irow.OvnGetExternalIds(externalKeyOcVersion)
_, ok := irow.GetExternalId(externalKeyOcVersion)
if !ok {
irows = append(irows, irow)
}
@@ -325,14 +328,14 @@ func (keeper *OVNNorthboundKeeper) Sweep(ctx context.Context) error {
{
var args []string
for _, irow := range db.LogicalRouterStaticRoute.Rows() {
_, ok := irow.OvnGetExternalIds(externalKeyOcVersion)
_, ok := irow.GetExternalId(externalKeyOcVersion)
if !ok {
ref, ok := irow.OvnGetExternalIds(externalKeyOcRef)
ref, ok := irow.GetExternalId(externalKeyOcRef)
if ok {
parts := strings.SplitN(ref, "/", 4)
if len(parts) == 4 {
vpcId := parts[1]
args = append(args, "--", "remove", "Logical_Router", vpcLrName(vpcId), "static_routes", irow.OvnUuid())
args = append(args, "--", "remove", "Logical_Router", vpcLrName(vpcId), "static_routes", irow.OvsdbUuid())
}
}
}
+13 -11
View File
@@ -23,6 +23,8 @@ import (
"time"
"yunion.io/x/log"
"yunion.io/x/ovsdb/schema/ovn_nb"
"yunion.io/x/ovsdb/types"
"yunion.io/x/pkg/errors"
)
@@ -138,12 +140,12 @@ func ovnNbctlArgsString(args []string) string {
return s
}
func OvnNbctlArgsDestroy(irows []IRow) []string {
func OvnNbctlArgsDestroy(irows []types.IRow) []string {
sort.Slice(irows, func(i, j int) bool {
ri := irows[i]
rj := irows[j]
iri := ri.OvnIsRoot()
irj := rj.OvnIsRoot()
iri := ri.OvsdbIsRoot()
irj := rj.OvsdbIsRoot()
if !iri && irj {
return true
}
@@ -152,17 +154,17 @@ func OvnNbctlArgsDestroy(irows []IRow) []string {
var args []string
for _, irow := range irows {
switch irow.(type) {
case *LogicalSwitchPort:
args = append(args, "--", "--if-exists", "lsp-del", irow.OvnUuid())
case *LogicalRouterPort:
args = append(args, "--", "--if-exists", "lrp-del", irow.OvnUuid())
case *LogicalRouterStaticRoute:
case *ovn_nb.LogicalSwitchPort:
args = append(args, "--", "--if-exists", "lsp-del", irow.OvsdbUuid())
case *ovn_nb.LogicalRouterPort:
args = append(args, "--", "--if-exists", "lrp-del", irow.OvsdbUuid())
case *ovn_nb.LogicalRouterStaticRoute:
// find out vpc id/logical router from external_ids
default:
if !irow.OvnIsRoot() {
panic(irow.OvnTableName())
if !irow.OvsdbIsRoot() {
panic(irow.OvsdbTableName())
}
args = append(args, "--", "--if-exists", "destroy", irow.OvnTableName(), irow.OvnUuid())
args = append(args, "--", "--if-exists", "destroy", irow.OvsdbTableName(), irow.OvsdbUuid())
}
}
return args