Capture pipeline readiness diagnostics and failures

This commit is contained in:
2026-11-28 04:26:30 +00:00
parent 97642c491f
commit 71239b6fbb
8 changed files with 218 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import (
"text/template"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
frameworkdebug "gitea.maximumdirect.net/eric/notarius/internal/framework/debug"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/preflight"
"go.maximumdirect.net/commandkit"
@@ -37,26 +38,39 @@ var preflightTemplate = template.Must(commandkit.ParseTextTemplate("pipeline-pre
{{range .Checks}}{{if ne .Result "passed"}}{{.Result}}: {{.Message}}
{{end}}{{end}}`, nil))
func pipelinePreflightCommand(newState func() *nativeLoaders, modules func() (pipeline.Registries, error)) commandkit.Command {
func pipelinePreflightCommand(version string, newState func() *nativeLoaders, modules func() (pipeline.Registries, error), options Options) commandkit.Command {
options = defaultOptions(options)
return commandkit.NewCommand(commandkit.CommandSpec[pipelineRunInput]{Name: "preflight", Summary: "Check readiness for a configured pipeline invocation.", Flags: runFlags(), Arguments: pipelineRunArguments(), CheckInput: checkPipelineRunInput,
Run: func(invocation commandkit.Invocation, input pipelineRunInput) commandkit.Outcome {
return executePreflight(invocation, input, newState(), modules)
return executePreflight(invocation, input, newState(), modules, version, options)
},
})
}
func executePreflight(invocation commandkit.Invocation, input pipelineRunInput, state *nativeLoaders, modules func() (pipeline.Registries, error)) commandkit.Outcome {
func executePreflight(invocation commandkit.Invocation, input pipelineRunInput, state *nativeLoaders, modules func() (pipeline.Registries, error), version string, options Options) commandkit.Outcome {
ctx := invocation.Context
loaded, failure := loadPipelineInvocation(ctx, invocation, input, state)
if failure != nil {
return *failure
}
report := preflightReport{Pipeline: input.PipelineID, Checks: []preflightCheck{}}
var capture *frameworkdebug.Recorder
add := func(id string, scope preflightScope, result, message string) {
report.Checks = append(report.Checks, preflightCheck{ID: id, Scope: scope, Result: result, Message: message})
}
finish := func(outcome commandkit.Outcome) commandkit.Outcome {
report.Ready = outcome.Status == 0
report.DebugDirectory = capture.Path()
if err := capture.Finalize(report, outcome.Cause); err != nil {
if outcome.Status == 0 {
outcome = commandkit.Failure("Could not complete debug capture.").WithCause(err)
} else {
outcome = outcome.WithCause(err)
}
}
if outcome.Status != 0 && capture.Path() != "" {
outcome = outcome.WithExplanation(outcome.Explanation.Message+runLocations("", capture.Path()), outcome.Explanation.Details...)
}
for _, check := range report.Checks {
if check.Result == "passed" {
_ = invocation.Reporter().Info(check.Message)
@@ -64,11 +78,39 @@ func executePreflight(invocation commandkit.Invocation, input pipelineRunInput,
}
if err := invocation.Reporter().Report(commandkit.Report{Value: report, TextTemplate: preflightTemplate}); err != nil {
if outcome.Status == 0 {
return commandkit.Failure("Could not present pipeline readiness.")
return commandkit.Failure("Could not present pipeline readiness." + runLocations("", capture.Path()))
}
}
return outcome
}
directory, err := input.Debug.directory(loaded.config.Debug.Directory)
if err != nil {
add("debug", preflightScope{Path: input.Debug.Directory.Value}, "failed", err.Error())
return finish(commandkit.Failure(err.Error()).WithCause(err))
}
if directory != "" {
startedAt := options.Now().UTC()
runID, err := options.RunIDGenerator(startedAt)
if err == nil {
err = validateRunID(runID)
}
if err == nil {
capture, err = startRunDebug(input.Debug, loaded.config.Debug.Directory, runID, startedAt)
}
if err != nil {
add("debug", preflightScope{Path: directory}, "failed", err.Error())
return finish(commandkit.Failure(err.Error()).WithCause(err))
}
add("debug", preflightScope{Path: capture.Path()}, "passed", "Debug capture is accessible.")
if err := capture.WriteJSON("context.json", map[string]any{"application_version": version, "pipeline": input.PipelineID, "input_path": input.InputFile.Value, "run_id": runID, "started_at": startedAt, "lanes": input.Lanes, "references": input.References, "unbinds": input.Unbinds, "resources": input.Overrides.Resources, "execution_overrides": input.Overrides.Execution}); err != nil {
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
return finish(commandkit.Failure(err.Error()).WithCause(err))
}
if err := captureRunConfiguration(capture, loaded.result); err != nil {
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
return finish(commandkit.Failure(err.Error()).WithCause(err))
}
}
selected, failure := selectPipelineInvocation(ctx, loaded, input, modules)
if failure != nil {
add("selection", preflightScope{Pipeline: input.PipelineID}, "failed", failure.Explanation.Message)
@@ -114,19 +156,33 @@ func executePreflight(invocation commandkit.Invocation, input pipelineRunInput,
add("targets", preflightScope{Pipeline: input.PipelineID}, "failed", err.Error())
return finish(pipelineResourceFailure(err))
}
observations, err := preflight.RunSmokeChecks(ctx, selected.engine, targets, input.Overrides.Execution.Execution, input.Overrides.Execution.TransientRetries)
evidence := newRunEvidenceRecorder(capture, loaded.result)
evidenceIndex := 0
observations, err := preflight.RunSmokeChecksObserved(ctx, selected.engine, targets, input.Overrides.Execution.Execution, input.Overrides.Execution.TransientRetries, func(observation preflight.SmokeObservation) error {
if observation.Report != nil {
err := evidence.WriteJSON(fmt.Sprintf("smoke/%03d.json", evidenceIndex), observation.Report)
evidenceIndex++
return err
}
return nil
})
failedCheck := false
for _, observation := range observations {
if !observation.Applicable {
continue
}
scope := preflightScope{Profile: observation.ProfileID, Backend: observation.BackendID, Bindings: observation.Bindings}
if observation.Err != nil {
failedCheck = true
add(string(observation.Kind), scope, "failed", observation.Err.Error())
} else {
add(string(observation.Kind), scope, "passed", fmt.Sprintf("%s check passed for profile %q.", observation.Kind, observation.ProfileID))
}
}
if err != nil {
if !failedCheck {
add("debug", preflightScope{Path: capture.Path()}, "failed", err.Error())
}
return finish(commandkit.Failure(err.Error()).WithCause(err))
}
return finish(commandkit.Success())