daemon: remove Daemon.checkContainer and related utils

This was added in 12485d62ee 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>
This commit is contained in:
Sebastiaan van Stijn 2023-08-10 17:39:56 +02:00
parent 56597db804
commit 13648a0e21
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 18 additions and 31 deletions

View file

@ -68,16 +68,6 @@ func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, e
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,
// false otherwise.
func (daemon *Daemon) Exists(id string) bool {

View file

@ -61,15 +61,19 @@ func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]s
func (daemon *Daemon) getIpcContainer(id string) (*container.Container, error) {
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)
if err != nil {
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
if st, err := os.Stat(ctr.ShmPath); err != nil || !st.IsDir() {
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) {
containerID := ctr.HostConfig.PidMode.Container()
ctr, err := daemon.GetContainer(containerID)
id := ctr.HostConfig.PidMode.Container()
ctr, err := daemon.GetContainer(id)
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)
}
if !ctr.IsRunning() {
return nil, errNotRunning(id)
}
if ctr.IsRestarting() {
return nil, errContainerIsRestarting(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))
}
return nil
}
func containerIsNotRestarting(c *container.Container) error {
if c.IsRestarting() {
return errContainerIsRestarting(c.ID)
}
return nil
return ctr, nil
}
func (daemon *Daemon) setupIpcDirs(c *container.Container) error {