|
| 1 | +package install |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "runtime/debug" |
| 7 | + |
| 8 | + apppreflightmanager "github.com/replicatedhq/embedded-cluster/api/internal/managers/app/preflight" |
| 9 | + "github.com/replicatedhq/embedded-cluster/api/internal/statemachine" |
| 10 | + states "github.com/replicatedhq/embedded-cluster/api/internal/states/install" |
| 11 | + "github.com/replicatedhq/embedded-cluster/api/types" |
| 12 | + ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1" |
| 13 | + "github.com/replicatedhq/embedded-cluster/pkg-new/preflights" |
| 14 | +) |
| 15 | + |
| 16 | +type RunAppPreflightOptions struct { |
| 17 | + ConfigValues types.AppConfigValues |
| 18 | + PreflightBinaryPath string |
| 19 | + ProxySpec *ecv1beta1.ProxySpec |
| 20 | + ExtraPaths []string |
| 21 | +} |
| 22 | + |
| 23 | +func (c *InstallController) RunAppPreflights(ctx context.Context, opts RunAppPreflightOptions) (finalErr error) { |
| 24 | + lock, err := c.stateMachine.AcquireLock() |
| 25 | + if err != nil { |
| 26 | + return types.NewConflictError(err) |
| 27 | + } |
| 28 | + |
| 29 | + defer func() { |
| 30 | + if r := recover(); r != nil { |
| 31 | + finalErr = fmt.Errorf("panic: %v: %s", r, string(debug.Stack())) |
| 32 | + } |
| 33 | + if finalErr != nil { |
| 34 | + lock.Release() |
| 35 | + } |
| 36 | + }() |
| 37 | + |
| 38 | + if err := c.stateMachine.ValidateTransition(lock, states.StateAppPreflightsRunning); err != nil { |
| 39 | + return types.NewConflictError(err) |
| 40 | + } |
| 41 | + |
| 42 | + // Extract app preflight spec from Helm charts |
| 43 | + appPreflightSpec, err := c.appReleaseManager.ExtractAppPreflightSpec(ctx, opts.ConfigValues) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("extract app preflight spec: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + err = c.stateMachine.Transition(lock, states.StateAppPreflightsRunning) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("transition states: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + go func() (finalErr error) { |
| 54 | + // Background context is used to avoid canceling the operation if the context is canceled |
| 55 | + ctx := context.Background() |
| 56 | + |
| 57 | + defer lock.Release() |
| 58 | + |
| 59 | + defer func() { |
| 60 | + if r := recover(); r != nil { |
| 61 | + finalErr = fmt.Errorf("panic running app preflights: %v: %s", r, string(debug.Stack())) |
| 62 | + } |
| 63 | + // Handle errors from preflight execution |
| 64 | + if finalErr != nil { |
| 65 | + c.logger.Error(finalErr) |
| 66 | + |
| 67 | + if err := c.stateMachine.Transition(lock, states.StateAppPreflightsExecutionFailed); err != nil { |
| 68 | + c.logger.Errorf("failed to transition states: %w", err) |
| 69 | + } |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // Get the state from the preflights output |
| 74 | + state := c.getStateFromAppPreflightsOutput(ctx) |
| 75 | + // Transition to the appropriate state based on preflight results |
| 76 | + if err := c.stateMachine.Transition(lock, state); err != nil { |
| 77 | + c.logger.Errorf("failed to transition states: %w", err) |
| 78 | + } |
| 79 | + }() |
| 80 | + |
| 81 | + // Create RunOptions from the provided options |
| 82 | + runOpts := preflights.RunOptions{ |
| 83 | + PreflightBinaryPath: opts.PreflightBinaryPath, |
| 84 | + ProxySpec: opts.ProxySpec, |
| 85 | + ExtraPaths: opts.ExtraPaths, |
| 86 | + } |
| 87 | + |
| 88 | + err := c.appPreflightManager.RunAppPreflights(ctx, apppreflightmanager.RunAppPreflightOptions{ |
| 89 | + AppPreflightSpec: appPreflightSpec, |
| 90 | + RunOptions: runOpts, |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("run app preflights: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | + }() |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func (c *InstallController) getStateFromAppPreflightsOutput(ctx context.Context) statemachine.State { |
| 103 | + output, err := c.GetAppPreflightOutput(ctx) |
| 104 | + // If there was an error getting the state we assume preflight execution failed |
| 105 | + if err != nil { |
| 106 | + c.logger.WithError(err).Error("error getting app preflight output") |
| 107 | + return states.StateAppPreflightsExecutionFailed |
| 108 | + } |
| 109 | + // If there is no output, we assume preflights succeeded |
| 110 | + if output == nil || !output.HasFail() { |
| 111 | + return states.StateAppPreflightsSucceeded |
| 112 | + } |
| 113 | + return states.StateAppPreflightsFailed |
| 114 | +} |
| 115 | + |
| 116 | +func (c *InstallController) GetAppPreflightStatus(ctx context.Context) (types.Status, error) { |
| 117 | + return c.appPreflightManager.GetAppPreflightStatus(ctx) |
| 118 | +} |
| 119 | + |
| 120 | +func (c *InstallController) GetAppPreflightOutput(ctx context.Context) (*types.PreflightsOutput, error) { |
| 121 | + return c.appPreflightManager.GetAppPreflightOutput(ctx) |
| 122 | +} |
| 123 | + |
| 124 | +func (c *InstallController) GetAppPreflightTitles(ctx context.Context) ([]string, error) { |
| 125 | + return c.appPreflightManager.GetAppPreflightTitles(ctx) |
| 126 | +} |
0 commit comments