mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
The Container.State struct holds the container's state, and most of
its fields are expected to change dynamically. Some o these state-changes
are explicit, for example, setting the container to be "stopped". Other
state changes can be more explicit, for example due to the containers'
process exiting or being "OOM" killed by the kernel.
The distinction between explicit ("desired") state changes and "state"
("actual state") is sometimes vague; for some properties, we clearly
separated them, for example if a user requested the container to be
stopped or restarted, we store state in the Container object itself;
HasBeenManuallyStopped bool // used for unless-stopped restart policy
HasBeenManuallyRestarted bool `json:"-"` // used to distinguish restart caused by restart policy from the manual one
Other properties are more ambiguous. such as "HasBeenStartedBefore" and
"RestartCount", which are stored on the Container (and persisted to
disk), but may be more related to "actual" state, and likely should
not be persisted;
RestartCount int
HasBeenStartedBefore bool
Given that (per the above) concurrency must be taken into account, most
changes to the `container.State` struct should be protected; here's where
things get blurry. While the `State` type provides various accessor methods,
only some of them take concurrency into account; for example, [State.IsRunning]
and [State.GetPID] acquire a lock, whereas [State.ExitCodeValue] does not.
Even the (commonly used) [State.StateString] has no locking at all.
The way to handle this is error-prone; [container.State] contains a mutex,
and it's exported. Given that its embedded in the [container.Container]
struct, it's also exposed as an exported mutex for the container. The
assumption here is that by "merging" the two, the caller to acquire a lock
when either the container _or_ its state must be mutated. However, because
some methods on `container.State` handle their own locking, consumers must
be deeply familiar with the internals; if both changes to the `Container`
AND `Container.State` must be made. This gets amplified more as some
(exported!) methods, such as [container.SetRunning] mutate multiple fields,
but don't acquire a lock (so expect the caller to hold one), but their
(also exported) counterpart (e.g. [State.IsRunning]) do.
It should be clear from the above, that this needs some architectural
changes; a clearer separation between "desired" and "actual" state (opening
the potential to update the container's config without manually touching
its `State`), possibly a method to obtain a read-only copy of the current
state (for those querying state), and reviewing which fields belong where
(and should be persisted to disk, or only remain in memory).
This PR preserves the status quo; it makes no structural changes, other
than exposing where we access the container's state. Where previously the
State fields and methods were referred to as "part of the container"
(e.g. `ctr.IsRunning()` or `ctr.Running`), we now explicitly reference
the embedded `State` (`ctr.State.IsRunning`, `ctr.State.Running`).
The exception (for now) is the mutex, which is still referenced through
the embedded struct (`ctr.Lock()` instead of `ctr.State.Lock()`), as this
is (mostly) by design to protect the container, and what's in it (including
its `State`).
[State.IsRunning]: c4afa77157/daemon/container/state.go (L205-L209)
[State.GetPID]: c4afa77157/daemon/container/state.go (L211-L216)
[State.ExitCodeValue]: c4afa77157/daemon/container/state.go (L218-L228)
[State.StateString]: c4afa77157/daemon/container/state.go (L102-L131)
[container.State]: c4afa77157/daemon/container/state.go (L15-L23)
[container.Container]: c4afa77157/daemon/container/container.go (L67-L75)
[container.SetRunning]: c4afa77157/daemon/container/state.go (L230-L277)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
167 lines
4.4 KiB
Go
167 lines
4.4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/containerd/log"
|
|
"github.com/moby/moby/api/types/events"
|
|
"github.com/moby/moby/v2/daemon/container"
|
|
"github.com/moby/moby/v2/daemon/libnetwork"
|
|
"github.com/moby/moby/v2/daemon/network"
|
|
"github.com/moby/moby/v2/errdefs"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// ContainerRename changes the name of a container, using the oldName
|
|
// to find the container. An error is returned if newName is already
|
|
// reserved.
|
|
func (daemon *Daemon) ContainerRename(oldName, newName string) (retErr error) {
|
|
if oldName == "" || newName == "" {
|
|
return errdefs.InvalidParameter(errors.New("Neither old nor new names may be empty"))
|
|
}
|
|
|
|
ctr, err := daemon.GetContainer(oldName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctr.Lock()
|
|
defer ctr.Unlock()
|
|
|
|
// Canonicalize name for comparing.
|
|
if newName[0] != '/' {
|
|
newName = "/" + newName
|
|
}
|
|
if ctr.Name == newName {
|
|
return errdefs.InvalidParameter(errors.New("Renaming a container with the same name as its current name"))
|
|
}
|
|
|
|
links := map[string]*container.Container{}
|
|
for k, v := range daemon.linkIndex.children(ctr) {
|
|
if !strings.HasPrefix(k, ctr.Name) {
|
|
return errdefs.InvalidParameter(errors.Errorf("Linked container %s does not match parent %s", k, ctr.Name))
|
|
}
|
|
links[strings.TrimPrefix(k, ctr.Name)] = v
|
|
}
|
|
|
|
newName, err = daemon.reserveName(ctr.ID, newName)
|
|
if err != nil {
|
|
return errors.Wrap(err, "Error when allocating new name")
|
|
}
|
|
|
|
for k, v := range links {
|
|
daemon.containersReplica.ReserveName(newName+k, v.ID)
|
|
daemon.linkIndex.link(ctr, v, newName+k)
|
|
}
|
|
|
|
oldName = ctr.Name
|
|
ctr.Name = newName
|
|
|
|
defer func() {
|
|
if retErr != nil {
|
|
ctr.Name = oldName
|
|
daemon.reserveName(ctr.ID, oldName)
|
|
for k, v := range links {
|
|
daemon.containersReplica.ReserveName(oldName+k, v.ID)
|
|
daemon.linkIndex.link(ctr, v, oldName+k)
|
|
daemon.linkIndex.unlink(newName+k, v, ctr)
|
|
daemon.containersReplica.ReleaseName(newName + k)
|
|
}
|
|
daemon.releaseName(newName)
|
|
} else {
|
|
daemon.releaseName(oldName)
|
|
}
|
|
}()
|
|
|
|
for k, v := range links {
|
|
daemon.linkIndex.unlink(oldName+k, v, ctr)
|
|
daemon.containersReplica.ReleaseName(oldName + k)
|
|
}
|
|
if err := ctr.CheckpointTo(context.TODO(), daemon.containersReplica); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !ctr.State.Running {
|
|
daemon.LogContainerEventWithAttributes(ctr, events.ActionRename, map[string]string{
|
|
"oldName": oldName,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
defer func() {
|
|
if retErr != nil {
|
|
ctr.Name = oldName
|
|
if err := ctr.CheckpointTo(context.WithoutCancel(context.TODO()), daemon.containersReplica); err != nil {
|
|
log.G(context.TODO()).WithFields(log.Fields{
|
|
"containerID": ctr.ID,
|
|
"error": err,
|
|
}).Error("failed to write container state to disk during rename")
|
|
}
|
|
}
|
|
}()
|
|
|
|
if sid := ctr.NetworkSettings.SandboxID; sid != "" && daemon.netController != nil {
|
|
sb, err := daemon.netController.SandboxByID(sid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = sb.Rename(strings.TrimPrefix(ctr.Name, "/")); err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if retErr != nil {
|
|
if err := sb.Rename(oldName); err != nil {
|
|
log.G(context.TODO()).WithFields(log.Fields{
|
|
"sandboxID": sid,
|
|
"oldName": oldName,
|
|
"newName": newName,
|
|
"error": err,
|
|
}).Errorf("failed to revert sandbox rename")
|
|
}
|
|
}
|
|
}()
|
|
|
|
for nwName, epConfig := range ctr.NetworkSettings.Networks {
|
|
nw, err := daemon.FindNetwork(nwName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ep := sb.GetEndpoint(epConfig.EndpointID)
|
|
if ep == nil {
|
|
return fmt.Errorf("no endpoint attached to network %s found", nw.Name())
|
|
}
|
|
|
|
oldDNSNames := make([]string, len(epConfig.DNSNames))
|
|
copy(oldDNSNames, epConfig.DNSNames)
|
|
|
|
epConfig.DNSNames = buildEndpointDNSNames(ctr, epConfig.Aliases)
|
|
if err := ep.UpdateDNSNames(epConfig.DNSNames); err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func(ep *libnetwork.Endpoint, epConfig *network.EndpointSettings, oldDNSNames []string) {
|
|
if retErr == nil {
|
|
return
|
|
}
|
|
|
|
epConfig.DNSNames = oldDNSNames
|
|
if err := ep.UpdateDNSNames(epConfig.DNSNames); err != nil {
|
|
log.G(context.TODO()).WithFields(log.Fields{
|
|
"sandboxID": sid,
|
|
"oldName": oldName,
|
|
"newName": newName,
|
|
"error": err,
|
|
}).Errorf("failed to revert DNSNames update")
|
|
}
|
|
}(ep, epConfig, oldDNSNames)
|
|
}
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(ctr, events.ActionRename, map[string]string{
|
|
"oldName": oldName,
|
|
})
|
|
return nil
|
|
}
|