func collectManagerErrors()

in internal/pkg/agent/application/coordinator/coordinator.go [1822:1896]


func collectManagerErrors(timeout time.Duration, varsErrCh, runtimeErrCh, configErrCh, otelErrCh, upgradeMarkerWatcherErrCh chan error) error {
	var runtimeErr, configErr, varsErr, otelErr, upgradeMarkerWatcherErr error
	var returnedRuntime, returnedConfig, returnedVars, returnedOtel, returnedUpgradeMarkerWatcher bool

	// in case other components are locked up, let us time out
	timeoutWait := time.NewTimer(timeout)
	defer timeoutWait.Stop()

	/*
		Wait for all managers to gently shut down. All managers send
		an error status on their termination channel after their Run method
		returns.
		Logic:
		If all three manager channels return a value, or close, we're done.
		If any errors are non-nil (and not just context.Canceled), collect and
		return them with multierror.
		Otherwise, return nil.
	*/

	// combinedErr will store any reported errors as well as timeout errors
	// for unresponsive managers.
	var errs []error

waitLoop:
	for !returnedRuntime || !returnedConfig || !returnedVars || !returnedOtel || !returnedUpgradeMarkerWatcher {
		select {
		case runtimeErr = <-runtimeErrCh:
			returnedRuntime = true
		case configErr = <-configErrCh:
			returnedConfig = true
		case varsErr = <-varsErrCh:
			returnedVars = true
		case otelErr = <-otelErrCh:
			returnedOtel = true
		case upgradeMarkerWatcherErr = <-upgradeMarkerWatcherErrCh:
			returnedUpgradeMarkerWatcher = true
		case <-timeoutWait.C:
			var timeouts []string
			if !returnedRuntime {
				timeouts = []string{"no response from runtime manager"}
			}
			if !returnedConfig {
				timeouts = append(timeouts, "no response from config manager")
			}
			if !returnedVars {
				timeouts = append(timeouts, "no response from vars manager")
			}
			if !returnedOtel {
				timeouts = append(timeouts, "no response from otel manager")
			}
			if !returnedUpgradeMarkerWatcher {
				timeouts = append(timeouts, "no response from upgrade marker watcher")
			}
			timeoutStr := strings.Join(timeouts, ", ")
			errs = append(errs, fmt.Errorf("timeout while waiting for managers to shut down: %v", timeoutStr))
			break waitLoop
		}
	}
	if runtimeErr != nil && !errors.Is(runtimeErr, context.Canceled) {
		errs = append(errs, fmt.Errorf("runtime manager: %w", runtimeErr))
	}
	if configErr != nil && !errors.Is(configErr, context.Canceled) {
		errs = append(errs, fmt.Errorf("config manager: %w", configErr))
	}
	if varsErr != nil && !errors.Is(varsErr, context.Canceled) {
		errs = append(errs, fmt.Errorf("vars manager: %w", varsErr))
	}
	if otelErr != nil && !errors.Is(otelErr, context.Canceled) {
		errs = append(errs, fmt.Errorf("otel manager: %w", otelErr))
	}
	if upgradeMarkerWatcherErr != nil && !errors.Is(upgradeMarkerWatcherErr, context.Canceled) {
		errs = append(errs, fmt.Errorf("upgrade marker watcher: %w", upgradeMarkerWatcherErr))
	}
	return errors.Join(errs...)
}