Skip to content

Commit

Permalink
lxd/network/acl/ovn: Add a new 'parseLogTime' function
Browse files Browse the repository at this point in the history
This is used to parse a timestamp that is either contained in a log file 'fields' (parsed log entry)
or just passed as a parameter when we're parsing the syslogs (in the case of a MicroOVN deployment)
, in which case this timestamp needs parsing and conversion.

Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Dec 2, 2024
1 parent cf64979 commit b4964b6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lxd/network/acl/acl_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,28 @@ type ovnLogEntry struct {
Action string `json:"action"`
}

// parseLogTime parses a timestamp that is a string format and returns a time.Time object.
// If 'timestamp' is empty (which is the case if the timestamp is contained in the 'fields' slice when we're reading an OVN log file for example),
// we try to extract the timestamp from the 'fields'.
func parseLogTime(timestamp string, fields []string) (time.Time, error) {
// When reading from a log file, the timestamp is provided as a message parameter.
if timestamp == "" {
// Parse the timestamp.
return time.Parse(time.RFC3339, fields[0])
}

// Else, if from syslog, the timestamp is provided as a separate field
// which is passed separately.
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
}

// 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 {
fields := strings.Split(input, "|")
Expand Down

0 comments on commit b4964b6

Please sign in to comment.