daemon: rename variables that shadowed imports

Not a full list yet, but renaming to prevent shadowing, and to use a more
consistent short form (ctr for container).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-06-17 00:10:04 +02:00
parent 21a7686862
commit 7b438c5c31
3 changed files with 249 additions and 249 deletions

View File

@@ -45,11 +45,11 @@ func ipAddresses(ips []net.IP) []string {
return addrs
}
func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *container.Container) ([]libnetwork.SandboxOption, error) {
func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnetwork.SandboxOption, error) {
var sboxOptions []libnetwork.SandboxOption
sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname), libnetwork.OptionDomainname(container.Config.Domainname))
sboxOptions = append(sboxOptions, libnetwork.OptionHostname(ctr.Config.Hostname), libnetwork.OptionDomainname(ctr.Config.Domainname))
if container.HostConfig.NetworkMode.IsHost() {
if ctr.HostConfig.NetworkMode.IsHost() {
sboxOptions = append(sboxOptions, libnetwork.OptionUseDefaultSandbox())
} else {
// OptionUseExternalKey is mandatory for userns support.
@@ -58,27 +58,27 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
}
// Add platform-specific Sandbox options.
if err := buildSandboxPlatformOptions(container, cfg, &sboxOptions); err != nil {
if err := buildSandboxPlatformOptions(ctr, cfg, &sboxOptions); err != nil {
return nil, err
}
if len(container.HostConfig.DNS) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNS(container.HostConfig.DNS))
if len(ctr.HostConfig.DNS) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNS(ctr.HostConfig.DNS))
} else if len(cfg.DNS) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNS(ipAddresses(cfg.DNS)))
}
if len(container.HostConfig.DNSSearch) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(container.HostConfig.DNSSearch))
if len(ctr.HostConfig.DNSSearch) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(ctr.HostConfig.DNSSearch))
} else if len(cfg.DNSSearch) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(cfg.DNSSearch))
}
if len(container.HostConfig.DNSOptions) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(container.HostConfig.DNSOptions))
if len(ctr.HostConfig.DNSOptions) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(ctr.HostConfig.DNSOptions))
} else if len(cfg.DNSOptions) > 0 {
sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(cfg.DNSOptions))
}
for _, extraHost := range container.HostConfig.ExtraHosts {
for _, extraHost := range ctr.HostConfig.ExtraHosts {
// allow IPv6 addresses in extra hosts; only split on first ":"
if _, err := opts.ValidateExtraHost(extraHost); err != nil {
return nil, err
@@ -98,8 +98,8 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
}
bindings := make(nat.PortMap)
if container.HostConfig.PortBindings != nil {
for p, b := range container.HostConfig.PortBindings {
if ctr.HostConfig.PortBindings != nil {
for p, b := range ctr.HostConfig.PortBindings {
bindings[p] = []nat.PortBinding{}
for _, bb := range b {
bindings[p] = append(bindings[p], nat.PortBinding{
@@ -111,8 +111,8 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
}
// TODO(thaJeztah): Move this code to a method on nat.PortSet.
ports := make([]nat.Port, 0, len(container.Config.ExposedPorts))
for p := range container.Config.ExposedPorts {
ports := make([]nat.Port, 0, len(ctr.Config.ExposedPorts))
for p := range ctr.Config.ExposedPorts {
ports = append(ports, p)
}
nat.SortPortMap(ports, bindings)
@@ -147,7 +147,7 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
})
}
if container.HostConfig.PublishAllPorts && len(bindings[port]) == 0 {
if ctr.HostConfig.PublishAllPorts && len(bindings[port]) == 0 {
publishedPorts = append(publishedPorts, types.PortBinding{
Proto: portProto,
Port: portNum,
@@ -161,7 +161,7 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
// return if this call to build join options is not for default bridge network
// Legacy Link is only supported by docker run --link
defaultNetName := runconfig.DefaultDaemonNetworkMode().NetworkName()
bridgeSettings, ok := container.NetworkSettings.Networks[defaultNetName]
bridgeSettings, ok := ctr.NetworkSettings.Networks[defaultNetName]
if !ok || bridgeSettings.EndpointSettings == nil || bridgeSettings.EndpointID == "" {
return sboxOptions, nil
}
@@ -170,7 +170,7 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
childEndpoints []string
cEndpointID string
)
for linkAlias, child := range daemon.children(container) {
for linkAlias, child := range daemon.children(ctr) {
if !isLinkable(child) {
return nil, fmt.Errorf("Cannot link to %s, as it does not belong to the default network", child.Name)
}
@@ -195,8 +195,8 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
}
var parentEndpoints []string
for alias, parent := range daemon.parents(container) {
if cfg.DisableBridge || !container.HostConfig.NetworkMode.IsPrivate() {
for alias, parent := range daemon.parents(ctr) {
if cfg.DisableBridge || !ctr.HostConfig.NetworkMode.IsPrivate() {
continue
}
@@ -217,19 +217,19 @@ func (daemon *Daemon) buildSandboxOptions(cfg *config.Config, container *contain
return sboxOptions, nil
}
func (daemon *Daemon) updateNetworkSettings(container *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings) error {
if container.NetworkSettings == nil {
container.NetworkSettings = &network.Settings{}
func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings) error {
if ctr.NetworkSettings == nil {
ctr.NetworkSettings = &network.Settings{}
}
if container.NetworkSettings.Networks == nil {
container.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
if ctr.NetworkSettings.Networks == nil {
ctr.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
}
if !container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
if !ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
return runconfig.ErrConflictHostNetwork
}
for s, v := range container.NetworkSettings.Networks {
for s, v := range ctr.NetworkSettings.Networks {
sn, err := daemon.FindNetwork(getNetworkID(s, v.EndpointSettings))
if err != nil {
continue
@@ -256,20 +256,20 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n *l
}
}
container.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{
ctr.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{
EndpointSettings: endpointConfig,
}
return nil
}
func (daemon *Daemon) updateEndpointNetworkSettings(cfg *config.Config, container *container.Container, n *libnetwork.Network, ep *libnetwork.Endpoint) error {
if err := buildEndpointInfo(container.NetworkSettings, n, ep); err != nil {
func (daemon *Daemon) updateEndpointNetworkSettings(cfg *config.Config, ctr *container.Container, n *libnetwork.Network, ep *libnetwork.Endpoint) error {
if err := buildEndpointInfo(ctr.NetworkSettings, n, ep); err != nil {
return err
}
if container.HostConfig.NetworkMode == runconfig.DefaultDaemonNetworkMode() {
container.NetworkSettings.Bridge = cfg.BridgeConfig.Iface
if ctr.HostConfig.NetworkMode == runconfig.DefaultDaemonNetworkMode() {
ctr.NetworkSettings.Bridge = cfg.BridgeConfig.Iface
}
return nil
@@ -277,11 +277,11 @@ func (daemon *Daemon) updateEndpointNetworkSettings(cfg *config.Config, containe
// UpdateNetwork is used to update the container's network (e.g. when linked containers
// get removed/unlinked).
func (daemon *Daemon) updateNetwork(cfg *config.Config, container *container.Container) error {
func (daemon *Daemon) updateNetwork(cfg *config.Config, ctr *container.Container) error {
var (
start = time.Now()
ctrl = daemon.netController
sid = container.NetworkSettings.SandboxID
sid = ctr.NetworkSettings.SandboxID
)
sb, err := ctrl.SandboxByID(sid)
@@ -291,7 +291,7 @@ func (daemon *Daemon) updateNetwork(cfg *config.Config, container *container.Con
// Find if container is connected to the default bridge network
var n *libnetwork.Network
for name, v := range container.NetworkSettings.Networks {
for name, v := range ctr.NetworkSettings.Networks {
sn, err := daemon.FindNetwork(getNetworkID(name, v.EndpointSettings))
if err != nil {
continue
@@ -307,7 +307,7 @@ func (daemon *Daemon) updateNetwork(cfg *config.Config, container *container.Con
return nil
}
sbOptions, err := daemon.buildSandboxOptions(cfg, container)
sbOptions, err := daemon.buildSandboxOptions(cfg, ctr)
if err != nil {
return fmt.Errorf("Update network failed: %v", err)
}
@@ -321,13 +321,13 @@ func (daemon *Daemon) updateNetwork(cfg *config.Config, container *container.Con
return nil
}
func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (*libnetwork.Network, *networktypes.NetworkingConfig, error) {
func (daemon *Daemon) findAndAttachNetwork(ctr *container.Container, idOrName string, epConfig *networktypes.EndpointSettings) (*libnetwork.Network, *networktypes.NetworkingConfig, error) {
id := getNetworkID(idOrName, epConfig)
n, err := daemon.FindNetwork(id)
if err != nil {
// We should always be able to find the network for a managed container.
if container.Managed {
if ctr.Managed {
return nil, nil, err
}
}
@@ -335,14 +335,14 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN
// If we found a network and if it is not dynamically created
// we should never attempt to attach to that network here.
if n != nil {
if container.Managed || !n.Dynamic() {
if ctr.Managed || !n.Dynamic() {
return n, nil, nil
}
// Throw an error if the container is already attached to the network
if container.NetworkSettings.Networks != nil {
if ctr.NetworkSettings.Networks != nil {
networkName := n.Name()
containerName := strings.TrimPrefix(container.Name, "/")
if nw, ok := container.NetworkSettings.Networks[networkName]; ok && nw.EndpointID != "" {
containerName := strings.TrimPrefix(ctr.Name, "/")
if nw, ok := ctr.NetworkSettings.Networks[networkName]; ok && nw.EndpointID != "" {
err := fmt.Errorf("%s is already attached to network %s", containerName, networkName)
return n, nil, errdefs.Conflict(err)
}
@@ -371,7 +371,7 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN
// trigger attachment in the swarm cluster manager.
if daemon.clusterProvider != nil {
var err error
nwCfg, err = daemon.clusterProvider.AttachNetwork(id, container.ID, addresses)
nwCfg, err = daemon.clusterProvider.AttachNetwork(id, ctr.ID, addresses)
if err != nil {
return nil, nil, err
}
@@ -380,8 +380,8 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN
n, err = daemon.FindNetwork(id)
if err != nil {
if daemon.clusterProvider != nil {
if err := daemon.clusterProvider.DetachNetwork(id, container.ID); err != nil {
log.G(context.TODO()).Warnf("Could not rollback attachment for container %s to network %s: %v", container.ID, idOrName, err)
if err := daemon.clusterProvider.DetachNetwork(id, ctr.ID); err != nil {
log.G(context.TODO()).Warnf("Could not rollback attachment for container %s to network %s: %v", ctr.ID, idOrName, err)
}
}
@@ -410,16 +410,16 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN
// This container has attachment to a swarm scope
// network. Update the container network settings accordingly.
container.NetworkSettings.HasSwarmEndpoint = true
ctr.NetworkSettings.HasSwarmEndpoint = true
return n, nwCfg, nil
}
// updateContainerNetworkSettings updates the network settings
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container, endpointsConfig map[string]*networktypes.EndpointSettings) {
func (daemon *Daemon) updateContainerNetworkSettings(ctr *container.Container, endpointsConfig map[string]*networktypes.EndpointSettings) {
var n *libnetwork.Network
mode := container.HostConfig.NetworkMode
if container.Config.NetworkDisabled || mode.IsContainer() {
mode := ctr.HostConfig.NetworkMode
if ctr.Config.NetworkDisabled || mode.IsContainer() {
return
}
@@ -434,17 +434,17 @@ func (daemon *Daemon) updateContainerNetworkSettings(container *container.Contai
}
}
if container.NetworkSettings == nil {
container.NetworkSettings = &network.Settings{}
if ctr.NetworkSettings == nil {
ctr.NetworkSettings = &network.Settings{}
}
if len(endpointsConfig) > 0 {
if container.NetworkSettings.Networks == nil {
container.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
if ctr.NetworkSettings.Networks == nil {
ctr.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
}
for name, epConfig := range endpointsConfig {
container.NetworkSettings.Networks[name] = &network.EndpointSettings{
ctr.NetworkSettings.Networks[name] = &network.EndpointSettings{
EndpointSettings: epConfig,
// At this point, during container creation, epConfig.MacAddress is the
// configured value from the API. If there is no configured value, the
@@ -455,9 +455,9 @@ func (daemon *Daemon) updateContainerNetworkSettings(container *container.Contai
}
}
if container.NetworkSettings.Networks == nil {
container.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
container.NetworkSettings.Networks[networkName] = &network.EndpointSettings{
if ctr.NetworkSettings.Networks == nil {
ctr.NetworkSettings.Networks = make(map[string]*network.EndpointSettings)
ctr.NetworkSettings.Networks[networkName] = &network.EndpointSettings{
EndpointSettings: &networktypes.EndpointSettings{},
}
}
@@ -466,20 +466,20 @@ func (daemon *Daemon) updateContainerNetworkSettings(container *container.Contai
return
}
// Make sure to internally store the per network endpoint config by network name
if _, ok := container.NetworkSettings.Networks[networkName]; ok {
if _, ok := ctr.NetworkSettings.Networks[networkName]; ok {
return
}
if n != nil {
if nwConfig, ok := container.NetworkSettings.Networks[n.ID()]; ok {
container.NetworkSettings.Networks[networkName] = nwConfig
delete(container.NetworkSettings.Networks, n.ID())
if nwConfig, ok := ctr.NetworkSettings.Networks[n.ID()]; ok {
ctr.NetworkSettings.Networks[networkName] = nwConfig
delete(ctr.NetworkSettings.Networks, n.ID())
return
}
}
}
func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, container *container.Container) (retErr error) {
func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container) (retErr error) {
if daemon.netController == nil {
return nil
}
@@ -487,17 +487,17 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
start := time.Now()
// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
if err := daemon.netController.SandboxDestroy(ctx, container.ID); err != nil {
log.G(ctx).WithError(err).Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
if err := daemon.netController.SandboxDestroy(ctx, ctr.ID); err != nil {
log.G(ctx).WithError(err).Errorf("failed to cleanup up stale network sandbox for container %s", ctr.ID)
}
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
if ctr.Config.NetworkDisabled || ctr.HostConfig.NetworkMode.IsContainer() {
return nil
}
updateSettings := false
if len(container.NetworkSettings.Networks) == 0 {
daemon.updateContainerNetworkSettings(container, nil)
if len(ctr.NetworkSettings.Networks) == 0 {
daemon.updateContainerNetworkSettings(ctr, nil)
updateSettings = true
}
@@ -506,16 +506,16 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
// on sandbox initialize for link, but the sandbox only be initialized
// on first network connecting.
defaultNetName := runconfig.DefaultDaemonNetworkMode().NetworkName()
if nConf, ok := container.NetworkSettings.Networks[defaultNetName]; ok {
if nConf, ok := ctr.NetworkSettings.Networks[defaultNetName]; ok {
cleanOperationalData(nConf)
if err := daemon.connectToNetwork(ctx, cfg, container, defaultNetName, nConf, updateSettings); err != nil {
if err := daemon.connectToNetwork(ctx, cfg, ctr, defaultNetName, nConf, updateSettings); err != nil {
return err
}
}
// the intermediate map is necessary because "connectToNetwork" modifies "container.NetworkSettings.Networks"
networks := make(map[string]*network.EndpointSettings)
for n, epConf := range container.NetworkSettings.Networks {
for n, epConf := range ctr.NetworkSettings.Networks {
if n == defaultNetName {
continue
}
@@ -525,7 +525,7 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
for netName, epConf := range networks {
cleanOperationalData(epConf)
if err := daemon.connectToNetwork(ctx, cfg, container, netName, epConf, updateSettings); err != nil {
if err := daemon.connectToNetwork(ctx, cfg, ctr, netName, epConf, updateSettings); err != nil {
return err
}
}
@@ -533,20 +533,20 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
// If the container is not to be connected to any network,
// create its network sandbox now if not present
if len(networks) == 0 {
if _, err := daemon.netController.GetSandbox(container.ID); err != nil {
if _, err := daemon.netController.GetSandbox(ctr.ID); err != nil {
if !errdefs.IsNotFound(err) {
return err
}
sbOptions, err := daemon.buildSandboxOptions(cfg, container)
sbOptions, err := daemon.buildSandboxOptions(cfg, ctr)
if err != nil {
return err
}
sb, err := daemon.netController.NewSandbox(ctx, container.ID, sbOptions...)
sb, err := daemon.netController.NewSandbox(ctx, ctr.ID, sbOptions...)
if err != nil {
return err
}
setNetworkSandbox(container, sb)
setNetworkSandbox(ctr, sb)
defer func() {
if retErr != nil {
sb.Delete(context.WithoutCancel(ctx))
@@ -555,7 +555,7 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
}
}
if _, err := container.WriteHostConfig(); err != nil {
if _, err := ctr.WriteHostConfig(); err != nil {
return err
}
networkActions.WithValues("allocate").UpdateSince(start)
@@ -655,12 +655,12 @@ func cleanOperationalData(es *network.EndpointSettings) {
}
}
func (daemon *Daemon) updateNetworkConfig(container *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings, updateSettings bool) error {
func (daemon *Daemon) updateNetworkConfig(ctr *container.Container, n *libnetwork.Network, endpointConfig *networktypes.EndpointSettings, updateSettings bool) error {
// Set up DNS names for a user defined network, and for the default 'nat'
// network on Windows (IsBridge() returns true for nat).
if containertypes.NetworkMode(n.Name()).IsUserDefined() ||
(serviceDiscoveryOnDefaultNetwork() && containertypes.NetworkMode(n.Name()).IsBridge()) {
endpointConfig.DNSNames = buildEndpointDNSNames(container, endpointConfig.Aliases)
endpointConfig.DNSNames = buildEndpointDNSNames(ctr, endpointConfig.Aliases)
}
if err := validateEndpointSettings(n, n.Name(), endpointConfig); err != nil {
@@ -668,7 +668,7 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, n *lib
}
if updateSettings {
if err := daemon.updateNetworkSettings(container, n, endpointConfig); err != nil {
if err := daemon.updateNetworkSettings(ctr, n, endpointConfig); err != nil {
return err
}
}
@@ -698,21 +698,21 @@ func buildEndpointDNSNames(ctr *container.Container, aliases []string) []string
return sliceutil.Dedup(dnsNames)
}
func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config, container *container.Container, idOrName string, endpointConfig *network.EndpointSettings, updateSettings bool) (retErr error) {
containerName := strings.TrimPrefix(container.Name, "/")
func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config, ctr *container.Container, idOrName string, endpointConfig *network.EndpointSettings, updateSettings bool) (retErr error) {
containerName := strings.TrimPrefix(ctr.Name, "/")
ctx, span := otel.Tracer("").Start(ctx, "daemon.connectToNetwork", trace.WithAttributes(
attribute.String("container.ID", container.ID),
attribute.String("container.ID", ctr.ID),
attribute.String("container.name", containerName),
attribute.String("network.idOrName", idOrName)))
defer span.End()
start := time.Now()
if container.HostConfig.NetworkMode.IsContainer() {
if ctr.HostConfig.NetworkMode.IsContainer() {
return runconfig.ErrConflictSharedNetwork
}
if cfg.DisableBridge && containertypes.NetworkMode(idOrName).IsBridge() {
container.Config.NetworkDisabled = true
ctr.Config.NetworkDisabled = true
return nil
}
if endpointConfig == nil {
@@ -721,7 +721,7 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
}
}
n, nwCfg, err := daemon.findAndAttachNetwork(container, idOrName, endpointConfig.EndpointSettings)
n, nwCfg, err := daemon.findAndAttachNetwork(ctr, idOrName, endpointConfig.EndpointSettings)
if err != nil {
return err
}
@@ -730,8 +730,8 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
}
nwName := n.Name()
if idOrName != container.HostConfig.NetworkMode.NetworkName() {
if err := daemon.normalizeNetMode(container); err != nil {
if idOrName != ctr.HostConfig.NetworkMode.NetworkName() {
if err := daemon.normalizeNetMode(ctr); err != nil {
return err
}
}
@@ -749,13 +749,13 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
}
}
if err := daemon.updateNetworkConfig(container, n, endpointConfig.EndpointSettings, updateSettings); err != nil {
if err := daemon.updateNetworkConfig(ctr, n, endpointConfig.EndpointSettings, updateSettings); err != nil {
return err
}
// TODO(thaJeztah): should this fail early if no sandbox was found?
sb, _ := daemon.netController.GetSandbox(container.ID)
createOptions, err := buildCreateEndpointOptions(container, n, endpointConfig, sb, ipAddresses(cfg.DNS))
sb, _ := daemon.netController.GetSandbox(ctr.ID)
createOptions, err := buildCreateEndpointOptions(ctr, n, endpointConfig, sb, ipAddresses(cfg.DNS))
if err != nil {
return err
}
@@ -771,28 +771,28 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
}
}
}()
container.NetworkSettings.Networks[nwName] = endpointConfig
ctr.NetworkSettings.Networks[nwName] = endpointConfig
delete(container.NetworkSettings.Networks, n.ID())
delete(ctr.NetworkSettings.Networks, n.ID())
if err := daemon.updateEndpointNetworkSettings(cfg, container, n, ep); err != nil {
if err := daemon.updateEndpointNetworkSettings(cfg, ctr, n, ep); err != nil {
return err
}
if sb == nil {
sbOptions, err := daemon.buildSandboxOptions(cfg, container)
sbOptions, err := daemon.buildSandboxOptions(cfg, ctr)
if err != nil {
return err
}
sb, err = daemon.netController.NewSandbox(ctx, container.ID, sbOptions...)
sb, err = daemon.netController.NewSandbox(ctx, ctr.ID, sbOptions...)
if err != nil {
return err
}
setNetworkSandbox(container, sb)
setNetworkSandbox(ctr, sb)
}
joinOptions, err := buildJoinOptions(container.NetworkSettings, n)
joinOptions, err := buildJoinOptions(ctr.NetworkSettings, n)
if err != nil {
return err
}
@@ -801,20 +801,20 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
return err
}
if !container.Managed {
if !ctr.Managed {
// add container name/alias to DNS
if err := daemon.ActivateContainerServiceBinding(container.Name); err != nil {
return fmt.Errorf("Activate container service binding for %s failed: %v", container.Name, err)
if err := daemon.ActivateContainerServiceBinding(ctr.Name); err != nil {
return fmt.Errorf("Activate container service binding for %s failed: %v", ctr.Name, err)
}
}
if err := updateJoinInfo(container.NetworkSettings, n, ep); err != nil {
if err := updateJoinInfo(ctr.NetworkSettings, n, ep); err != nil {
return fmt.Errorf("Updating join info failed: %v", err)
}
container.NetworkSettings.Ports = getPortMapInfo(sb)
ctr.NetworkSettings.Ports = getPortMapInfo(sb)
daemon.LogNetworkEventWithAttributes(n, events.ActionConnect, map[string]string{"container": container.ID})
daemon.LogNetworkEventWithAttributes(n, events.ActionConnect, map[string]string{"container": ctr.ID})
networkActions.WithValues("connect").UpdateSince(start)
return nil
}
@@ -864,7 +864,7 @@ func (daemon *Daemon) ForceEndpointDelete(name string, networkName string) error
return ep.Delete(context.TODO(), true)
}
func (daemon *Daemon) disconnectFromNetwork(ctx context.Context, container *container.Container, n *libnetwork.Network, force bool) error {
func (daemon *Daemon) disconnectFromNetwork(ctx context.Context, ctr *container.Container, n *libnetwork.Network, force bool) error {
var (
ep *libnetwork.Endpoint
sbox *libnetwork.Sandbox
@@ -875,7 +875,7 @@ func (daemon *Daemon) disconnectFromNetwork(ctx context.Context, container *cont
return false
}
if sb := epInfo.Sandbox(); sb != nil {
if sb.ContainerID() == container.ID {
if sb.ContainerID() == ctr.ID {
ep = current
sbox = sb
return true
@@ -887,96 +887,96 @@ func (daemon *Daemon) disconnectFromNetwork(ctx context.Context, container *cont
if ep == nil {
if force {
var err error
ep, err = n.EndpointByName(strings.TrimPrefix(container.Name, "/"))
ep, err = n.EndpointByName(strings.TrimPrefix(ctr.Name, "/"))
if err != nil {
return err
}
return ep.Delete(ctx, force)
}
return fmt.Errorf("container %s is not connected to network %s", container.ID, n.Name())
return fmt.Errorf("container %s is not connected to network %s", ctr.ID, n.Name())
}
if err := ep.Leave(ctx, sbox); err != nil {
return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err)
return fmt.Errorf("container %s failed to leave network %s: %v", ctr.ID, n.Name(), err)
}
container.NetworkSettings.Ports = getPortMapInfo(sbox)
ctr.NetworkSettings.Ports = getPortMapInfo(sbox)
if err := ep.Delete(ctx, false); err != nil {
return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err)
return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", ctr.ID, n.Name(), err)
}
delete(container.NetworkSettings.Networks, n.Name())
delete(ctr.NetworkSettings.Networks, n.Name())
daemon.tryDetachContainerFromClusterNetwork(n, container)
daemon.tryDetachContainerFromClusterNetwork(n, ctr)
return nil
}
func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network *libnetwork.Network, container *container.Container) {
if !container.Managed && daemon.clusterProvider != nil && network.Dynamic() {
if err := daemon.clusterProvider.DetachNetwork(network.Name(), container.ID); err != nil {
func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network *libnetwork.Network, ctr *container.Container) {
if !ctr.Managed && daemon.clusterProvider != nil && network.Dynamic() {
if err := daemon.clusterProvider.DetachNetwork(network.Name(), ctr.ID); err != nil {
log.G(context.TODO()).WithError(err).Warn("error detaching from network")
if err := daemon.clusterProvider.DetachNetwork(network.ID(), container.ID); err != nil {
if err := daemon.clusterProvider.DetachNetwork(network.ID(), ctr.ID); err != nil {
log.G(context.TODO()).WithError(err).Warn("error detaching from network")
}
}
}
daemon.LogNetworkEventWithAttributes(network, events.ActionDisconnect, map[string]string{
"container": container.ID,
"container": ctr.ID,
})
}
// normalizeNetMode checks whether the network mode references a network by a partial ID. In that case, it replaces the
// partial ID with the full network ID.
// TODO(aker): transform ID into name when the referenced network is one of the predefined.
func (daemon *Daemon) normalizeNetMode(container *container.Container) error {
if container.HostConfig.NetworkMode.IsUserDefined() {
netMode := container.HostConfig.NetworkMode.NetworkName()
func (daemon *Daemon) normalizeNetMode(ctr *container.Container) error {
if ctr.HostConfig.NetworkMode.IsUserDefined() {
netMode := ctr.HostConfig.NetworkMode.NetworkName()
nw, err := daemon.FindNetwork(netMode)
if err != nil {
return fmt.Errorf("could not find a network matching network mode %s: %w", netMode, err)
}
if netMode != nw.ID() && netMode != nw.Name() {
container.HostConfig.NetworkMode = containertypes.NetworkMode(nw.ID())
ctr.HostConfig.NetworkMode = containertypes.NetworkMode(nw.ID())
}
}
return nil
}
func (daemon *Daemon) initializeNetworking(ctx context.Context, cfg *config.Config, container *container.Container) error {
if container.HostConfig.NetworkMode.IsContainer() {
func (daemon *Daemon) initializeNetworking(ctx context.Context, cfg *config.Config, ctr *container.Container) error {
if ctr.HostConfig.NetworkMode.IsContainer() {
// we need to get the hosts files from the container to join
nc, err := daemon.getNetworkedContainer(container.ID, container.HostConfig.NetworkMode.ConnectedContainer())
nc, err := daemon.getNetworkedContainer(ctr.ID, ctr.HostConfig.NetworkMode.ConnectedContainer())
if err != nil {
return err
}
err = daemon.initializeNetworkingPaths(container, nc)
err = daemon.initializeNetworkingPaths(ctr, nc)
if err != nil {
return err
}
container.Config.Hostname = nc.Config.Hostname
container.Config.Domainname = nc.Config.Domainname
ctr.Config.Hostname = nc.Config.Hostname
ctr.Config.Domainname = nc.Config.Domainname
return nil
}
if container.HostConfig.NetworkMode.IsHost() && container.Config.Hostname == "" {
if ctr.HostConfig.NetworkMode.IsHost() && ctr.Config.Hostname == "" {
hn, err := os.Hostname()
if err != nil {
return err
}
container.Config.Hostname = hn
ctr.Config.Hostname = hn
}
if err := daemon.allocateNetwork(ctx, cfg, container); err != nil {
if err := daemon.allocateNetwork(ctx, cfg, ctr); err != nil {
return err
}
return container.BuildHostnameFile()
return ctr.BuildHostnameFile()
}
func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*container.Container, error) {
@@ -996,7 +996,7 @@ func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID st
return nc, nil
}
func (daemon *Daemon) releaseNetwork(ctx context.Context, container *container.Container) {
func (daemon *Daemon) releaseNetwork(ctx context.Context, ctr *container.Container) {
ctx = context.WithoutCancel(ctx)
start := time.Now()
@@ -1006,24 +1006,24 @@ func (daemon *Daemon) releaseNetwork(ctx context.Context, container *container.C
return
}
// If the container uses the network namespace of another container, it doesn't own it -- nothing to do here.
if container.HostConfig.NetworkMode.IsContainer() {
if ctr.HostConfig.NetworkMode.IsContainer() {
return
}
if container.NetworkSettings == nil {
if ctr.NetworkSettings == nil {
return
}
container.NetworkSettings.Ports = nil
sid := container.NetworkSettings.SandboxID
ctr.NetworkSettings.Ports = nil
sid := ctr.NetworkSettings.SandboxID
if sid == "" {
return
}
container.NetworkSettings.SandboxID = ""
container.NetworkSettings.SandboxKey = ""
ctr.NetworkSettings.SandboxID = ""
ctr.NetworkSettings.SandboxKey = ""
var networks []*libnetwork.Network
for n, epSettings := range container.NetworkSettings.Networks {
for n, epSettings := range ctr.NetworkSettings.Networks {
if nw, err := daemon.FindNetwork(getNetworkID(n, epSettings.EndpointSettings)); err == nil {
networks = append(networks, nw)
}
@@ -1042,11 +1042,11 @@ func (daemon *Daemon) releaseNetwork(ctx context.Context, container *container.C
}
if err := sb.Delete(ctx); err != nil {
log.G(ctx).Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
log.G(ctx).Errorf("Error deleting sandbox id %s for container %s: %v", sid, ctr.ID, err)
}
for _, nw := range networks {
daemon.tryDetachContainerFromClusterNetwork(nw, container)
daemon.tryDetachContainerFromClusterNetwork(nw, ctr)
}
networkActions.WithValues("release").UpdateSince(start)
}
@@ -1056,25 +1056,25 @@ func errRemovalContainer(containerID string) error {
}
// ConnectToNetwork connects a container to a network
func (daemon *Daemon) ConnectToNetwork(ctx context.Context, container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
func (daemon *Daemon) ConnectToNetwork(ctx context.Context, ctr *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
if endpointConfig == nil {
endpointConfig = &networktypes.EndpointSettings{}
}
container.Lock()
defer container.Unlock()
ctr.Lock()
defer ctr.Unlock()
if !container.Running {
if container.RemovalInProgress || container.Dead {
return errRemovalContainer(container.ID)
if !ctr.Running {
if ctr.RemovalInProgress || ctr.Dead {
return errRemovalContainer(ctr.ID)
}
n, err := daemon.FindNetwork(idOrName)
if err == nil && n != nil {
if err := daemon.updateNetworkConfig(container, n, endpointConfig, true); err != nil {
if err := daemon.updateNetworkConfig(ctr, n, endpointConfig, true); err != nil {
return err
}
} else {
container.NetworkSettings.Networks[idOrName] = &network.EndpointSettings{
ctr.NetworkSettings.Networks[idOrName] = &network.EndpointSettings{
EndpointSettings: endpointConfig,
}
}
@@ -1082,52 +1082,52 @@ func (daemon *Daemon) ConnectToNetwork(ctx context.Context, container *container
epc := &network.EndpointSettings{
EndpointSettings: endpointConfig,
}
if err := daemon.connectToNetwork(ctx, &daemon.config().Config, container, idOrName, epc, true); err != nil {
if err := daemon.connectToNetwork(ctx, &daemon.config().Config, ctr, idOrName, epc, true); err != nil {
return err
}
}
return container.CheckpointTo(ctx, daemon.containersReplica)
return ctr.CheckpointTo(ctx, daemon.containersReplica)
}
// DisconnectFromNetwork disconnects container from network n.
func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, container *container.Container, networkName string, force bool) error {
func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, ctr *container.Container, networkName string, force bool) error {
n, err := daemon.FindNetwork(networkName)
container.Lock()
defer container.Unlock()
ctr.Lock()
defer ctr.Unlock()
if !container.Running || (err != nil && force) {
if container.RemovalInProgress || container.Dead {
return errRemovalContainer(container.ID)
if !ctr.Running || (err != nil && force) {
if ctr.RemovalInProgress || ctr.Dead {
return errRemovalContainer(ctr.ID)
}
// In case networkName is resolved we will use n.Name()
// this will cover the case where network id is passed.
if n != nil {
networkName = n.Name()
}
if _, ok := container.NetworkSettings.Networks[networkName]; !ok {
return fmt.Errorf("container %s is not connected to the network %s", container.ID, networkName)
if _, ok := ctr.NetworkSettings.Networks[networkName]; !ok {
return fmt.Errorf("container %s is not connected to the network %s", ctr.ID, networkName)
}
delete(container.NetworkSettings.Networks, networkName)
delete(ctr.NetworkSettings.Networks, networkName)
} else if err == nil {
if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
if ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
return runconfig.ErrConflictHostNetwork
}
if err := daemon.disconnectFromNetwork(ctx, container, n, false); err != nil {
if err := daemon.disconnectFromNetwork(ctx, ctr, n, false); err != nil {
return err
}
} else {
return err
}
if err := container.CheckpointTo(ctx, daemon.containersReplica); err != nil {
if err := ctr.CheckpointTo(ctx, daemon.containersReplica); err != nil {
return err
}
if n != nil {
daemon.LogNetworkEventWithAttributes(n, events.ActionDisconnect, map[string]string{
"container": container.ID,
"container": ctr.ID,
})
}
@@ -1172,7 +1172,7 @@ func getNetworkID(name string, endpointSettings *networktypes.EndpointSettings)
}
// setNetworkSandbox updates the sandbox ID and Key.
func setNetworkSandbox(c *container.Container, sb *libnetwork.Sandbox) {
c.NetworkSettings.SandboxID = sb.ID()
c.NetworkSettings.SandboxKey = sb.Key()
func setNetworkSandbox(ctr *container.Container, sb *libnetwork.Sandbox) {
ctr.NetworkSettings.SandboxID = sb.ID()
ctr.NetworkSettings.SandboxKey = sb.Key()
}

View File

@@ -26,11 +26,11 @@ import (
"golang.org/x/sys/unix"
)
func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
func (daemon *Daemon) setupLinkedContainers(ctr *container.Container) ([]string, error) {
var env []string
children := daemon.children(container)
children := daemon.children(ctr)
bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
bridgeSettings := ctr.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
if bridgeSettings == nil || bridgeSettings.EndpointSettings == nil {
return nil, nil
}
@@ -100,36 +100,36 @@ func (daemon *Daemon) getPIDContainer(id string) (*container.Container, error) {
}
// setupContainerDirs sets up base container directories (root, ipc, tmpfs and secrets).
func (daemon *Daemon) setupContainerDirs(c *container.Container) (_ []container.Mount, err error) {
if err := daemon.setupContainerMountsRoot(c); err != nil {
func (daemon *Daemon) setupContainerDirs(ctr *container.Container) (_ []container.Mount, err error) {
if err := daemon.setupContainerMountsRoot(ctr); err != nil {
return nil, err
}
if err := daemon.setupIPCDirs(c); err != nil {
if err := daemon.setupIPCDirs(ctr); err != nil {
return nil, err
}
if err := daemon.setupSecretDir(c); err != nil {
if err := daemon.setupSecretDir(ctr); err != nil {
return nil, err
}
defer func() {
if err != nil {
daemon.cleanupSecretDir(c)
daemon.cleanupSecretDir(ctr)
}
}()
var ms []container.Mount
if !c.HostConfig.IpcMode.IsPrivate() && !c.HostConfig.IpcMode.IsEmpty() {
ms = append(ms, c.IpcMounts()...)
if !ctr.HostConfig.IpcMode.IsPrivate() && !ctr.HostConfig.IpcMode.IsEmpty() {
ms = append(ms, ctr.IpcMounts()...)
}
tmpfsMounts, err := c.TmpfsMounts()
tmpfsMounts, err := ctr.TmpfsMounts()
if err != nil {
return nil, err
}
ms = append(ms, tmpfsMounts...)
secretMounts, err := c.SecretMounts()
secretMounts, err := ctr.SecretMounts()
if err != nil {
return nil, err
}
@@ -138,8 +138,8 @@ func (daemon *Daemon) setupContainerDirs(c *container.Container) (_ []container.
return ms, nil
}
func (daemon *Daemon) setupIPCDirs(c *container.Container) error {
ipcMode := c.HostConfig.IpcMode
func (daemon *Daemon) setupIPCDirs(ctr *container.Container) error {
ipcMode := ctr.HostConfig.IpcMode
switch {
case ipcMode.IsContainer():
@@ -147,18 +147,18 @@ func (daemon *Daemon) setupIPCDirs(c *container.Container) error {
if err != nil {
return errors.Wrapf(err, "failed to join IPC namespace")
}
c.ShmPath = ic.ShmPath
ctr.ShmPath = ic.ShmPath
case ipcMode.IsHost():
if _, err := os.Stat("/dev/shm"); err != nil {
return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
}
c.ShmPath = "/dev/shm"
ctr.ShmPath = "/dev/shm"
case ipcMode.IsPrivate(), ipcMode.IsNone():
// c.ShmPath will/should not be used, so make it empty.
// Container's /dev/shm mount comes from OCI spec.
c.ShmPath = ""
ctr.ShmPath = ""
case ipcMode.IsEmpty():
// A container was created by an older version of the daemon.
@@ -167,8 +167,8 @@ func (daemon *Daemon) setupIPCDirs(c *container.Container) error {
case ipcMode.IsShareable():
rootIDs := daemon.idMapping.RootPair()
if !c.HasMountFor("/dev/shm") {
shmPath, err := c.ShmResourcePath()
if !ctr.HasMountFor("/dev/shm") {
shmPath, err := ctr.ShmResourcePath()
if err != nil {
return err
}
@@ -177,14 +177,14 @@ func (daemon *Daemon) setupIPCDirs(c *container.Container) error {
return err
}
shmproperty := "mode=1777,size=" + strconv.FormatInt(c.HostConfig.ShmSize, 10)
if err := unix.Mount("shm", shmPath, "tmpfs", uintptr(unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
shmproperty := "mode=1777,size=" + strconv.FormatInt(ctr.HostConfig.ShmSize, 10)
if err := unix.Mount("shm", shmPath, "tmpfs", uintptr(unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV), label.FormatMountLabel(shmproperty, ctr.GetMountLabel())); err != nil {
return fmt.Errorf("mounting shm tmpfs: %s", err)
}
if err := os.Chown(shmPath, rootIDs.UID, rootIDs.GID); err != nil {
return err
}
c.ShmPath = shmPath
ctr.ShmPath = shmPath
}
default:
@@ -194,28 +194,28 @@ func (daemon *Daemon) setupIPCDirs(c *container.Container) error {
return nil
}
func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
if len(c.SecretReferences) == 0 && len(c.ConfigReferences) == 0 {
func (daemon *Daemon) setupSecretDir(ctr *container.Container) (setupErr error) {
if len(ctr.SecretReferences) == 0 && len(ctr.ConfigReferences) == 0 {
return nil
}
if err := daemon.createSecretsDir(c); err != nil {
if err := daemon.createSecretsDir(ctr); err != nil {
return err
}
defer func() {
if setupErr != nil {
daemon.cleanupSecretDir(c)
daemon.cleanupSecretDir(ctr)
}
}()
if c.DependencyStore == nil {
if ctr.DependencyStore == nil {
return fmt.Errorf("secret store is not initialized")
}
// retrieve possible remapped range start for root UID, GID
rootIDs := daemon.idMapping.RootPair()
for _, s := range c.SecretReferences {
for _, s := range ctr.SecretReferences {
// TODO (ehazlett): use type switch when more are supported
if s.File == nil {
log.G(context.TODO()).Error("secret target type is not a file target")
@@ -224,7 +224,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
// secrets are created in the SecretMountPath on the host, at a
// single level
fPath, err := c.SecretFilePath(*s)
fPath, err := ctr.SecretFilePath(*s)
if err != nil {
return errors.Wrap(err, "error getting secret file path")
}
@@ -236,7 +236,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
"name": s.File.Name,
"path": fPath,
}).Debug("injecting secret")
secret, err := c.DependencyStore.Secrets().Get(s.SecretID)
secret, err := ctr.DependencyStore.Secrets().Get(s.SecretID)
if err != nil {
return errors.Wrap(err, "unable to get secret from secret store")
}
@@ -261,7 +261,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
}
}
for _, configRef := range c.ConfigReferences {
for _, configRef := range ctr.ConfigReferences {
// TODO (ehazlett): use type switch when more are supported
if configRef.File == nil {
// Runtime configs are not mounted into the container, but they're
@@ -275,7 +275,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
continue
}
fPath, err := c.ConfigFilePath(*configRef)
fPath, err := ctr.ConfigFilePath(*configRef)
if err != nil {
return errors.Wrap(err, "error getting config file path for container")
}
@@ -287,7 +287,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
"name": configRef.File.Name,
"path": fPath,
}).Debug("injecting config")
config, err := c.DependencyStore.Configs().Get(configRef.ConfigID)
config, err := ctr.DependencyStore.Configs().Get(configRef.ConfigID)
if err != nil {
return errors.Wrap(err, "unable to get config from config store")
}
@@ -312,15 +312,15 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
}
}
return daemon.remountSecretDir(c)
return daemon.remountSecretDir(ctr)
}
// createSecretsDir is used to create a dir suitable for storing container secrets.
// In practice this is using a tmpfs mount and is used for both "configs" and "secrets"
func (daemon *Daemon) createSecretsDir(c *container.Container) error {
func (daemon *Daemon) createSecretsDir(ctr *container.Container) error {
// retrieve possible remapped range start for root UID, GID
rootIDs := daemon.idMapping.RootPair()
dir, err := c.SecretMountPath()
dir, err := ctr.SecretMountPath()
if err != nil {
return errors.Wrap(err, "error getting container secrets dir")
}
@@ -337,12 +337,12 @@ func (daemon *Daemon) createSecretsDir(c *container.Container) error {
return nil
}
func (daemon *Daemon) remountSecretDir(c *container.Container) error {
dir, err := c.SecretMountPath()
func (daemon *Daemon) remountSecretDir(ctr *container.Container) error {
dir, err := ctr.SecretMountPath()
if err != nil {
return errors.Wrap(err, "error getting container secrets path")
}
if err := label.Relabel(dir, c.MountLabel, false); err != nil {
if err := label.Relabel(dir, ctr.MountLabel, false); err != nil {
log.G(context.TODO()).WithError(err).WithField("dir", dir).Warn("Error while attempting to set selinux label")
}
rootIDs := daemon.idMapping.RootPair()
@@ -356,10 +356,10 @@ func (daemon *Daemon) remountSecretDir(c *container.Container) error {
return nil
}
func (daemon *Daemon) cleanupSecretDir(c *container.Container) {
dir, err := c.SecretMountPath()
func (daemon *Daemon) cleanupSecretDir(ctr *container.Container) {
dir, err := ctr.SecretMountPath()
if err != nil {
log.G(context.TODO()).WithError(err).WithField("container", c.ID).Warn("error getting secrets mount path for container")
log.G(context.TODO()).WithError(err).WithField("container", ctr.ID).Warn("error getting secrets mount path for container")
}
if err := mount.RecursiveUnmount(dir); err != nil {
log.G(context.TODO()).WithField("dir", dir).WithError(err).Warn("Error while attempting to unmount dir, this may prevent removal of container.")
@@ -369,8 +369,8 @@ func (daemon *Daemon) cleanupSecretDir(c *container.Container) {
}
}
func killProcessDirectly(container *container.Container) error {
pid := container.GetPID()
func killProcessDirectly(ctr *container.Container) error {
pid := ctr.GetPID()
if pid == 0 {
// Ensure that we don't kill ourselves
return nil
@@ -381,7 +381,7 @@ func killProcessDirectly(container *container.Container) error {
return errdefs.System(err)
}
err = errNoSuchProcess{pid, syscall.SIGKILL}
log.G(context.TODO()).WithError(err).WithField("container", container.ID).Debug("no such process")
log.G(context.TODO()).WithError(err).WithField("container", ctr.ID).Debug("no such process")
return err
}
@@ -390,11 +390,11 @@ func killProcessDirectly(container *container.Container) error {
// Since we can not kill a zombie pid, add zombie check here
isZombie, err := process.Zombie(pid)
if err != nil {
log.G(context.TODO()).WithError(err).WithField("container", container.ID).Warn("Container state is invalid")
log.G(context.TODO()).WithError(err).WithField("container", ctr.ID).Warn("Container state is invalid")
return err
}
if isZombie {
return errdefs.System(errors.Errorf("container %s PID %d is zombie and can not be killed. Use the --init option when creating containers to run an init inside the container that forwards signals and reaps processes", stringid.TruncateID(container.ID), pid))
return errdefs.System(errors.Errorf("container %s PID %d is zombie and can not be killed. Use the --init option when creating containers to run an init inside the container that forwards signals and reaps processes", stringid.TruncateID(ctr.ID), pid))
}
}
return nil
@@ -417,7 +417,7 @@ func serviceDiscoveryOnDefaultNetwork() bool {
return false
}
func buildSandboxPlatformOptions(container *container.Container, cfg *config.Config, sboxOptions *[]libnetwork.SandboxOption) error {
func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config, sboxOptions *[]libnetwork.SandboxOption) error {
var err error
// In host-mode networking, the container does not have its own networking
@@ -426,7 +426,7 @@ func buildSandboxPlatformOptions(container *container.Container, cfg *config.Con
// the host filesystem.
// Note that containers with "container" networking have been handled in
// "initializeNetworking()", so do not have to be accounted for here.
if container.HostConfig.NetworkMode.IsHost() {
if ctr.HostConfig.NetworkMode.IsHost() {
*sboxOptions = append(
*sboxOptions,
libnetwork.OptionOriginHostsPath("/etc/hosts"),
@@ -441,31 +441,31 @@ func buildSandboxPlatformOptions(container *container.Container, cfg *config.Con
}
*sboxOptions = append(*sboxOptions, libnetwork.OptionOriginResolvConfPath(originResolvConfPath))
container.HostsPath, err = container.GetRootResourcePath("hosts")
ctr.HostsPath, err = ctr.GetRootResourcePath("hosts")
if err != nil {
return err
}
*sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
*sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(ctr.HostsPath))
container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
ctr.ResolvConfPath, err = ctr.GetRootResourcePath("resolv.conf")
if err != nil {
return err
}
*sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
*sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(ctr.ResolvConfPath))
return nil
}
func (daemon *Daemon) initializeNetworkingPaths(container *container.Container, nc *container.Container) error {
container.HostnamePath = nc.HostnamePath
container.HostsPath = nc.HostsPath
container.ResolvConfPath = nc.ResolvConfPath
func (daemon *Daemon) initializeNetworkingPaths(ctr *container.Container, nc *container.Container) error {
ctr.HostnamePath = nc.HostnamePath
ctr.HostsPath = nc.HostsPath
ctr.ResolvConfPath = nc.ResolvConfPath
return nil
}
func (daemon *Daemon) setupContainerMountsRoot(c *container.Container) error {
func (daemon *Daemon) setupContainerMountsRoot(ctr *container.Container) error {
// get the root mount path so we can make it unbindable
p, err := c.MountsResourcePath("")
p, err := ctr.MountsResourcePath("")
if err != nil {
return err
}

View File

@@ -13,16 +13,16 @@ import (
"github.com/pkg/errors"
)
func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
func (daemon *Daemon) setupLinkedContainers(ctr *container.Container) ([]string, error) {
return nil, nil
}
func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
if len(c.ConfigReferences) == 0 {
func (daemon *Daemon) setupConfigDir(ctr *container.Container) (setupErr error) {
if len(ctr.ConfigReferences) == 0 {
return nil
}
localPath := c.ConfigsDirPath()
localPath := ctr.ConfigsDirPath()
log.G(context.TODO()).Debugf("configs: setting up config dir: %s", localPath)
// create local config root
@@ -38,11 +38,11 @@ func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
}
}()
if c.DependencyStore == nil {
if ctr.DependencyStore == nil {
return fmt.Errorf("config store is not initialized")
}
for _, configRef := range c.ConfigReferences {
for _, configRef := range ctr.ConfigReferences {
// TODO (ehazlett): use type switch when more are supported
if configRef.File == nil {
// Runtime configs are not mounted into the container, but they're
@@ -56,14 +56,14 @@ func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
continue
}
fPath, err := c.ConfigFilePath(*configRef)
fPath, err := ctr.ConfigFilePath(*configRef)
if err != nil {
return errors.Wrap(err, "error getting config file path for container")
}
log := log.G(context.TODO()).WithFields(log.Fields{"name": configRef.File.Name, "path": fPath})
log.Debug("injecting config")
config, err := c.DependencyStore.Configs().Get(configRef.ConfigID)
config, err := ctr.DependencyStore.Configs().Get(configRef.ConfigID)
if err != nil {
return errors.Wrap(err, "unable to get config from config store")
}
@@ -75,7 +75,7 @@ func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
return nil
}
func (daemon *Daemon) setupIpcDirs(container *container.Container) error {
func (daemon *Daemon) setupIpcDirs(ctr *container.Container) error {
return nil
}
@@ -85,16 +85,16 @@ func (daemon *Daemon) setupIpcDirs(container *container.Container) error {
// inside the container. Without this fix, docker cp is broken to any
// container which has a volume, regardless of where the file is inside the
// container.
func (daemon *Daemon) mountVolumes(container *container.Container) error {
func (daemon *Daemon) mountVolumes(ctr *container.Container) error {
return nil
}
func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
if len(c.SecretReferences) == 0 {
func (daemon *Daemon) setupSecretDir(ctr *container.Container) (setupErr error) {
if len(ctr.SecretReferences) == 0 {
return nil
}
localMountPath, err := c.SecretMountPath()
localMountPath, err := ctr.SecretMountPath()
if err != nil {
return err
}
@@ -113,11 +113,11 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
}
}()
if c.DependencyStore == nil {
if ctr.DependencyStore == nil {
return fmt.Errorf("secret store is not initialized")
}
for _, s := range c.SecretReferences {
for _, s := range ctr.SecretReferences {
// TODO (ehazlett): use type switch when more are supported
if s.File == nil {
log.G(context.TODO()).Error("secret target type is not a file target")
@@ -126,7 +126,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
// secrets are created in the SecretMountPath on the host, at a
// single level
fPath, err := c.SecretFilePath(*s)
fPath, err := ctr.SecretFilePath(*s)
if err != nil {
return err
}
@@ -134,7 +134,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
"name": s.File.Name,
"path": fPath,
}).Debug("injecting secret")
secret, err := c.DependencyStore.Secrets().Get(s.SecretID)
secret, err := ctr.DependencyStore.Secrets().Get(s.SecretID)
if err != nil {
return errors.Wrap(err, "unable to get secret from secret store")
}
@@ -146,7 +146,7 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
return nil
}
func killProcessDirectly(container *container.Container) error {
func killProcessDirectly(ctr *container.Container) error {
return nil
}
@@ -163,7 +163,7 @@ func serviceDiscoveryOnDefaultNetwork() bool {
return true
}
func buildSandboxPlatformOptions(container *container.Container, cfg *config.Config, sboxOptions *[]libnetwork.SandboxOption) error {
func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config, sboxOptions *[]libnetwork.SandboxOption) error {
// By default, the Windows internal resolver forwards requests to external
// resolvers - but forwarding can be disabled using feature flag
// "windows-dns-proxy":false.
@@ -173,12 +173,12 @@ func buildSandboxPlatformOptions(container *container.Container, cfg *config.Con
return nil
}
func (daemon *Daemon) initializeNetworkingPaths(container *container.Container, nc *container.Container) error {
func (daemon *Daemon) initializeNetworkingPaths(ctr *container.Container, nc *container.Container) error {
if nc.HostConfig.Isolation.IsHyperV() {
return fmt.Errorf("sharing of hyperv containers network is not supported")
}
container.NetworkSharedContainerID = nc.ID
ctr.NetworkSharedContainerID = nc.ID
if nc.NetworkSettings != nil {
for n := range nc.NetworkSettings.Networks {
@@ -200,12 +200,12 @@ func (daemon *Daemon) initializeNetworkingPaths(container *container.Container,
if data["GW_INFO"] != nil {
gwInfo := data["GW_INFO"].(map[string]interface{})
if gwInfo["hnsid"] != nil {
container.SharedEndpointList = append(container.SharedEndpointList, gwInfo["hnsid"].(string))
ctr.SharedEndpointList = append(ctr.SharedEndpointList, gwInfo["hnsid"].(string))
}
}
if data["hnsid"] != nil {
container.SharedEndpointList = append(container.SharedEndpointList, data["hnsid"].(string))
ctr.SharedEndpointList = append(ctr.SharedEndpointList, data["hnsid"].(string))
}
}
}