Browse Source

Merge pull request #11110 from ndeloof/wait_missing

fail start if depependency is missing
Guillaume Lours 2 years ago
parent
commit
b8773ad1c5
4 changed files with 13 additions and 6 deletions
  1. 9 2
      pkg/compose/convergence.go
  2. 2 2
      pkg/compose/convergence_test.go
  3. 1 1
      pkg/compose/run.go
  4. 1 1
      pkg/compose/start.go

+ 9 - 2
pkg/compose/convergence.go

@@ -318,7 +318,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 {
@@ -330,6 +330,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 {
@@ -729,7 +736,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
 	}

+ 2 - 2
pkg/compose/convergence_test.go

@@ -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}
@@ -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))
 	})
 }

+ 1 - 1
pkg/compose/run.go

@@ -92,7 +92,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
 	}
 
 	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
 		}
 	}

+ 1 - 1
pkg/compose/start.go

@@ -148,7 +148,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 errors.Is(ctx.Err(), context.DeadlineExceeded) {
 				return fmt.Errorf("application not healthy after %s", options.WaitTimeout)