Skip to content

Commit 31e2766

Browse files
authored
Merge pull request #2 from lucadelmonte/ovn-chassis-status
Add version command support and enhance OvnChassis structure with NbCfg and NbCfgTimestamp fields
2 parents 292625c + 87e68f9 commit 31e2766

3 files changed

Lines changed: 110 additions & 7 deletions

File tree

encoder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var methods = map[string]method{
3333
"get_schema": {Name: "get_schema"},
3434
"transact": {Name: "transact"},
3535
"list-commands": {Name: "list-commands"},
36+
"version": {Name: "version"},
3637
"coverage/show": {Name: "coverage/show"},
3738
"memory/show": {Name: "memory/show"},
3839
"cluster/status": {Name: "cluster/status"},
@@ -111,6 +112,7 @@ func (enc *ovsdbEncoder) Encode(v interface{}) error {
111112
e.WriteString(s)
112113
// e.WriteString("\"Open_vSwitch\",{\"op\":\"select\",\"table\":\"Open_vSwitch\",\"where\":[]}")
113114
case "list-commands":
115+
case "version":
114116
case "coverage/show":
115117
case "memory/show":
116118
case "dpif/show":

ovn_chassis.go

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ type OvnChassis struct {
2828
UUID string
2929
Proto string
3030
}
31-
Up int
32-
Ports []string
33-
Switches []string
31+
NbCfg int64 // Configuration sequence number from Chassis_Private table (0 if not present)
32+
NbCfgTimestamp int64 // Timestamp from Chassis_Private table (0 if not present)
33+
Ports []string
34+
Switches []string
3435
}
3536

3637
// GetChassis returns a list of OVN chassis.
@@ -134,6 +135,92 @@ func (cli *OvnClient) GetChassis() ([]*OvnChassis, error) {
134135
break
135136
}
136137
}
138+
139+
query = "SELECT chassis, name, nb_cfg, nb_cfg_timestamp FROM Chassis_Private"
140+
result, err = cli.Database.Southbound.Client.Transact(cli.Database.Southbound.Name, query)
141+
if err != nil {
142+
return chassis, nil
143+
}
144+
145+
// Create maps for chassis nb_cfg and nb_cfg_timestamp
146+
chassisNbCfgMap := make(map[string]int64)
147+
chassisTimestampMap := make(map[string]int64)
148+
if len(result.Rows) > 0 {
149+
for _, row := range result.Rows {
150+
var chassisUUID string
151+
var chassisName string
152+
var nbCfg int64
153+
var nbCfgTimestamp int64
154+
155+
// Get chassis UUID (reference to Chassis table)
156+
if r, dt, err := row.GetColumnValue("chassis", result.Columns); err == nil && dt == "string" {
157+
chassisUUID = r.(string)
158+
}
159+
160+
// Get chassis name
161+
if r, dt, err := row.GetColumnValue("name", result.Columns); err == nil && dt == "string" {
162+
chassisName = r.(string)
163+
}
164+
165+
// Get the nb_cfg
166+
if r, dt, err := row.GetColumnValue("nb_cfg", result.Columns); err == nil {
167+
switch dt {
168+
case "int64":
169+
nbCfg = r.(int64)
170+
case "integer":
171+
// GetColumnValue returns "integer" for float64 values after converting to int64
172+
nbCfg = r.(int64)
173+
case "float64":
174+
nbCfg = int64(r.(float64))
175+
case "int":
176+
nbCfg = int64(r.(int))
177+
}
178+
}
179+
180+
// Get the nb_cfg_timestamp
181+
if r, dt, err := row.GetColumnValue("nb_cfg_timestamp", result.Columns); err == nil {
182+
switch dt {
183+
case "int64":
184+
nbCfgTimestamp = r.(int64)
185+
case "integer":
186+
// GetColumnValue returns "integer" for float64 values after converting to int64
187+
nbCfgTimestamp = r.(int64)
188+
case "float64":
189+
nbCfgTimestamp = int64(r.(float64))
190+
case "int":
191+
nbCfgTimestamp = int64(r.(int))
192+
}
193+
}
194+
195+
// Store values by both UUID and name
196+
if chassisUUID != "" {
197+
chassisNbCfgMap[chassisUUID] = nbCfg
198+
chassisTimestampMap[chassisUUID] = nbCfgTimestamp
199+
}
200+
if chassisName != "" {
201+
chassisNbCfgMap[chassisName] = nbCfg
202+
chassisTimestampMap[chassisName] = nbCfgTimestamp
203+
}
204+
}
205+
}
206+
207+
// Set the NbCfg and NbCfgTimestamp fields for each chassis
208+
// Will be 0 if chassis has no entry in Chassis_Private
209+
for _, c := range chassis {
210+
if nbCfg, exists := chassisNbCfgMap[c.UUID]; exists {
211+
c.NbCfg = nbCfg
212+
} else if nbCfg, exists := chassisNbCfgMap[c.Name]; exists {
213+
c.NbCfg = nbCfg
214+
}
215+
216+
if timestamp, exists := chassisTimestampMap[c.UUID]; exists {
217+
c.NbCfgTimestamp = timestamp
218+
} else if timestamp, exists := chassisTimestampMap[c.Name]; exists {
219+
c.NbCfgTimestamp = timestamp
220+
}
221+
// If no entry found, NbCfg and NbCfgTimestamp remain 0 (default)
222+
}
223+
137224
return chassis, nil
138225
}
139226

