From ec69e86fabb9b4323161971e75211181d826b51c Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Tue, 1 Sep 2020 15:48:52 -0400 Subject: [PATCH] oci: return IsAlive error instead of logging When a container has been stopped, but the rest of its pod is still stopping, the kubelet still runs exec probes IsAlive() correctly identifies the container has been stopped, and logs an error, but in reality, this is expected. Instead of logging the error, return it in IsAlive (and also ExecSync), and let the kubelet report it if it thinks it'll be problematic This fixes superluous errors like this: "Checking if PID of 4a81020e858fbdd1ee6a271190ab36aec1940489386e177f33c2e62afa309580 is running failed: PID running but not the original container. PID wrap may have occurred" Signed-off-by: Peter Hunt --- internal/oci/container.go | 9 ++------- internal/oci/container_test.go | 6 +++--- server/container_execsync.go | 9 ++++----- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/internal/oci/container.go b/internal/oci/container.go index 745f895021e..021b2408c79 100644 --- a/internal/oci/container.go +++ b/internal/oci/container.go @@ -391,14 +391,9 @@ func (c *Container) exitFilePath() string { // IsAlive is a function that checks if a container's init PID exists. // It is used to check a container state when we don't want a `$runtime state` call -func (c *Container) IsAlive() bool { +func (c *Container) IsAlive() error { _, err := c.pid() - if err != nil { - logrus.Errorf("checking if PID of %s is running failed: %v", c.id, err) - return false - } - - return true + return errors.Wrapf(err, "checking if PID of %s is running failed", c.id) } // Pid returns the container's init PID. diff --git a/internal/oci/container_test.go b/internal/oci/container_test.go index fd031d2188d..72f0db51842 100644 --- a/internal/oci/container_test.go +++ b/internal/oci/container_test.go @@ -306,7 +306,7 @@ var _ = t.Describe("Container", func() { err := sut.IsAlive() // Then - Expect(err).To(Equal(false)) + Expect(err).NotTo(BeNil()) }) It("should succeed if pid is running", func() { // Given @@ -318,7 +318,7 @@ var _ = t.Describe("Container", func() { err := sut.IsAlive() // Then - Expect(err).To(Equal(true)) + Expect(err).To(BeNil()) }) It("should be false if pid is not running", func() { // Given @@ -331,7 +331,7 @@ var _ = t.Describe("Container", func() { err := sut.IsAlive() // Then - Expect(err).To(Equal(false)) + Expect(err).NotTo(BeNil()) }) }) t.Describe("Pid", func() { diff --git a/server/container_execsync.go b/server/container_execsync.go index 21071f8855b..316c0c2317a 100644 --- a/server/container_execsync.go +++ b/server/container_execsync.go @@ -1,8 +1,7 @@ package server import ( - "fmt" - + "github.com/pkg/errors" "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -16,13 +15,13 @@ func (s *Server) ExecSync(ctx context.Context, req *pb.ExecSyncRequest) (*pb.Exe return nil, status.Errorf(codes.NotFound, "could not find container %q: %v", req.ContainerId, err) } - if !c.IsAlive() { - return nil, fmt.Errorf("container is not created or running") + if err := c.IsAlive(); err != nil { + return nil, status.Errorf(codes.NotFound, "container is not created or running: %v", err) } cmd := req.Cmd if cmd == nil { - return nil, fmt.Errorf("exec command cannot be empty") + return nil, errors.New("exec command cannot be empty") } execResp, err := s.Runtime().ExecSyncContainer(c, cmd, req.Timeout)