Skip to content

Commit

Permalink
Store analysis.json file in preflight bundle
Browse files Browse the repository at this point in the history
Signed-off-by: Evans Mungai <evans@replicated.com>
  • Loading branch information
banjoh committed Sep 13, 2024
1 parent 4aa9b6f commit 55484be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
46 changes: 39 additions & 7 deletions pkg/preflight/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package preflight
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
Expand All @@ -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"
Expand Down Expand Up @@ -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{}
Expand All @@ -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()...)
Expand Down Expand Up @@ -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, "version.json", 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
Expand Down
4 changes: 1 addition & 3 deletions test/validate-preflight-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ 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
echo "Failed to find collected preflight bundle"
exit 1
fi

rm -rf "$tmpdir"

exit $EXIT_STATUS

0 comments on commit 55484be

Please sign in to comment.