|
| 1 | +// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16: |
| 2 | +//go:build go1.23 |
| 3 | + |
1 | 4 | package daemon |
2 | 5 |
|
3 | 6 | import ( |
4 | 7 | "context" |
5 | 8 | "fmt" |
| 9 | + "maps" |
6 | 10 | "os" |
7 | 11 | "path/filepath" |
8 | 12 | "runtime" |
| 13 | + "slices" |
9 | 14 | "strings" |
10 | 15 | "time" |
11 | 16 |
|
@@ -196,6 +201,31 @@ func (daemon *Daemon) GetByName(name string) (*container.Container, error) { |
196 | 201 | return e, nil |
197 | 202 | } |
198 | 203 |
|
| 204 | +// GetDependentContainers returns a list of containers that depend on the given container. |
| 205 | +// Dependencies are determined by: |
| 206 | +// - Network mode dependencies (--network=container:xxx) |
| 207 | +// - Legacy container links (--link) |
| 208 | +// |
| 209 | +// This is primarily used during daemon startup to determine container startup order, |
| 210 | +// ensuring that dependent containers are started after their dependencies are running. |
| 211 | +// Upon error, it returns the last known dependent containers, which may be empty. |
| 212 | +func (daemon *Daemon) GetDependentContainers(c *container.Container) []*container.Container { |
| 213 | + var dependentContainers []*container.Container |
| 214 | + |
| 215 | + if c.HostConfig.NetworkMode.IsContainer() { |
| 216 | + // If the container is using a network mode that depends on another container, |
| 217 | + // we need to find that container and add it to the dependency map. |
| 218 | + dependencyContainer, err := daemon.GetContainer(c.HostConfig.NetworkMode.ConnectedContainer()) |
| 219 | + if err != nil { |
| 220 | + log.G(context.TODO()).WithError(err).Errorf("Could not find dependent container for %s", c.ID) |
| 221 | + return dependentContainers |
| 222 | + } |
| 223 | + dependentContainers = append(dependentContainers, dependencyContainer) |
| 224 | + } |
| 225 | + |
| 226 | + return append(dependentContainers, slices.Collect(maps.Values(daemon.linkIndex.children(c)))...) |
| 227 | +} |
| 228 | + |
199 | 229 | func (daemon *Daemon) setSecurityOptions(cfg *config.Config, container *container.Container, hostConfig *containertypes.HostConfig) error { |
200 | 230 | container.Lock() |
201 | 231 | defer container.Unlock() |
|
0 commit comments