system.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,13 @@ func getSystemID(client *Client, dbName string, filepath string) (string, error)
124124
}
125125

126126
func getVersionViaAppctl(sock string, timeout int) (string, error) {
127-
var app Client
128-
var err error
129127
cmd := "version"
130-
app, err = NewClient(sock, timeout)
128+
app, err := NewClient(sock, timeout)
131129
if err != nil {
132130
return "", fmt.Errorf("failed to connect to socket %s: %s", sock, err)
133131
}
134-
defer app.Close()
135132
r, err := app.query(cmd, nil)
133+
app.Close()
136134
if err != nil {
137135
return "", fmt.Errorf("the '%s' command failed: %s", cmd, err)
138136
}
@@ -296,6 +294,14 @@ func (cli *OvnClient) GetSystemInfo() error {
296294
}
297295
// Get schema for db_version
298296
schema, _ := cli.Database.Vswitch.Client.GetSchema(cli.Database.Vswitch.Name)
297+
// Ensure PID is read and socket path is updated before using control socket
298+
if cli.Database.Vswitch.Process.ID == 0 {
299+
p, pidErr := getProcessInfoFromFile(cli.Database.Vswitch.File.Pid.Path)
300+
if pidErr == nil {
301+
cli.Database.Vswitch.Process = p
302+
}
303+
}
304+
cli.updateRefs()
299305
// Query version information via ovs-appctl for fields not in DB (OVS 3.x+)
300306
populateVersionFromAppctl(systemInfo, cli.Database.Vswitch.Socket.Control, cli.Timeout, &schema)
301307
cli.System.ID = systemInfo["system-id"]
@@ -331,6 +337,14 @@ func (cli *OvsClient) GetSystemInfo() error {
331337
}
332338
// Get schema for db_version
333339
schema, _ := cli.Database.Vswitch.Client.GetSchema(cli.Database.Vswitch.Name)
340+
// Ensure PID is read and socket path is updated before using control socket
341+
if cli.Database.Vswitch.Process.ID == 0 {
342+
p, pidErr := getProcessInfoFromFile(cli.Database.Vswitch.File.Pid.Path)
343+
if pidErr == nil {
344+
cli.Database.Vswitch.Process = p
345+
}
346+
}
347+
cli.updateRefs()
334348
// Query version information via ovs-appctl for fields not in DB (OVS 3.x+)
335349
populateVersionFromAppctl(systemInfo, cli.Database.Vswitch.Socket.Control, cli.Timeout, &schema)
336350
cli.System.ID = systemInfo["system-id"]

0 commit comments

Comments
 (0)