Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions cmd/ovsdb-check/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"context"
"fmt"
"log"
"time"

"yunion.io/x/ovsdb/client"
"yunion.io/x/ovsdb/schema/ovn_nb"
)

func main() {
// 1. Connect to OVSDB (Adjust address as needed)
// Default OVN NB socket location
target := "unix:/var/run/ovn/ovnnb_db.sock"

fmt.Printf("Connecting to %s...\n", target)
cli, err := client.NewClient(target)
if err != nil {
log.Fatalf("Failed to connect: %v\n(Make sure OVN is running and the socket path is correct)", err)
}
defer cli.Close()
log.Println("Connected to OVSDB")

// 2. Setup Monitoring
// We use the generated OVNNorthbound struct as the target cache
dbCache := &ovn_nb.OVNNorthbound{}
ctx := context.Background()

// Start monitoring in a goroutine (it blocks waiting for updates)
go func() {
log.Println("Starting Monitor...")
if err := cli.MonitorDB(ctx, "OVN_Northbound", dbCache); err != nil {
log.Printf("Monitor failed: %v", err)
}
}()

// Wait for initial sync
fmt.Println("Waiting for cache sync...")
time.Sleep(1 * time.Second)
fmt.Printf("Cache Synced: %d Logical Switches found\n", len(dbCache.LogicalSwitch))

// 3. Perform a Transaction (Insert)
lsName := fmt.Sprintf("test-ls-%d", time.Now().Unix())
newLs := &ovn_nb.LogicalSwitch{
Name: lsName,
ExternalIds: map[string]string{
"created-by": "go-client",
},
}

log.Printf("Creating Logical Switch: %s", lsName)
op := client.OvsdbCreateOp(newLs, "newSwitch")

// Execute Transaction
results, err := cli.TransactOps(ctx, "OVN_Northbound", op)
if err != nil {
log.Fatalf("Transaction failed: %v", err)
}

// Check Result
if len(results) > 0 && results[0].Error == "" {
fmt.Printf("Transaction Success! UUID: %v\n", results[0].Uuid)
} else {
fmt.Printf("Transaction Response: %+v\n", results)
}

// 4. Verify Update in Cache
time.Sleep(200 * time.Millisecond) // Allow monitor update to arrive
found := false
for _, ls := range dbCache.LogicalSwitch {
if ls.Name == lsName {
fmt.Printf("Verified: Switch %s exists in local cache (UUID: %s)\n", lsName, ls.Uuid)
found = true

// Cleanup: Delete the test switch
log.Printf("Cleaning up (Deleting %s)...", lsName)
delOp := client.OvsdbDeleteOp("Logical_Switch", client.NewConditionUuid(ls.Uuid))
_, err := cli.TransactOps(ctx, "OVN_Northbound", delOp)
if err != nil {
log.Printf("Cleanup failed: %v", err)
} else {
log.Println("Cleanup successful.")
}
break
}
}

if !found {
log.Printf("Error: Created switch not found in cache yet.")
}
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,7 @@ yunion.io/x/log/hooks
# yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900
## explicit; go 1.14
yunion.io/x/ovsdb/cli_util
yunion.io/x/ovsdb/client
yunion.io/x/ovsdb/schema/ovn_nb
yunion.io/x/ovsdb/types
# yunion.io/x/pkg v1.10.4-0.20251114095758-2a2f105d9712
Expand Down
193 changes: 193 additions & 0 deletions vendor/yunion.io/x/ovsdb/client/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading