From a21e112364f93a1a7d342584a239b289a8ee9f0a Mon Sep 17 00:00:00 2001
From: Jhon Honce <jhonce@redhat.com>
Date: Mon, 4 Apr 2022 13:04:40 -0700
Subject: Add podman machine events

Signed-off-by: Jhon Honce <jhonce@redhat.com>
---
 libpod/events/config.go       |  6 +++++-
 libpod/events/events.go       | 22 ++++++++++++-------
 libpod/events/events_linux.go |  2 ++
 libpod/events/memory.go       | 49 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 9 deletions(-)
 create mode 100644 libpod/events/memory.go

(limited to 'libpod')

diff --git a/libpod/events/config.go b/libpod/events/config.go
index 35680a275..00cdca007 100644
--- a/libpod/events/config.go
+++ b/libpod/events/config.go
@@ -17,6 +17,8 @@ const (
 	Journald EventerType = iota
 	// Null is a no-op events logger. It does not read or write events.
 	Null EventerType = iota
+	// Memory indicates the event logger will hold events in memory
+	Memory EventerType = iota
 )
 
 // Event describes the attributes of a libpod event
@@ -55,7 +57,7 @@ type Details struct {
 // EventerOptions describe options that need to be passed to create
 // an eventer
 type EventerOptions struct {
-	// EventerType describes whether to use journald or a file
+	// EventerType describes whether to use journald, file or memory
 	EventerType string
 	// LogFilePath is the path to where the log file should reside if using
 	// the file logger
@@ -110,6 +112,8 @@ const (
 	System Type = "system"
 	// Volume - event is related to volumes
 	Volume Type = "volume"
+	// Machine - event is related to machine VM's
+	Machine Type = "machine"
 
 	// Attach ...
 	Attach Status = "attach"
diff --git a/libpod/events/events.go b/libpod/events/events.go
index 1745095fb..04417fd8d 100644
--- a/libpod/events/events.go
+++ b/libpod/events/events.go
@@ -20,6 +20,8 @@ func (et EventerType) String() string {
 		return "file"
 	case Journald:
 		return "journald"
+	case Memory:
+		return "memory"
 	case Null:
 		return "none"
 	default:
@@ -34,6 +36,8 @@ func IsValidEventer(eventer string) bool {
 		return true
 	case Journald.String():
 		return true
+	case Memory.String():
+		return true
 	case Null.String():
 		return true
 	default:
@@ -41,7 +45,7 @@ func IsValidEventer(eventer string) bool {
 	}
 }
 
-// NewEvent creates a event struct and populates with
+// NewEvent creates an event struct and populates with
 // the given status and time.
 func NewEvent(status Status) Event {
 	return Event{
@@ -63,7 +67,7 @@ func (e *Event) ToJSONString() (string, error) {
 	return string(b), err
 }
 
-// ToHumanReadable returns human readable event as a formatted string
+// ToHumanReadable returns human-readable event as a formatted string
 func (e *Event) ToHumanReadable(truncate bool) string {
 	var humanFormat string
 	id := e.ID
@@ -90,7 +94,7 @@ func (e *Event) ToHumanReadable(truncate bool) string {
 		} else {
 			humanFormat = fmt.Sprintf("%s %s %s", e.Time, e.Type, e.Status)
 		}
-	case Volume:
+	case Volume, Machine:
 		humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name)
 	}
 	return humanFormat
@@ -99,19 +103,19 @@ func (e *Event) ToHumanReadable(truncate bool) string {
 // NewEventFromString takes stringified json and converts
 // it to an event
 func newEventFromJSONString(event string) (*Event, error) {
-	e := Event{}
-	if err := json.Unmarshal([]byte(event), &e); err != nil {
+	e := new(Event)
+	if err := json.Unmarshal([]byte(event), e); err != nil {
 		return nil, err
 	}
-	return &e, nil
+	return e, nil
 }
 
-// ToString converts a Type to a string
+// String converts a Type to a string
 func (t Type) String() string {
 	return string(t)
 }
 
-// ToString converts a status to a string
+// String converts a status to a string
 func (s Status) String() string {
 	return string(s)
 }
@@ -123,6 +127,8 @@ func StringToType(name string) (Type, error) {
 		return Container, nil
 	case Image.String():
 		return Image, nil
+	case Machine.String():
+		return Machine, nil
 	case Network.String():
 		return Network, nil
 	case Pod.String():
diff --git a/libpod/events/events_linux.go b/libpod/events/events_linux.go
index 482d7d6dd..4320f2190 100644
--- a/libpod/events/events_linux.go
+++ b/libpod/events/events_linux.go
@@ -21,6 +21,8 @@ func NewEventer(options EventerOptions) (Eventer, error) {
 		return EventLogFile{options}, nil
 	case strings.ToUpper(Null.String()):
 		return NewNullEventer(), nil
+	case strings.ToUpper(Memory.String()):
+		return NewMemoryEventer(), nil
 	default:
 		return nil, errors.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType))
 	}
diff --git a/libpod/events/memory.go b/libpod/events/memory.go
new file mode 100644
index 000000000..b3e03d86b
--- /dev/null
+++ b/libpod/events/memory.go
@@ -0,0 +1,49 @@
+package events
+
+import (
+	"context"
+)
+
+// EventMemory is the structure for event writing to a channel. It contains the eventer
+// options and the event itself.  Methods for reading and writing are also defined from it.
+type EventMemory struct {
+	options  EventerOptions
+	elements chan *Event
+}
+
+// Write event to memory queue
+func (e EventMemory) Write(event Event) (err error) {
+	e.elements <- &event
+	return
+}
+
+// Read event(s) from memory queue
+func (e EventMemory) Read(ctx context.Context, options ReadOptions) (err error) {
+	select {
+	case <-ctx.Done():
+		return
+	default:
+	}
+
+	select {
+	case event := <-e.elements:
+		options.EventChannel <- event
+	default:
+	}
+	return nil
+}
+
+// String returns eventer type
+func (e EventMemory) String() string {
+	return e.options.EventerType
+}
+
+// NewMemoryEventer returns configured MemoryEventer
+func NewMemoryEventer() Eventer {
+	return EventMemory{
+		options: EventerOptions{
+			EventerType: Memory.String(),
+		},
+		elements: make(chan *Event, 100),
+	}
+}
-- 
cgit v1.2.3-54-g00ecf