Add operation lock for containers

Signed-off-by: Mrunal Patel <mpatel@redhat.com>
This commit is contained in:
Mrunal Patel 2016-12-13 12:50:27 -08:00
parent 4f21dc6492
commit 2800b83f2f

View file

@ -143,6 +143,8 @@ func (r *Runtime) CreateContainer(c *Container) error {
// StartContainer starts a container. // StartContainer starts a container.
func (r *Runtime) StartContainer(c *Container) error { func (r *Runtime) StartContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name); err != nil { if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name); err != nil {
return err return err
} }
@ -265,6 +267,8 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
// StopContainer stops a container. // StopContainer stops a container.
func (r *Runtime) StopContainer(c *Container) error { func (r *Runtime) StopContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil { if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil {
return err return err
} }
@ -291,13 +295,15 @@ func (r *Runtime) StopContainer(c *Container) error {
// DeleteContainer deletes a container. // DeleteContainer deletes a container.
func (r *Runtime) DeleteContainer(c *Container) error { func (r *Runtime) DeleteContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "delete", c.name) return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "delete", c.name)
} }
// UpdateStatus refreshes the status of the container. // UpdateStatus refreshes the status of the container.
func (r *Runtime) UpdateStatus(c *Container) error { func (r *Runtime) UpdateStatus(c *Container) error {
c.stateLock.Lock() c.opLock.Lock()
defer c.stateLock.Unlock() defer c.opLock.Unlock()
out, err := exec.Command(r.path, "state", c.name).CombinedOutput() out, err := exec.Command(r.path, "state", c.name).CombinedOutput()
if err != nil { if err != nil {
return fmt.Errorf("error getting container state for %s: %s: %q", c.name, err, out) return fmt.Errorf("error getting container state for %s: %s: %q", c.name, err, out)
@ -331,8 +337,8 @@ func (r *Runtime) UpdateStatus(c *Container) error {
// ContainerStatus returns the state of a container. // ContainerStatus returns the state of a container.
func (r *Runtime) ContainerStatus(c *Container) *ContainerState { func (r *Runtime) ContainerStatus(c *Container) *ContainerState {
c.stateLock.Lock() c.opLock.Lock()
defer c.stateLock.Unlock() defer c.opLock.Unlock()
return c.state return c.state
} }
@ -350,7 +356,7 @@ type Container struct {
terminal bool terminal bool
state *ContainerState state *ContainerState
metadata *pb.ContainerMetadata metadata *pb.ContainerMetadata
stateLock sync.Mutex opLock sync.Mutex
} }
// ContainerState represents the status of a container. // ContainerState represents the status of a container.