daemon: deprecate Daemon.Register and make it internal

This function was only used internally in the daemon. This patch splits
the implementation to a non-exported version and deprecates the exported
one.

While at it, also pass through the context (which is used for tracing),
and added a note about the function potentially not being atomic.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-10-20 13:23:12 +02:00
parent b0632b2345
commit 5208e2954c
3 changed files with 20 additions and 3 deletions

View File

@@ -103,7 +103,14 @@ func (daemon *Daemon) load(id string) (*container.Container, error) {
}
// Register makes a container object usable by the daemon as <container.ID>
//
// Deprecated: this function is unused and will be removed in the next release.
func (daemon *Daemon) Register(c *container.Container) error {
return daemon.register(context.TODO(), c)
}
// register makes a container object usable by the daemon as [container.Container.ID].
func (daemon *Daemon) register(ctx context.Context, c *container.Container) error {
// Attach to stdout and stderr
if c.Config.OpenStdin {
c.StreamConfig.NewInputPipes()
@@ -116,8 +123,18 @@ func (daemon *Daemon) Register(c *container.Container) error {
c.Lock()
defer c.Unlock()
// FIXME(thaJeztah): this logic may not be atomic:
//
// - daemon.containers.Add does not promise to "add", allows overwriting a container with the given ID.
// - c.CheckpointTo may fail, in which case we registered a container, but failed to write to disk
//
// We should consider:
//
// - changing the signature of containers.Add to return an error if the
// given ID exists (potentially adding an alternative to "set" / "update")
// - adding a defer to rollback the "Add" when failing to CheckPoint.
daemon.containers.Add(c.ID, c)
return c.CheckpointTo(context.TODO(), daemon.containersReplica)
return c.CheckpointTo(ctx, daemon.containersReplica)
}
func (daemon *Daemon) newContainer(name string, operatingSystem string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {

View File

@@ -236,7 +236,7 @@ func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts
}
daemon.updateContainerNetworkSettings(ctr, endpointsConfigs)
if err := daemon.Register(ctr); err != nil {
if err := daemon.register(ctx, ctr); err != nil {
return nil, err
}
stateCtr.set(ctr.ID, "stopped")

View File

@@ -335,7 +335,7 @@ func (daemon *Daemon) restore(cfg *configStore) error {
mapLock.Unlock()
return
}
if err := daemon.Register(c); err != nil {
if err := daemon.register(context.TODO(), c); err != nil {
logger.WithError(err).Error("failed to register container")
mapLock.Lock()
delete(containers, c.ID)