diff --git a/pkg/compose/reconcile.go b/pkg/compose/reconcile.go index af70e484d..c1a9421dc 100644 --- a/pkg/compose/reconcile.go +++ b/pkg/compose/reconcile.go @@ -1356,7 +1356,13 @@ func (r *reconciler) planServiceStart(service types.ServiceConfig) error { if service.Provider != nil { return r.planProviderStart(service) } - if service.GetScale() == 0 { + // imperative parity (startService): deploy.replicas: 0 is an unconditional + // no-op, but a service with scale: 0 is still visited -- its depends_on + // conditions are evaluated and its dependents ordered after them -- and + // whatever replicas it already owns are started (scope Start only: the + // create phase removes them all under CreateStart). It is just never an + // error for it to have no container to start. + if service.Deploy != nil && service.Deploy.Replicas != nil && *service.Deploy.Replicas == 0 { return nil } @@ -1370,7 +1376,7 @@ func (r *reconciler) planServiceStart(service types.ServiceConfig) error { if err != nil { return err } - if len(replicas) == 0 && !anyRunning && r.options.Scope == ScopeStart { + if len(replicas) == 0 && !anyRunning && r.options.Scope == ScopeStart && service.GetScale() > 0 { // imperative parity (startService): a scale>0 service with no // container at all cannot be started — only reachable under scope // Start, since CreateStart would have planned the missing creates diff --git a/pkg/compose/reconcile_start_test.go b/pkg/compose/reconcile_start_test.go index 82b243a60..dc4abbd42 100644 --- a/pkg/compose/reconcile_start_test.go +++ b/pkg/compose/reconcile_start_test.go @@ -307,6 +307,69 @@ func TestPlanStart_AllRunningStillWaitsOnConditions(t *testing.T) { assert.Assert(t, !wait.Operation.BestEffort) } +// Imperative parity (startService): a service with scale: 0 is still visited +// -- its depends_on conditions are evaluated before the no-op, so an unhealthy +// dependency still fails `up`/`start` and whatever is ordered after the +// service is ordered after those prerequisites -- and under scope Start the +// replicas it already owns are started. Only deploy.replicas: 0 is an +// unconditional no-op. Having no container is never an error for it. +func TestPlanStart_ScaleZeroIsStillVisited(t *testing.T) { + zero := 0 + db := types.ServiceConfig{Name: "db"} + scaled := serviceWithDeps("app", types.DependsOnConfig{"db": {Condition: types.ServiceConditionHealthy, Required: true}}) + scaled.Scale = &zero + deployed := serviceWithDeps("job", types.DependsOnConfig{"db": {Condition: types.ServiceConditionHealthy, Required: true}}) + deployed.Deploy = &types.DeployConfig{Replicas: &zero} + project := &types.Project{ + Name: "myproject", + Services: types.Services{"db": db, "app": scaled, "job": deployed}, + } + dbHash, err := serviceHashWithResolvedRefs(db, nil) + assert.NilError(t, err) + observed := emptyObserved() + observed.Containers["db"] = []ObservedContainer{observedServiceContainer("db", 1, container.StateRunning, dbHash)} + + // no container for the scale-zero service: its dependency wait is planned + // and nothing is an error; deploy.replicas: 0 plans nothing at all + for _, scope := range []ReconcileScope{ScopeStart, ScopeCreateStart} { + plan, err := reconcile(t.Context(), project, observed, startScopeOptions(scope), noPrompt) + assert.NilError(t, err) + waits := 0 + for _, n := range plan.Nodes { + switch n.Operation.Type { + case OpWaitCondition: + waits++ + assert.Equal(t, n.Operation.Name, "db") + case OpStartContainer, OpCreateContainer: + t.Fatalf("no container expected for scale-zero services, got %s %s:\n%s", n.Operation.Type, n.Operation.ResourceID, plan) + } + } + assert.Equal(t, waits, 1, "one deduplicated db wait, from app only:\n%s", plan) + } + + // scope Start also starts the replicas the scale-zero service still owns + appHash, err := serviceHashWithResolvedRefs(scaled, nil) + assert.NilError(t, err) + observed.Containers["app"] = []ObservedContainer{observedServiceContainer("app", 1, container.StateExited, appHash)} + plan, err := reconcile(t.Context(), project, observed, startScopeOptions(ScopeStart), noPrompt) + assert.NilError(t, err) + var started []string + for _, n := range plan.Nodes { + if n.Operation.Type == OpStartContainer { + started = append(started, n.Operation.ResourceID) + } + } + assert.DeepEqual(t, started, []string{"service:app:1"}) + + // the create phase removes every replica of a scale-zero service before + // the start phase looks: nothing to start under CreateStart + plan, err = reconcile(t.Context(), project, observed, startScopeOptions(ScopeCreateStart), noPrompt) + assert.NilError(t, err) + for _, n := range plan.Nodes { + assert.Assert(t, n.Operation.Type != OpStartContainer, "unexpected start node:\n%s", plan) + } +} + // Imperative parity (startService): under scope Start, a scale>0 service // with no container at all fails the plan the way `compose start` fails. func TestPlanStart_NoContainerToStartFails(t *testing.T) {