Skip to content

Commit 662f8b4

Browse files
committed
fail start if depependency is missing
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent fd8ab2f commit 662f8b4

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

pkg/compose/convergence.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func containerReasonEvents(containers Containers, eventFunc func(string, string)
306306
const ServiceConditionRunningOrHealthy = "running_or_healthy"
307307

308308
//nolint:gocyclo
309-
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependencies types.DependsOnConfig, containers Containers) error {
309+
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, service string, dependencies types.DependsOnConfig, containers Containers) error {
310310
eg, _ := errgroup.WithContext(ctx)
311311
w := progress.ContextWriter(ctx)
312312
for dep, config := range dependencies {
@@ -318,6 +318,13 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
318318

319319
waitingFor := containers.filter(isService(dep))
320320
w.Events(containerEvents(waitingFor, progress.Waiting))
321+
if len(waitingFor) == 0 {
322+
if config.Required {
323+
return fmt.Errorf("%s is missing dependency %s", service, dep)
324+
}
325+
logrus.Warnf("%s is missing dependency %s", service, dep)
326+
continue
327+
}
321328

322329
dep, config := dep, config
323330
eg.Go(func() error {
@@ -717,7 +724,7 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
717724
return nil
718725
}
719726

720-
err := s.waitDependencies(ctx, project, service.DependsOn, containers)
727+
err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, containers)
721728
if err != nil {
722729
return err
723730
}

pkg/compose/convergence_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func TestWaitDependencies(t *testing.T) {
229229
"db": {Condition: ServiceConditionRunningOrHealthy},
230230
"redis": {Condition: ServiceConditionRunningOrHealthy},
231231
}
232-
assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies, nil))
232+
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
233233
})
234234
t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
235235
dbService := types.ServiceConfig{Name: "db", Scale: 1}
@@ -239,6 +239,6 @@ func TestWaitDependencies(t *testing.T) {
239239
"db": {Condition: types.ServiceConditionStarted, Required: true},
240240
"redis": {Condition: types.ServiceConditionStarted, Required: true},
241241
}
242-
assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies, nil))
242+
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
243243
})
244244
}

pkg/compose/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
9696
updateServices(&service, observedState)
9797

9898
if !opts.NoDeps {
99-
if err := s.waitDependencies(ctx, project, service.DependsOn, observedState); err != nil {
99+
if err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, observedState); err != nil {
100100
return "", err
101101
}
102102
}

pkg/compose/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
117117
defer cancel()
118118
}
119119

120-
err = s.waitDependencies(ctx, project, depends, containers)
120+
err = s.waitDependencies(ctx, project, project.Name, depends, containers)
121121
if err != nil {
122122
if ctx.Err() == context.DeadlineExceeded {
123123
return fmt.Errorf("application not healthy after %s", options.WaitTimeout)

0 commit comments

Comments
 (0)