mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-29 00:37:24 +00:00
virtcontainers: refactoring code for error handling in sandbox
Use a defined error variable replade inplace error, and shortcut for handling errors returned from function calls. Fixes: #2187 Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
parent
858f39ef75
commit
350acb2d6e
@ -65,6 +65,10 @@ const (
|
|||||||
DirMode = os.FileMode(0750) | os.ModeDir
|
DirMode = os.FileMode(0750) | os.ModeDir
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errSandboxNotRunning = errors.New("Sandbox not running")
|
||||||
|
)
|
||||||
|
|
||||||
// SandboxStatus describes a sandbox status.
|
// SandboxStatus describes a sandbox status.
|
||||||
type SandboxStatus struct {
|
type SandboxStatus struct {
|
||||||
ID string
|
ID string
|
||||||
@ -319,7 +323,7 @@ func (s *Sandbox) Status() SandboxStatus {
|
|||||||
// Monitor returns a error channel for watcher to watch at
|
// Monitor returns a error channel for watcher to watch at
|
||||||
func (s *Sandbox) Monitor(ctx context.Context) (chan error, error) {
|
func (s *Sandbox) Monitor(ctx context.Context) (chan error, error) {
|
||||||
if s.state.State != types.StateRunning {
|
if s.state.State != types.StateRunning {
|
||||||
return nil, fmt.Errorf("Sandbox is not running")
|
return nil, errSandboxNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Lock()
|
s.Lock()
|
||||||
@ -334,7 +338,7 @@ func (s *Sandbox) Monitor(ctx context.Context) (chan error, error) {
|
|||||||
// WaitProcess waits on a container process and return its exit code
|
// WaitProcess waits on a container process and return its exit code
|
||||||
func (s *Sandbox) WaitProcess(ctx context.Context, containerID, processID string) (int32, error) {
|
func (s *Sandbox) WaitProcess(ctx context.Context, containerID, processID string) (int32, error) {
|
||||||
if s.state.State != types.StateRunning {
|
if s.state.State != types.StateRunning {
|
||||||
return 0, fmt.Errorf("Sandbox not running")
|
return 0, errSandboxNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := s.findContainer(containerID)
|
c, err := s.findContainer(containerID)
|
||||||
@ -349,7 +353,7 @@ func (s *Sandbox) WaitProcess(ctx context.Context, containerID, processID string
|
|||||||
// When all is true, it sends the signal to all processes of a container.
|
// When all is true, it sends the signal to all processes of a container.
|
||||||
func (s *Sandbox) SignalProcess(ctx context.Context, containerID, processID string, signal syscall.Signal, all bool) error {
|
func (s *Sandbox) SignalProcess(ctx context.Context, containerID, processID string, signal syscall.Signal, all bool) error {
|
||||||
if s.state.State != types.StateRunning {
|
if s.state.State != types.StateRunning {
|
||||||
return fmt.Errorf("Sandbox not running")
|
return errSandboxNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := s.findContainer(containerID)
|
c, err := s.findContainer(containerID)
|
||||||
@ -363,7 +367,7 @@ func (s *Sandbox) SignalProcess(ctx context.Context, containerID, processID stri
|
|||||||
// WinsizeProcess resizes the tty window of a process
|
// WinsizeProcess resizes the tty window of a process
|
||||||
func (s *Sandbox) WinsizeProcess(ctx context.Context, containerID, processID string, height, width uint32) error {
|
func (s *Sandbox) WinsizeProcess(ctx context.Context, containerID, processID string, height, width uint32) error {
|
||||||
if s.state.State != types.StateRunning {
|
if s.state.State != types.StateRunning {
|
||||||
return fmt.Errorf("Sandbox not running")
|
return errSandboxNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := s.findContainer(containerID)
|
c, err := s.findContainer(containerID)
|
||||||
@ -377,7 +381,7 @@ func (s *Sandbox) WinsizeProcess(ctx context.Context, containerID, processID str
|
|||||||
// IOStream returns stdin writer, stdout reader and stderr reader of a process
|
// IOStream returns stdin writer, stdout reader and stderr reader of a process
|
||||||
func (s *Sandbox) IOStream(containerID, processID string) (io.WriteCloser, io.Reader, io.Reader, error) {
|
func (s *Sandbox) IOStream(containerID, processID string) (io.WriteCloser, io.Reader, io.Reader, error) {
|
||||||
if s.state.State != types.StateRunning {
|
if s.state.State != types.StateRunning {
|
||||||
return nil, nil, nil, fmt.Errorf("Sandbox not running")
|
return nil, nil, nil, errSandboxNotRunning
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := s.findContainer(containerID)
|
c, err := s.findContainer(containerID)
|
||||||
@ -1122,8 +1126,7 @@ func (s *Sandbox) CreateContainer(ctx context.Context, contConfig ContainerConfi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create and start the container
|
// create and start the container
|
||||||
err = c.create(ctx)
|
if err = c.create(ctx); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1138,8 +1141,8 @@ func (s *Sandbox) CreateContainer(ctx context.Context, contConfig ContainerConfi
|
|||||||
logger := s.Logger().WithFields(logrus.Fields{"container-id": c.id, "sandox-id": s.id, "rollback": true})
|
logger := s.Logger().WithFields(logrus.Fields{"container-id": c.id, "sandox-id": s.id, "rollback": true})
|
||||||
logger.WithError(err).Error("Cleaning up partially created container")
|
logger.WithError(err).Error("Cleaning up partially created container")
|
||||||
|
|
||||||
if err2 := c.stop(ctx, true); err2 != nil {
|
if errStop := c.stop(ctx, true); errStop != nil {
|
||||||
logger.WithError(err2).Error("Could not delete container")
|
logger.WithError(errStop).Error("Could not stop container")
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Removing stopped container from sandbox store")
|
logger.Debug("Removing stopped container from sandbox store")
|
||||||
@ -1150,8 +1153,7 @@ func (s *Sandbox) CreateContainer(ctx context.Context, contConfig ContainerConfi
|
|||||||
// Sandbox is responsible to update VM resources needed by Containers
|
// Sandbox is responsible to update VM resources needed by Containers
|
||||||
// Update resources after having added containers to the sandbox, since
|
// Update resources after having added containers to the sandbox, since
|
||||||
// container status is requiered to know if more resources should be added.
|
// container status is requiered to know if more resources should be added.
|
||||||
err = s.updateResources(ctx)
|
if err = s.updateResources(ctx); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1175,8 +1177,7 @@ func (s *Sandbox) StartContainer(ctx context.Context, containerID string) (VCCon
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start it.
|
// Start it.
|
||||||
err = c.start(ctx)
|
if err = c.start(ctx); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1188,8 +1189,7 @@ func (s *Sandbox) StartContainer(ctx context.Context, containerID string) (VCCon
|
|||||||
|
|
||||||
// Update sandbox resources in case a stopped container
|
// Update sandbox resources in case a stopped container
|
||||||
// is started
|
// is started
|
||||||
err = s.updateResources(ctx)
|
if err = s.updateResources(ctx); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1248,8 +1248,7 @@ func (s *Sandbox) DeleteContainer(ctx context.Context, containerID string) (VCCo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete it.
|
// Delete it.
|
||||||
err = c.delete(ctx)
|
if err = c.delete(ctx); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1323,8 +1322,7 @@ func (s *Sandbox) UpdateContainer(ctx context.Context, containerID string, resou
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.update(ctx, resources)
|
if err = c.update(ctx, resources); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user