From b831eea5bed35fb825270b0212f74edc56a8e6d6 Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Fri, 13 Sep 2024 12:07:28 +0100 Subject: [PATCH] Store analysis.json file in preflight bundle Signed-off-by: Evans Mungai --- pkg/constants/constants.go | 1 + pkg/preflight/run.go | 46 +++++++++++++++++++++++++----- pkg/supportbundle/collect.go | 2 -- pkg/supportbundle/supportbundle.go | 2 +- test/validate-preflight-e2e.sh | 4 +-- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 9a6e7f29d..adcdfdfd7 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -20,6 +20,7 @@ const ( LIB_TRACER_NAME = "github.com/replicatedhq/troubleshoot" TROUBLESHOOT_ROOT_SPAN_NAME = "ReplicatedTroubleshootRootSpan" EXCLUDED = "excluded" + ANALYSIS_FILENAME = "analysis.json" // Cluster Resources Collector Directories CLUSTER_RESOURCES_DIR = "cluster-resources" diff --git a/pkg/preflight/run.go b/pkg/preflight/run.go index 1202f6481..7f4919a30 100644 --- a/pkg/preflight/run.go +++ b/pkg/preflight/run.go @@ -3,6 +3,7 @@ package preflight import ( "bytes" "context" + "encoding/json" "fmt" "os" "os/signal" @@ -17,6 +18,7 @@ import ( troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" "github.com/replicatedhq/troubleshoot/pkg/collect" "github.com/replicatedhq/troubleshoot/pkg/constants" + "github.com/replicatedhq/troubleshoot/pkg/convert" "github.com/replicatedhq/troubleshoot/pkg/k8sutil" "github.com/replicatedhq/troubleshoot/pkg/types" "github.com/replicatedhq/troubleshoot/pkg/version" @@ -175,14 +177,9 @@ func RunPreflights(interactive bool, output string, format string, args []string return types.NewExitCodeError(constants.EXIT_CODE_CATCH_ALL, errors.New("no data was collected")) } - version, err := version.GetVersionFile() - if err != nil { - return errors.Wrap(err, "failed to get version file") - } - - err = collectorResults.SaveResult(bundlePath, constants.VERSION_FILENAME, bytes.NewBuffer([]byte(version))) + err = saveTSVersionToBundle(collectorResults, bundlePath) if err != nil { - return errors.Wrap(err, "failed to write version") + return errors.Wrap(err, "failed to save version file") } analyzeResults := []*analyzer.AnalyzeResult{} @@ -191,6 +188,10 @@ func RunPreflights(interactive bool, output string, format string, args []string if err != nil { return errors.Wrap(err, "failed to analyze support bundle") } + err = saveAnalysisResultsToBundle(collectorResults, analyzeResults, bundlePath) + if err != nil { + return errors.Wrap(err, "failed to save analysis results to bundle") + } } else { for _, res := range collectResults { analyzeResults = append(analyzeResults, res.Analyze()...) @@ -246,6 +247,37 @@ func RunPreflights(interactive bool, output string, format string, args []string return types.NewExitCodeError(exitCode, errors.New("preflights failed with warnings or errors")) } +func saveAnalysisResultsToBundle( + results collect.CollectorResult, analyzeResults []*analyzer.AnalyzeResult, bundlePath string, +) error { + data := convert.FromAnalyzerResult(analyzeResults) + analysis, err := json.MarshalIndent(data, "", " ") + if err != nil { + return err + } + + err = results.SaveResult(bundlePath, "analysis.json", bytes.NewBuffer(analysis)) + if err != nil { + return err + } + + return nil +} + +func saveTSVersionToBundle(results collect.CollectorResult, bundlePath string) error { + version, err := version.GetVersionFile() + if err != nil { + return err + } + + err = results.SaveResult(bundlePath, constants.VERSION_FILENAME, bytes.NewBuffer([]byte(version))) + if err != nil { + return err + } + + return nil +} + // Determine if any preflight checks passed vs failed vs warned // If all checks passed: 0 // If 1 or more checks failed: 3 diff --git a/pkg/supportbundle/collect.go b/pkg/supportbundle/collect.go index ce4c5121a..cfac2c75f 100644 --- a/pkg/supportbundle/collect.go +++ b/pkg/supportbundle/collect.go @@ -220,8 +220,6 @@ func findFileName(basename, extension string) (string, error) { } } -const AnalysisFilename = "analysis.json" - func getAnalysisFile(analyzeResults []*analyze.AnalyzeResult) (io.Reader, error) { data := convert.FromAnalyzerResult(analyzeResults) analysis, err := json.MarshalIndent(data, "", " ") diff --git a/pkg/supportbundle/supportbundle.go b/pkg/supportbundle/supportbundle.go index 3f5e0dad4..676046988 100644 --- a/pkg/supportbundle/supportbundle.go +++ b/pkg/supportbundle/supportbundle.go @@ -173,7 +173,7 @@ func CollectSupportBundleFromSpec( return nil, errors.Wrap(err, "failed to get analysis file") } - err = result.SaveResult(bundlePath, AnalysisFilename, analysis) + err = result.SaveResult(bundlePath, constants.ANALYSIS_FILENAME, analysis) if err != nil { return nil, errors.Wrap(err, "failed to write analysis") } diff --git a/test/validate-preflight-e2e.sh b/test/validate-preflight-e2e.sh index a92ccd2af..36141d500 100755 --- a/test/validate-preflight-e2e.sh +++ b/test/validate-preflight-e2e.sh @@ -68,7 +68,7 @@ if [ $EXIT_STATUS -ne 0 ]; then echo "preflight command failed" exit $EXIT_STATUS fi -ls -al $tmpdir + if ls $tmpdir/preflightbundle-*.tar.gz; then echo "preflight bundle exists" else @@ -76,6 +76,4 @@ else exit 1 fi -rm -rf "$tmpdir" - exit $EXIT_STATUS