Skip to content

Commit

Permalink
fail start if depependency is missing
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Oct 19, 2023
1 parent fd8ab2f commit 1ffa194
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func containerReasonEvents(containers Containers, eventFunc func(string, string)
const ServiceConditionRunningOrHealthy = "running_or_healthy"

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

waitingFor := containers.filter(isService(dep))
w.Events(containerEvents(waitingFor, progress.Waiting))
if len(waitingFor) == 0 {
if config.Required {
return fmt.Errorf("%s is missing dependency %s", dependant, dep)
}
logrus.Warnf("%s is missing dependency %s", dependant, dep)
continue
}

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

err := s.waitDependencies(ctx, project, service.DependsOn, containers)
err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, containers)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/convergence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestWaitDependencies(t *testing.T) {
"db": {Condition: ServiceConditionRunningOrHealthy},
"redis": {Condition: ServiceConditionRunningOrHealthy},
}
assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies, nil))
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
})
t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
dbService := types.ServiceConfig{Name: "db", Scale: 1}
Expand All @@ -239,6 +239,6 @@ func TestWaitDependencies(t *testing.T) {
"db": {Condition: types.ServiceConditionStarted, Required: true},
"redis": {Condition: types.ServiceConditionStarted, Required: true},
}
assert.NilError(t, tested.waitDependencies(context.Background(), &project, dependencies, nil))
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
})
}
2 changes: 1 addition & 1 deletion pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
updateServices(&service, observedState)

if !opts.NoDeps {
if err := s.waitDependencies(ctx, project, service.DependsOn, observedState); err != nil {
if err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, observedState); err != nil {
return "", err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
defer cancel()
}

err = s.waitDependencies(ctx, project, depends, containers)
err = s.waitDependencies(ctx, project, project.Name, depends, containers)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
Expand Down

0 comments on commit 1ffa194

Please sign in to comment.