Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crictl events command #1241

Merged
merged 6 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions cmd/crictl/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

var eventsCommand = &cli.Command{
Name: "events",
Usage: "Fetch the events of containers",
Usage: "Stream the events of containers",
Aliases: []string{"event"},
UseShortOptionHandling: true,
Flags: []cli.Flag{
Expand All @@ -48,6 +48,16 @@ var eventsCommand = &cli.Command{
return cli.ShowSubcommandHelp(c)
}

switch format := c.String("output"); format {
case "json", "yaml":
if len(c.String("template")) > 0 {
return fmt.Errorf("template can't be used with %q format", format)
}
case "go-template":
default:
return fmt.Errorf("don't support %q format", format)
}

runtimeClient, err := getRuntimeService(c, 0)
if err != nil {
return err
Expand Down Expand Up @@ -82,7 +92,7 @@ func Events(cliContext *cli.Context, client internalapi.RuntimeService) error {
case e := <-containerEventsCh:
err := outputEvent(e, cliContext.String("output"), cliContext.String("template"))
if err != nil {
return err
fmt.Printf("formatting container event: %s\n", err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/crictl.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ COMMANDS:
- `statsp`: List pod(s) resource usage statistics
- `completion`: Output bash shell completion code
- `checkpoint`: Checkpoint one or more running containers
- `events, event`: Fetch the events of containers
- `events, event`: Stream the events of containers
- `help, h`: Shows a list of commands or help for one command

`crictl` by default connects on Unix to:
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ import (

// The actual test suite
var _ = t.Describe("events", func() {
It("should fail with not supported output format", func() {
t.CrictlExpectFailure("events --output=ini", "", "don't support \"ini\" format")
})

It("should fail with template set for non go-template format", func() {
t.CrictlExpectFailure("events --template=\"{{ .containerID }}\"", "", "template can't be used with \"json\" format")
})

It("should succeed", func() {
// Given
endpoint, testDir, crio := t.StartCrio()
Expand Down