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 <[email protected]>
  • Loading branch information
banjoh committed Sep 13, 2024
1 parent 4aa9b6f commit b831eea
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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, 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
Expand Down
2 changes: 0 additions & 2 deletions pkg/supportbundle/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", " ")
Expand Down
2 changes: 1 addition & 1 deletion pkg/supportbundle/supportbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
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 b831eea

Please sign in to comment.