compose/pkg/compose/executor_ops.go
Nicolas De Loof eaf172eab2 feat: reconciliation plan creates pre_start hook runners
pre_start hook containers become first-class resources of the
reconciliation plan. The reconciler plans one OpCreateHookContainer per
declared hook — gated on the same predicate the start phase uses to run
the hooks: a replica to start and no running replica SURVIVING the plan
(a recreated replica is not running at start time, so its runners are
planned too). Runner creation depends on every replica node (the
executor resolves the VolumesFrom target against a final live view),
on the infrastructure, and on the purge of previously observed runners:
names are deterministic (<project>-<service>-pre_start-<i>) so repeated
plans converge on the same container instead of accumulating anonymous
ones, and a new HookIndexLabel ties each runner to its hook.

runPreStart becomes pure execution: it looks up the created-state
runner for each hook by label and never creates one. A declared hook
without a prepared runner is an actionable error naming the
reconciliation command (docker compose up <service>) — the accepted
consequence is that stop-then-start of a hooked service errors, since
runners are consumed on success (locked by e2e). Success removal,
failure retention and cancellation cleanup are unchanged.

Runners stay invisible to the start/ps container listings — they carry
no ConfigHashLabel, which getDefaultFilters requires — and down keeps
removing them by hook label.

Rebased onto main, which diverged substantially since this branch was
opened (the unrelated container-spec-layering refactor, #14093,
restructured compose-go's ServiceConfig/PreStartHook types this PR also
touches). Reconciling pre_start.go/pre_start_test.go against that
refactor surfaced a real bug: createPreStartContainer builds the hook's
config via the shared getCreateConfigs, which unconditionally stamps a
ConfigHashLabel — silently contradicting this same commit's own
"invisible to ps/start" guarantee above, since that label is exactly
what getDefaultFilters requires. Fixed by deleting the label after
getCreateConfigs returns, with a test asserting its absence (an
independent local review, via the GitHub Copilot CLI, and a
fresh-context review agent both converged on catching this during
rebase verification). Also restored two security/label tests that only
existed on one side of the rebase (SecuritySensitiveFieldsHonored,
HookLabelsMerged), ported to call createPreStartContainer directly.

Also addresses glours' three review comments on this PR: hook-container
create operations for a service no longer chain serially (each depends
on the same shared containerNodes+infraDeps+purges set, letting the
executor create them in parallel), the corresponding plan-string test
assertion, and a clearer missing-runner error message hinting this
isn't necessarily a planning bug.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-09-30 12:03:10 +02:00

