Skip to content
Merged
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
1 change: 1 addition & 0 deletions contrib/cirrus/setup_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ then
# Some setup needs to vary between distros
case "${OS_RELEASE_ID}-${OS_RELEASE_VER}" in
ubuntu-18)
sudo apt-get -qq -y install libsystemd-dev
# Always install runc on Ubuntu
install_runc_from_git
;;
Expand Down
4 changes: 3 additions & 1 deletion docs/podman-events.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ podman\-events - Monitor Podman events
## DESCRIPTION

Monitor and print events that occur in Podman. Each event will include a timestamp,
a type, a status, name (if applicable), and image (if applicable).
a type, a status, name (if applicable), and image (if applicable). The default logging
mechanism is *journald*. This can be changed in libpod.conf by changing the `events_logger`
value to `file`. Only `file` and `journald` are the accepted.

The *container* event type will report the follow statuses:
* attach
Expand Down
4 changes: 4 additions & 0 deletions libpod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ runc = [
"/bin/runc",
"/usr/lib/cri-o-runc/sbin/runc"
]

# Selects which logging mechanism to use for Podman events. Valid values
# are `journald` or `file`.
events_logger = "journald"
69 changes: 20 additions & 49 deletions libpod/events.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package libpod

import (
"os"

"github.com/containers/libpod/libpod/events"
"github.com/hpcloud/tail"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// newEventer returns an eventer that can be used to read/write events
func (r *Runtime) newEventer() (events.Eventer, error) {
options := events.EventerOptions{
EventerType: r.config.EventsLogger,
LogFilePath: r.config.EventsLogFilePath,
}
return events.NewEventer(options)
}

// newContainerEvent creates a new event based on a container
func (c *Container) newContainerEvent(status events.Status) {
e := events.NewEvent(status)
e.ID = c.ID()
e.Name = c.Name()
e.Image = c.config.RootfsImageName
e.Type = events.Container
if err := e.Write(c.runtime.config.EventsLogFilePath); err != nil {
logrus.Errorf("unable to write event to %s", c.runtime.config.EventsLogFilePath)
if err := c.runtime.eventer.Write(e); err != nil {
logrus.Errorf("unable to write pod event: %q", err)
}
}

Expand All @@ -29,8 +34,8 @@ func (c *Container) newContainerExitedEvent(exitCode int32) {
e.Image = c.config.RootfsImageName
e.Type = events.Container
e.ContainerExitCode = int(exitCode)
if err := e.Write(c.runtime.config.EventsLogFilePath); err != nil {
logrus.Errorf("unable to write event to %s", c.runtime.config.EventsLogFilePath)
if err := c.runtime.eventer.Write(e); err != nil {
logrus.Errorf("unable to write pod event: %q", err)
}
}

Expand All @@ -40,8 +45,8 @@ func (p *Pod) newPodEvent(status events.Status) {
e.ID = p.ID()
e.Name = p.Name()
e.Type = events.Pod
if err := e.Write(p.runtime.config.EventsLogFilePath); err != nil {
logrus.Errorf("unable to write event to %s", p.runtime.config.EventsLogFilePath)
if err := p.runtime.eventer.Write(e); err != nil {
logrus.Errorf("unable to write pod event: %q", err)
}
}

Expand All @@ -50,51 +55,17 @@ func (v *Volume) newVolumeEvent(status events.Status) {
e := events.NewEvent(status)
e.Name = v.Name()
e.Type = events.Volume
if err := e.Write(v.runtime.config.EventsLogFilePath); err != nil {
logrus.Errorf("unable to write event to %s", v.runtime.config.EventsLogFilePath)
if err := v.runtime.eventer.Write(e); err != nil {
logrus.Errorf("unable to write volume event: %q", err)
}
}

// Events is a wrapper function for everyone to begin tailing the events log
// with options
func (r *Runtime) Events(fromStart, stream bool, options []events.EventFilter, eventChannel chan *events.Event) error {
if !r.valid {
return ErrRuntimeStopped
}

t, err := r.getTail(fromStart, stream)
func (r *Runtime) Events(options events.ReadOptions) error {
eventer, err := r.newEventer()
if err != nil {
return err
}
for line := range t.Lines {
event, err := events.NewEventFromString(line.Text)
if err != nil {
return err
}
switch event.Type {
case events.Image, events.Volume, events.Pod, events.Container:
// no-op
default:
return errors.Errorf("event type %s is not valid in %s", event.Type.String(), r.config.EventsLogFilePath)
}
include := true
for _, filter := range options {
include = include && filter(event)
}
if include {
eventChannel <- event
}
}
close(eventChannel)
return nil
}

func (r *Runtime) getTail(fromStart, stream bool) (*tail.Tail, error) {
reopen := true
seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
if fromStart || !stream {
seek.Whence = 0
reopen = false
}
return tail.TailFile(r.config.EventsLogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger})
return eventer.Read(options)
}
149 changes: 149 additions & 0 deletions libpod/events/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package events

import (
"time"
)

// EventerType ...
type EventerType int

const (
// LogFile indicates the event logger will be a logfile
LogFile EventerType = iota
// Journald indicates journald should be used to log events
Journald EventerType = iota
)

// Event describes the attributes of a libpod event
type Event struct {
// ContainerExitCode is for storing the exit code of a container which can
// be used for "internal" event notification
ContainerExitCode int
// ID can be for the container, image, volume, etc
ID string
// Image used where applicable
Image string
// Name where applicable
Name string
// Status describes the event that occurred
Status Status
// Time the event occurred
Time time.Time
// Type of event that occurred
Type Type
}

// 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 string
// LogFilePath is the path to where the log file should reside if using
// the file logger
LogFilePath string
}

// Eventer is the interface for journald or file event logging
type Eventer interface {
// Write an event to a backend
Write(event Event) error
// Read an event from the backend
Read(options ReadOptions) error
}

// ReadOptions describe the attributes needed to read event logs
type ReadOptions struct {
// EventChannel is the comm path back to user
EventChannel chan *Event
// Filters are key/value pairs that describe to limit output
Filters []string
// FromStart means you start reading from the start of the logs
FromStart bool
// Since reads "since" the given time
Since string
// Stream is follow
Stream bool
// Until reads "until" the given time
Until string
}

// Type of event that occurred (container, volume, image, pod, etc)
type Type string

// Status describes the actual event action (stop, start, create, kill)
type Status string

const (
// If you add or subtract any values to the following lists, make sure you also update
// the switch statements below and the enums for EventType or EventStatus in the
// varlink description file.

// Container - event is related to containers
Container Type = "container"
// Image - event is related to images
Image Type = "image"
// Pod - event is related to pods
Pod Type = "pod"
// Volume - event is related to volumes
Volume Type = "volume"

// Attach ...
Attach Status = "attach"
// Checkpoint ...
Checkpoint Status = "checkpoint"
// Cleanup ...
Cleanup Status = "cleanup"
// Commit ...
Commit Status = "commit"
// Create ...
Create Status = "create"
// Exec ...
Exec Status = "exec"
// Exited indicates that a container's process died
Exited Status = "died"
// Export ...
Export Status = "export"
// History ...
History Status = "history"
// Import ...
Import Status = "import"
// Init ...
Init Status = "init"
// Kill ...
Kill Status = "kill"
// LoadFromArchive ...
LoadFromArchive Status = "loadfromarchive"
// Mount ...
Mount Status = "mount"
// Pause ...
Pause Status = "pause"
// Prune ...
Prune Status = "prune"
// Pull ...
Pull Status = "pull"
// Push ...
Push Status = "push"
// Remove ...
Remove Status = "remove"
// Restore ...
Restore Status = "restore"
// Save ...
Save Status = "save"
// Start ...
Start Status = "start"
// Stop ...
Stop Status = "stop"
// Sync ...
Sync Status = "sync"
// Tag ...
Tag Status = "tag"
// Unmount ...
Unmount Status = "unmount"
// Unpause ...
Unpause Status = "unpause"
// Untag ...
Untag Status = "untag"
)

// EventFilter for filtering events
type EventFilter func(*Event) bool
Loading