diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-03-19 23:49:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-19 23:49:44 +0100 |
commit | d927b43350a079c05f54e984838b851bcc2e5931 (patch) | |
tree | 70a7e573f71d0ff3d38b349774bc19fdc2989602 /pkg/bindings/system/system.go | |
parent | aa6c8c2e55a7de14fb22f89af14d5c0636eecee0 (diff) | |
parent | d1c26af144b314f30cd69d42e33ab8cbacb080ff (diff) | |
download | podman-d927b43350a079c05f54e984838b851bcc2e5931.tar.gz podman-d927b43350a079c05f54e984838b851bcc2e5931.tar.bz2 podman-d927b43350a079c05f54e984838b851bcc2e5931.zip |
Merge pull request #5525 from baude/apiv2bindinglogs
apiv2 add bindings for logs|events
Diffstat (limited to 'pkg/bindings/system/system.go')
-rw-r--r-- | pkg/bindings/system/system.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/pkg/bindings/system/system.go b/pkg/bindings/system/system.go new file mode 100644 index 000000000..fce8bbb8e --- /dev/null +++ b/pkg/bindings/system/system.go @@ -0,0 +1,61 @@ +package system + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/url" + + "github.com/containers/libpod/pkg/api/handlers" + "github.com/containers/libpod/pkg/bindings" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +// Events allows you to monitor libdpod related events like container creation and +// removal. The events are then passed to the eventChan provided. The optional cancelChan +// can be used to cancel the read of events and close down the HTTP connection. +func Events(ctx context.Context, eventChan chan (handlers.Event), cancelChan chan bool, since, until *string, filters map[string][]string) error { + conn, err := bindings.GetClient(ctx) + if err != nil { + return err + } + params := url.Values{} + if since != nil { + params.Set("since", *since) + } + if until != nil { + params.Set("until", *until) + } + if filters != nil { + filterString, err := bindings.FiltersToString(filters) + if err != nil { + return errors.Wrap(err, "invalid filters") + } + params.Set("filters", filterString) + } + response, err := conn.DoRequest(nil, http.MethodGet, "/events", params) + if err != nil { + return err + } + if cancelChan != nil { + go func() { + <-cancelChan + err = response.Body.Close() + logrus.Error(errors.Wrap(err, "unable to close event response body")) + }() + } + dec := json.NewDecoder(response.Body) + for { + e := handlers.Event{} + if err := dec.Decode(&e); err != nil { + if err == io.EOF { + break + } + return errors.Wrap(err, "unable to decode event response") + } + eventChan <- e + } + return nil +} |