220 lines
8 KiB
Go

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"context"
"fmt"
"slices"
"github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
"github.com/sirupsen/logrus"
"github.com/docker/compose/v5/pkg/api"
"github.com/docker/compose/v5/pkg/utils"
)
// --- Network operations ---
func (exec *planExecutor) execCreateNetwork(ctx context.Context, op Operation) error {
return exec.compose.createNetwork(ctx, op.Network)
}
func (exec *planExecutor) execRemoveNetwork(ctx context.Context, op Operation) error {
_, err := exec.compose.apiClient().NetworkRemove(ctx, op.Name, client.NetworkRemoveOptions{})
// A best-effort removal (old network on a rename) tolerates the network
// still being in use — Docker reports that as a conflict. Any other error
// (transport failure, Moby API error, ...) is still propagated.
if err != nil && op.BestEffort && errdefs.IsConflict(err) {
logrus.Warnf("network %s is still in use and was left in place; remove it manually once no container is attached", op.Name)
return nil
}
return err
}
func (exec *planExecutor) execDisconnectNetwork(ctx context.Context, op Operation) error {
_, err := exec.compose.apiClient().NetworkDisconnect(ctx, op.Name, client.NetworkDisconnectOptions{
Container: op.Container.ID,
Force: true,
})
return err
}
func (exec *planExecutor) execConnectNetwork(ctx context.Context, op Operation) error {
_, err := exec.compose.apiClient().NetworkConnect(ctx, op.Name, client.NetworkConnectOptions{
Container: op.Container.ID,
})
return err
}
// --- Volume operations ---
func (exec *planExecutor) execCreateVolume(ctx context.Context, op Operation) error {
return exec.compose.createVolume(ctx, *op.Volume)
}
func (exec *planExecutor) execRemoveVolume(ctx context.Context, op Operation) error {
_, err := exec.compose.apiClient().VolumeRemove(ctx, op.Name, client.VolumeRemoveOptions{Force: true})
return err
}
// --- Container operations ---
func (exec *planExecutor) execCreateContainer(ctx context.Context, node *PlanNode) error {
op := node.Operation
service := *op.Service
// Detach VolumesFrom from the source slice: resolveServiceReferences mutates
// entries in place, and the shallow struct copy still shares the backing array.
service.VolumesFrom = slices.Clone(op.Service.VolumesFrom)
// Resolve service references (network_mode, ipc, pid, volumes_from) to
// actual container IDs from the in-memory view, which already includes
// any containers created by earlier plan nodes.
exec.containersMu.Lock()
err := resolveServiceReferences(&service, exec.containersByService)
exec.containersMu.Unlock()
if err != nil {
return err
}
labels := mergeLabels(service.Labels, service.CustomLabels)
if op.Inherited != nil {
// This is a recreate: add the replace label
replacedName := op.Service.ContainerName
if replacedName == "" {
replacedName = fmt.Sprintf("%s%s%d", op.Service.Name, api.Separator, op.Number)
}
labels = labels.Add(api.ContainerReplaceLabel, replacedName)
}
options := createOptions{
AutoRemove: false,
AttachStdin: false,
UseNetworkAliases: true,
Labels: labels,
}
ctr, err := exec.compose.createMobyContainer(ctx, exec.project, service, op.Name, op.Number, op.Inherited, options)
if err != nil {
return err
}
exec.pctx.set(node.ID, operationResult{
ContainerID: ctr.ID,
ContainerName: op.Name,
})
// Make the new container visible to subsequent execCreateContainer calls
// that resolve service references against op.Service.Name.
exec.containersMu.Lock()
exec.containersByService[op.Service.Name] = append(exec.containersByService[op.Service.Name], ctr)
exec.containersMu.Unlock()
return nil
}
func (exec *planExecutor) execStartContainer(ctx context.Context, op Operation) error {
startMx.Lock()
defer startMx.Unlock()
_, err := exec.compose.apiClient().ContainerStart(ctx, op.Container.ID, client.ContainerStartOptions{})
return err
}
func (exec *planExecutor) execStopContainer(ctx context.Context, op Operation) error {
_, err := exec.compose.apiClient().ContainerStop(ctx, op.Container.ID, client.ContainerStopOptions{
Timeout: utils.DurationSecondToInt(op.Timeout),
})
return err
}
func (exec *planExecutor) execRemoveContainer(ctx context.Context, op Operation) error {
_, err := exec.compose.apiClient().ContainerRemove(ctx, op.Container.ID, client.ContainerRemoveOptions{Force: true, RemoveVolumes: op.RemoveVolumes})
if err != nil {
if op.BestEffort {
// warn-only removal (stale pre_start hook runner): the container
// stays visible to the operator, the plan carries on — and the
// live view below keeps it, since it was not removed
logrus.Warnf("failed to remove %s: %v", op.ResourceID, err)
return nil
}
return err
}
// Why: a dependent service's create may resolve `network_mode: service:X`
// (or volumes_from / ipc / pid) against the live view. Containers.sorted()
// orders by canonical name; without this drop, a just-removed container
// can still win the lookup and the dependent receives a container:<id>
// reference that no longer exists in the daemon.
svcName := op.Container.Labels[api.ServiceLabel]
exec.containersMu.Lock()
exec.containersByService[svcName] = slices.DeleteFunc(
exec.containersByService[svcName],
func(c container.Summary) bool { return c.ID == op.Container.ID },
)
exec.containersMu.Unlock()
return nil
}
// execCreateHookContainer creates the runner container for one pre_start hook.
// The target replica — whose volumes the hook shares via VolumesFrom — is
// resolved from the live view at execution time: the node depends on every
// container operation of its service, so the view is final here, and the
// lowest-numbered replica matches the one the start phase hands the hooks.
func (exec *planExecutor) execCreateHookContainer(ctx context.Context, node *PlanNode) error {
// A dependency's done-channel also closes on failure: when a replica
// create failed and canceled the run, bail out on the cancellation
// instead of reporting the empty live view as an internal error.
if err := ctx.Err(); err != nil {
return err
}
op := node.Operation
service := *op.Service
exec.containersMu.Lock()
replicas := slices.Clone(exec.containersByService[service.Name])
exec.containersMu.Unlock()
if len(replicas) == 0 {
return fmt.Errorf("internal: no %q container to attach pre_start hook %d to", service.Name, op.HookIndex)
}
target := lowestNumberedContainer(replicas)
created, err := exec.compose.createPreStartContainer(ctx, exec.project, service, target, op.HookIndex, op.Name)
if err != nil {
return err
}
// Recorded for uniformity with execCreateContainer — every create
// operation publishes its result. No plan operation consumes a hook
// runner's entry today: the start phase rediscovers runners through
// listPreStartRunners, by design.
exec.pctx.set(node.ID, operationResult{
ContainerID: created.ID,
ContainerName: op.Name,
})
return nil
}
func (exec *planExecutor) execRenameContainer(ctx context.Context, node *PlanNode) error {
op := node.Operation
if op.CreateNodeID == 0 {
return fmt.Errorf("internal: rename node #%d missing CreateNodeID", node.ID)
}
createdID := exec.pctx.get(op.CreateNodeID).ContainerID
if createdID == "" {
return fmt.Errorf("internal: rename node #%d: create node #%d returned empty ID", node.ID, op.CreateNodeID)
}
_, err := exec.compose.apiClient().ContainerRename(ctx, createdID, client.ContainerRenameOptions{
NewName: op.Name,
})
return err
}