Skip to content

Commit

Permalink
lxd/network/acl: Allow 'ovnParseLogEntry' to read a log input using a…
Browse files Browse the repository at this point in the history
…n external timestamp

When reading a log file, the logtime (timestamp of the log entry) is part of the entry but when we read a syslog, the logtime is not contained
in the log entry fields and need to be parsed and converted.

We added two local functions to cope with these two usages:
* 'parseLogTimeFromFields'
* 'parseLogTimeFromTimestamp' (used when the OVN deployment is part of MicroOVN and we need to read the syslog)

Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Dec 3, 2024
1 parent cf64979 commit f345349
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 30 additions & 5 deletions lxd/network/acl/acl_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -1042,7 +1043,23 @@ type ovnLogEntry struct {
}

// ovnParseLogEntry takes a log line and expected ACL prefix and returns a re-formated log entry if matching.
func ovnParseLogEntry(input string, prefix string) string {
// The 'timestamp' string is in microseconds format. If empty, the timestamp is extracted from the log entry.
func ovnParseLogEntry(input string, timestamp string, prefix string) string {
parseLogTimeFromFields := func(fields []string) (time.Time, error) {
return time.Parse(time.RFC3339, fields[0])
}

parseLogTimeFromTimestamp := func(timestamp string) (time.Time, error) {
tsInt, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return time.Time{}, fmt.Errorf("Failed to parse timestamp: %w", err)
}

// The provided timestamp is in microseconds and need to be converted to nanoseconds.
tsNs := tsInt * 1000
return time.Unix(0, tsNs).UTC(), nil
}

fields := strings.Split(input, "|")

// Skip unknown formatting.
Expand Down Expand Up @@ -1071,10 +1088,18 @@ func ovnParseLogEntry(input string, prefix string) string {
return ""
}

// Parse the timestamp.
logTime, err := time.Parse(time.RFC3339, fields[0])
if err != nil {
return ""
var logTime time.Time
var err error
if timestamp == "" {
logTime, err = parseLogTimeFromFields(fields)
if err != nil {
return ""
}
} else {
logTime, err = parseLogTimeFromTimestamp(timestamp)
if err != nil {
return ""
}
}

// Get the protocol.
Expand Down
2 changes: 1 addition & 1 deletion lxd/network/acl/driver_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ func (d *common) GetLog(clientType request.ClientType) (string, error) {
logEntries := []string{}
scanner := bufio.NewScanner(logFile)
for scanner.Scan() {
logEntry := ovnParseLogEntry(scanner.Text(), fmt.Sprintf("lxd_acl%d-", d.id))
logEntry := ovnParseLogEntry(scanner.Text(), "", fmt.Sprintf("lxd_acl%d-", d.id))
if logEntry == "" {
continue
}
Expand Down

0 comments on commit f345349

Please sign in to comment.