summaryrefslogtreecommitdiff
path: root/pkg/varlinkapi/events.go
blob: 8628b1ce6b3cd3bdd515fbfd3ff0f79ce8e26ed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// +build varlink

package varlinkapi

import (
	"context"
	"time"

	"github.com/containers/podman/v2/libpod/events"
	iopodman "github.com/containers/podman/v2/pkg/varlink"
)

// GetEvents is a remote endpoint to get events from the event log
func (i *VarlinkAPI) GetEvents(call iopodman.VarlinkCall, filter []string, since string, until string) error {
	var (
		fromStart   bool
		eventsError error
		event       *events.Event
		stream      bool
	)
	if call.WantsMore() {
		stream = true
		call.Continues = true
	}
	if len(since) > 0 || len(until) > 0 {
		fromStart = true
	}
	eventChannel := make(chan *events.Event)
	go func() {
		readOpts := events.ReadOptions{FromStart: fromStart, Stream: stream, Filters: filter, EventChannel: eventChannel}
		eventsError = i.Runtime.Events(context.Background(), readOpts)
	}()
	if eventsError != nil {
		return call.ReplyErrorOccurred(eventsError.Error())
	}
	for {
		event = <-eventChannel
		if event == nil {
			call.Continues = false
			break
		}
		call.ReplyGetEvents(iopodman.Event{
			Id:     event.ID,
			Image:  event.Image,
			Name:   event.Name,
			Status: string(event.Status),
			Time:   event.Time.Format(time.RFC3339Nano),
			Type:   string(event.Type),
		})
		if !call.Continues {
			// For a one-shot on events, we break out here
			break
		}
	}
	return nil
}