Procházet zdrojové kódy

daemon: remove Daemon.checkContainer and related utils

This was added in 12485d62eeba27119260dc5eb54ac4fa83c3130b to save some
duplication, but was really over-engineered to save a few lines of code,
at the cost of hiding away what it does and also potentially returning
inconsistent errors (not addressed in this patch). Let's start with
inlining these.

This removes;

- Daemon.checkContainer
- daemon.containerIsRunning
- daemon.containerIsNotRestarting

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn před 1 rokem
rodič
revize
13648a0e21
2 změnil soubory, kde provedl 17 přidání a 30 odebrání
  1. 0 10
      daemon/container.go
  2. 17 20
      daemon/container_operations_unix.go

+ 0 - 10
daemon/container.go

@@ -68,16 +68,6 @@ func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, e
 	return ctr, nil
 	return ctr, nil
 }
 }
 
 
-// checkContainer make sure the specified container validates the specified conditions
-func (daemon *Daemon) checkContainer(container *container.Container, conditions ...func(*container.Container) error) error {
-	for _, condition := range conditions {
-		if err := condition(container); err != nil {
-			return err
-		}
-	}
-	return nil
-}
-
 // Exists returns a true if a container of the specified ID or name exists,
 // Exists returns a true if a container of the specified ID or name exists,
 // false otherwise.
 // false otherwise.
 func (daemon *Daemon) Exists(id string) bool {
 func (daemon *Daemon) Exists(id string) bool {

+ 17 - 20
daemon/container_operations_unix.go

@@ -61,15 +61,19 @@ func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]s
 
 
 func (daemon *Daemon) getIpcContainer(id string) (*container.Container, error) {
 func (daemon *Daemon) getIpcContainer(id string) (*container.Container, error) {
 	errMsg := "can't join IPC of container " + id
 	errMsg := "can't join IPC of container " + id
-	// Check the container exists
+
+	// Check if the container exists, is running, and not restarting
 	ctr, err := daemon.GetContainer(id)
 	ctr, err := daemon.GetContainer(id)
 	if err != nil {
 	if err != nil {
 		return nil, errors.Wrap(err, errMsg)
 		return nil, errors.Wrap(err, errMsg)
 	}
 	}
-	// Check the container is running and not restarting
-	if err := daemon.checkContainer(ctr, containerIsRunning, containerIsNotRestarting); err != nil {
-		return nil, errors.Wrap(err, errMsg)
+	if !ctr.IsRunning() {
+		return nil, errNotRunning(id)
 	}
 	}
+	if ctr.IsRestarting() {
+		return nil, errContainerIsRestarting(id)
+	}
+
 	// Check the container ipc is shareable
 	// Check the container ipc is shareable
 	if st, err := os.Stat(ctr.ShmPath); err != nil || !st.IsDir() {
 	if st, err := os.Stat(ctr.ShmPath); err != nil || !st.IsDir() {
 		if err == nil || os.IsNotExist(err) {
 		if err == nil || os.IsNotExist(err) {
@@ -83,26 +87,19 @@ func (daemon *Daemon) getIpcContainer(id string) (*container.Container, error) {
 }
 }
 
 
 func (daemon *Daemon) getPidContainer(ctr *container.Container) (*container.Container, error) {
 func (daemon *Daemon) getPidContainer(ctr *container.Container) (*container.Container, error) {
-	containerID := ctr.HostConfig.PidMode.Container()
-	ctr, err := daemon.GetContainer(containerID)
+	id := ctr.HostConfig.PidMode.Container()
+	ctr, err := daemon.GetContainer(id)
 	if err != nil {
 	if err != nil {
-		return nil, errors.Wrapf(err, "cannot join PID of a non running container: %s", containerID)
+		return nil, errors.Wrapf(err, "cannot join PID of a non running container: %s", id)
 	}
 	}
-	return ctr, daemon.checkContainer(ctr, containerIsRunning, containerIsNotRestarting)
-}
-
-func containerIsRunning(c *container.Container) error {
-	if !c.IsRunning() {
-		return errdefs.Conflict(errors.Errorf("container %s is not running", c.ID))
+	if !ctr.IsRunning() {
+		return nil, errNotRunning(id)
 	}
 	}
-	return nil
-}
-
-func containerIsNotRestarting(c *container.Container) error {
-	if c.IsRestarting() {
-		return errContainerIsRestarting(c.ID)
+	if ctr.IsRestarting() {
+		return nil, errContainerIsRestarting(id)
 	}
 	}
-	return nil
+
+	return ctr, nil
 }
 }
 
 
 func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
 func (daemon *Daemon) setupIpcDirs(c *container.Container) error {