podman events format json

Enable podman events to format the output as jsonline

Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
Qi Wang 2019-05-20 17:23:03 -04:00
parent 3cc9ab8992
commit 0ad374af6a
5 changed files with 71 additions and 18 deletions

View File

@ -68,7 +68,7 @@ Print usage statement.
**--format** **--format**
Format the output using the given Go template. An output value of *json* is not supported. Format the output to JSON Lines or using the given Go template.
**--filter**=*filter* **--filter**=*filter*
@ -134,6 +134,13 @@ $ sudo podman events --since 5m
2019-03-02 10:44:42.374637304 -0600 CST pod create ca731231718e (image=, name=webapp) 2019-03-02 10:44:42.374637304 -0600 CST pod create ca731231718e (image=, name=webapp)
``` ```
Show podman events in JSON Lines format
```
events --format json
{"ID":"683b0909d556a9c02fa8cd2b61c3531a965db42158627622d1a67b391964d519","Image":"localhost/myshdemo:latest","Name":"agitated_diffie","Status":"cleanup","Time":"2019-04-27T22:47:00.849932843-04:00","Type":"container"}
{"ID":"a0f8ab051bfd43f9c5141a8a2502139707e4b38d98ac0872e57c5315381e88ad","Image":"docker.io/library/alpine:latest","Name":"friendly_tereshkova","Status":"unmount","Time":"2019-04-28T13:43:38.063017276-04:00","Type":"container"}
```
## SEE ALSO ## SEE ALSO
podman(1) podman(1)

View File

@ -22,13 +22,13 @@ const (
type Event struct { type Event struct {
// ContainerExitCode is for storing the exit code of a container which can // ContainerExitCode is for storing the exit code of a container which can
// be used for "internal" event notification // be used for "internal" event notification
ContainerExitCode int ContainerExitCode int `json:",omitempty"`
// ID can be for the container, image, volume, etc // ID can be for the container, image, volume, etc
ID string ID string `json:",omitempty"`
// Image used where applicable // Image used where applicable
Image string Image string `json:",omitempty"`
// Name where applicable // Name where applicable
Name string Name string `json:",omitempty"`
// Status describes the event that occurred // Status describes the event that occurred
Status Status Status Status
// Time the event occurred // Time the event occurred

View File

@ -5,22 +5,22 @@ package adapter
import ( import (
"bufio" "bufio"
"context" "context"
"github.com/containers/libpod/libpod/define"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"text/template" "text/template"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/buildah" "github.com/containers/buildah"
"github.com/containers/buildah/imagebuildah" "github.com/containers/buildah/imagebuildah"
"github.com/containers/buildah/pkg/formats"
"github.com/containers/buildah/pkg/parse" "github.com/containers/buildah/pkg/parse"
"github.com/containers/image/docker/reference" "github.com/containers/image/docker/reference"
"github.com/containers/image/types" "github.com/containers/image/types"
"github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime" "github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod" "github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/define"
"github.com/containers/libpod/libpod/events" "github.com/containers/libpod/libpod/events"
"github.com/containers/libpod/libpod/image" "github.com/containers/libpod/libpod/image"
"github.com/containers/libpod/pkg/rootless" "github.com/containers/libpod/pkg/rootless"
@ -351,9 +351,13 @@ func (r *LocalRuntime) Events(c *cliconfig.EventValues) error {
fromStart bool fromStart bool
eventsError error eventsError error
) )
tmpl, err := template.New("events").Parse(c.Format) var tmpl *template.Template
if err != nil { if c.Format != formats.JSONString {
return err template, err := template.New("events").Parse(c.Format)
if err != nil {
return err
}
tmpl = template
} }
if len(c.Since) > 0 || len(c.Until) > 0 { if len(c.Since) > 0 || len(c.Until) > 0 {
fromStart = true fromStart = true
@ -369,7 +373,15 @@ func (r *LocalRuntime) Events(c *cliconfig.EventValues) error {
} }
w := bufio.NewWriter(os.Stdout) w := bufio.NewWriter(os.Stdout)
for event := range eventChannel { for event := range eventChannel {
if len(c.Format) > 0 { if c.Format == formats.JSONString {
jsonStr, err := event.ToJSONString()
if err != nil {
return errors.Wrapf(err, "unable to format json")
}
if _, err := w.Write([]byte(jsonStr)); err != nil {
return err
}
} else if len(c.Format) > 0 {
if err := tmpl.Execute(w, event); err != nil { if err := tmpl.Execute(w, event); err != nil {
return err return err
} }

View File

@ -14,9 +14,8 @@ import (
"text/template" "text/template"
"time" "time"
v1 "k8s.io/api/core/v1"
"github.com/containers/buildah/imagebuildah" "github.com/containers/buildah/imagebuildah"
"github.com/containers/buildah/pkg/formats"
"github.com/containers/image/docker/reference" "github.com/containers/image/docker/reference"
"github.com/containers/image/types" "github.com/containers/image/types"
"github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/cliconfig"
@ -32,6 +31,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/varlink/go/varlink" "github.com/varlink/go/varlink"
v1 "k8s.io/api/core/v1"
) )
// ImageRuntime is wrapper for image runtime // ImageRuntime is wrapper for image runtime
@ -820,9 +820,13 @@ func (r *LocalRuntime) Events(c *cliconfig.EventValues) error {
} }
w := bufio.NewWriter(os.Stdout) w := bufio.NewWriter(os.Stdout)
tmpl, err := template.New("events").Parse(c.Format) var tmpl *template.Template
if err != nil { if c.Format != formats.JSONString {
return err template, err := template.New("events").Parse(c.Format)
if err != nil {
return err
}
tmpl = template
} }
for { for {
@ -856,7 +860,15 @@ func (r *LocalRuntime) Events(c *cliconfig.EventValues) error {
Time: eTime, Time: eTime,
Type: eType, Type: eType,
} }
if len(c.Format) > 0 { if c.Format == formats.JSONString {
jsonStr, err := event.ToJSONString()
if err != nil {
return errors.Wrapf(err, "unable to format json")
}
if _, err := w.Write([]byte(jsonStr)); err != nil {
return err
}
} else if len(c.Format) > 0 {
if err := tmpl.Execute(w, event); err != nil { if err := tmpl.Execute(w, event); err != nil {
return err return err
} }

View File

@ -1,6 +1,7 @@
package integration package integration
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -116,4 +117,25 @@ var _ = Describe("Podman events", func() {
Expect(result.ExitCode()).To(BeZero()) Expect(result.ExitCode()).To(BeZero())
}) })
It("podman events format", func() {
info := GetHostDistributionInfo()
if info.Distribution != "fedora" {
Skip("need to verify images have correct packages for journald")
}
_, ec, _ := podmanTest.RunLsContainer("")
Expect(ec).To(Equal(0))
test := podmanTest.Podman([]string{"events", "--stream=false", "--format", "json"})
test.WaitWithDefaultTimeout()
fmt.Println(test.OutputToStringArray())
jsonArr := test.OutputToStringArray()
Expect(len(jsonArr)).To(Not(BeZero()))
eventsMap := make(map[string]string)
err := json.Unmarshal([]byte(jsonArr[0]), &eventsMap)
if err != nil {
os.Exit(1)
}
_, exist := eventsMap["Status"]
Expect(exist).To(BeTrue())
Expect(test.ExitCode()).To(BeZero())
})
}) })