Skip to content

Commit 2d16a05

Browse files
committed
only check if a dependency is required when something unexpected happens
Signed-off-by: Guillaume Lours <[email protected]>
1 parent bb94ea0 commit 2d16a05

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

pkg/compose/convergence.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,18 @@ func containerEvents(containers Containers, eventFunc func(string) progress.Even
286286
return events
287287
}
288288

289+
func containerSkippedEvents(containers Containers, eventFunc func(string, string) progress.Event, reason string) []progress.Event {
290+
events := []progress.Event{}
291+
for _, container := range containers {
292+
events = append(events, eventFunc(getContainerProgressName(container), reason))
293+
}
294+
return events
295+
}
296+
289297
// ServiceConditionRunningOrHealthy is a service condition on status running or healthy
290298
const ServiceConditionRunningOrHealthy = "running_or_healthy"
291299

300+
//nolint:gocyclo
292301
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependencies types.DependsOnConfig, containers Containers) error {
293302
eg, _ := errgroup.WithContext(ctx)
294303
w := progress.ContextWriter(ctx)
@@ -312,6 +321,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
312321
case ServiceConditionRunningOrHealthy:
313322
healthy, err := s.isServiceHealthy(ctx, waitingFor, true)
314323
if err != nil {
324+
if !config.Required {
325+
w.Events(containerSkippedEvents(waitingFor, progress.SkippedEvent, fmt.Sprintf("optional dependency %q is not running or is unhealthy", dep)))
326+
logrus.Warnf("optional dependency %q is not running or is unhealthy: %s", dep, err.Error())
327+
return nil
328+
}
315329
return err
316330
}
317331
if healthy {
@@ -321,6 +335,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
321335
case types.ServiceConditionHealthy:
322336
healthy, err := s.isServiceHealthy(ctx, waitingFor, false)
323337
if err != nil {
338+
if !config.Required {
339+
w.Events(containerSkippedEvents(waitingFor, progress.SkippedEvent, fmt.Sprintf("optional dependency %q failed to start", dep)))
340+
logrus.Warnf("optional dependency %q failed to start: %s", dep, err.Error())
341+
return nil
342+
}
324343
w.Events(containerEvents(waitingFor, progress.ErrorEvent))
325344
return errors.Wrap(err, "dependency failed to start")
326345
}
@@ -334,6 +353,12 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
334353
return err
335354
}
336355
if exited {
356+
logMessageSuffix := fmt.Sprintf("%q didn't complete successfully: exit %d", dep, code)
357+
if !config.Required {
358+
w.Events(containerSkippedEvents(waitingFor, progress.SkippedEvent, fmt.Sprintf("optional dependency %s", logMessageSuffix)))
359+
logrus.Warnf("optional dependency %s", logMessageSuffix)
360+
return nil
361+
}
337362
w.Events(containerEvents(waitingFor, progress.Exited))
338363
if code != 0 {
339364
return fmt.Errorf("service %q didn't complete successfully: exit %d", dep, code)
@@ -362,9 +387,6 @@ func shouldWaitForDependency(serviceName string, dependencyConfig types.ServiceD
362387
return false, nil
363388
}
364389
}
365-
if !dependencyConfig.Required {
366-
return false, nil
367-
}
368390
return false, err
369391
} else if service.Scale == 0 {
370392
// don't wait for the dependency which configured to have 0 containers running

pkg/e2e/up_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestUpWithDependencyNotRequired(t *testing.T) {
162162
})
163163

164164
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/deps-not-required.yaml", "--project-name", projectName,
165-
"up", "-d")
165+
"--profile", "not-required", "up", "-d")
166166
assert.Assert(t, strings.Contains(res.Combined(), "foo"), res.Combined())
167-
assert.Assert(t, !strings.Contains(res.Combined(), "bar"), res.Combined())
167+
assert.Assert(t, strings.Contains(res.Combined(), " optional dependency \"bar\" failed to start"), res.Combined())
168168
}

0 commit comments

Comments
 (0)