-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yury Gargay <[email protected]>
- Loading branch information
Showing
6 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
internalapi "k8s.io/cri-api/pkg/apis" | ||
pb "k8s.io/cri-api/pkg/apis/runtime/v1" | ||
) | ||
|
||
var eventsCommand = &cli.Command{ | ||
Name: "events", | ||
Usage: "Fetch the events of containers", | ||
Aliases: []string{"event"}, | ||
UseShortOptionHandling: true, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "output", | ||
Aliases: []string{"o"}, | ||
Value: "json", | ||
Usage: "Output format, One of: json|yaml|go-template", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "template", | ||
Usage: "The template string is only used when output is go-template; The Template format is golang template", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
if c.NArg() != 0 { | ||
return cli.ShowSubcommandHelp(c) | ||
} | ||
|
||
runtimeClient, err := getRuntimeService(c, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = Events(c, runtimeClient); err != nil { | ||
return fmt.Errorf("getting container events: %w", err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func Events(cliContext *cli.Context, client internalapi.RuntimeService) error { | ||
errCh := make(chan error, 1) | ||
|
||
containerEventsCh := make(chan *pb.ContainerEventResponse) | ||
go func() { | ||
logrus.Debug("getting container events") | ||
err := client.GetContainerEvents(containerEventsCh) | ||
if err == io.EOF { | ||
errCh <- nil | ||
return | ||
} | ||
errCh <- err | ||
}() | ||
|
||
for { | ||
select { | ||
case err := <-errCh: | ||
return err | ||
case e := <-containerEventsCh: | ||
err := outputEvent(e, cliContext.String("output"), cliContext.String("template")) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package e2e | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/onsi/gomega/gbytes" | ||
) | ||
|
||
// The actual test suite | ||
var _ = t.Describe("events", func() { | ||
It("should succeed", func() { | ||
// Given | ||
endpoint, testDir, crio := t.StartCrio() | ||
|
||
// When | ||
session := t.CrictlWithEndpointNoWait(endpoint, "events") | ||
Expect(session.Out).ToNot(Say("unknown method GetContainerEvents")) // no errors | ||
|
||
// Then | ||
session.Terminate() | ||
t.StopCrio(testDir, crio) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters