From 4138191b8d3292e4a240aa5891feeb9fa077e376 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Mon, 29 May 2017 13:16:27 +0200 Subject: [PATCH] server: add nil checks to not panic Signed-off-by: Antonio Murdaca --- server/container_exec.go | 7 +++++-- server/container_portforward.go | 4 ++++ server/server.go | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/server/container_exec.go b/server/container_exec.go index 8ca1103d..e6811d72 100644 --- a/server/container_exec.go +++ b/server/container_exec.go @@ -29,8 +29,11 @@ func (s *Server) Exec(ctx context.Context, req *pb.ExecRequest) (*pb.ExecRespons // Exec endpoint for streaming.Runtime func (ss streamService) Exec(containerID string, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error { - fmt.Println(containerID, cmd, stdin, stdout, stderr, tty, resize) - c := ss.runtimeServer.state.containers.Get(containerID) + c := ss.runtimeServer.GetContainer(containerID) + + if c == nil { + return fmt.Errorf("could not find container %q", containerID) + } if err := ss.runtimeServer.runtime.UpdateStatus(c); err != nil { return err diff --git a/server/container_portforward.go b/server/container_portforward.go index 9f01ecc3..97cf8258 100644 --- a/server/container_portforward.go +++ b/server/container_portforward.go @@ -29,6 +29,10 @@ func (s *Server) PortForward(ctx context.Context, req *pb.PortForwardRequest) (* func (ss streamService) PortForward(podSandboxID string, port int32, stream io.ReadWriteCloser) error { c := ss.runtimeServer.GetSandboxContainer(podSandboxID) + if c == nil { + return fmt.Errorf("could not find container for sandbox %q", podSandboxID) + } + if err := ss.runtimeServer.runtime.UpdateStatus(c); err != nil { return err } diff --git a/server/server.go b/server/server.go index 515e21cc..53e91d7c 100644 --- a/server/server.go +++ b/server/server.go @@ -678,6 +678,11 @@ func (s *Server) GetSandboxContainer(id string) *oci.Container { return sb.infraContainer } +// GetContainer returns a container by its ID +func (s *Server) GetContainer(id string) *oci.Container { + return s.getContainer(id) +} + func (s *Server) removeContainer(c *oci.Container) { s.stateLock.Lock() sandbox := s.state.sandboxes[c.Sandbox()]