github.com/verrazzano/verrazzano@v1.7.0/tools/vz/pkg/analysis/main.go (about)

     1  // Copyright (c) 2021, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package analysis
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/verrazzano/verrazzano/tools/vz/pkg/analysis/internal/util/cluster"
     9  	"github.com/verrazzano/verrazzano/tools/vz/pkg/analysis/internal/util/report"
    10  	"github.com/verrazzano/verrazzano/tools/vz/pkg/helpers"
    11  	"go.uber.org/zap"
    12  )
    13  
    14  var analyzerTypeFunctions = map[string]func(log *zap.SugaredLogger, args string) (err error){
    15  	"cluster": cluster.RunAnalysis,
    16  }
    17  
    18  var analyzerType = "cluster" //Currently does only cluster analysis
    19  var includeInfo = true
    20  var includeSupport = true
    21  var includeActions = true
    22  var minImpact int
    23  var minConfidence int
    24  var logger *zap.SugaredLogger
    25  
    26  // The analyze tool will analyze information which has already been captured from an environment
    27  func AnalysisMain(vzHelper helpers.VZHelper, directory string, reportFile string, reportFormat string, printReportToConsole bool) error {
    28  	logger = zap.S()
    29  	return handleMain(vzHelper, directory, reportFile, reportFormat, printReportToConsole)
    30  }
    31  
    32  // handleMain is where the main logic is at, separated here to allow for more test coverage
    33  func handleMain(vzHelper helpers.VZHelper, directory string, reportFile string, reportFormat string, printReportToConsole bool) error {
    34  	// TODO: how we surface different analysis report types will likely change up, for now it is specified here, and it may also
    35  	// make sense to treat all cluster dumps the same way whether single or multiple (structure the dumps the same way)
    36  	// We could also have different types of report output formats as well. For example, the current report format is
    37  	// presentin issues/actions/supporting data which would be used by someone with technical background to resolve an issue
    38  	// in their environment. We also could generate a more detailed "bug-report-type" which someone could call which would
    39  	// gather up information, sanitize it in a way that it could be sent along to someone else for further analysis, etc...
    40  
    41  	// Call the analyzer for the type specified
    42  	err := Analyze(logger, analyzerType, directory)
    43  	if err != nil {
    44  		fmt.Fprintf(vzHelper.GetOutputStream(), "Analyze failed with error: %s, exiting.\n", err.Error())
    45  		return fmt.Errorf("\nanalyze failed with error: %s, exiting", err.Error())
    46  	}
    47  	reportContext := helpers.ReportCtx{ReportFile: reportFile, ReportFormat: reportFormat, IncludeSupportData: includeSupport, IncludeInfo: includeInfo, IncludeActions: includeActions, MinConfidence: minConfidence, MinImpact: minImpact, PrintReportToConsole: printReportToConsole}
    48  
    49  	// Generate a report
    50  	err = report.GenerateHumanReport(logger, vzHelper, reportContext)
    51  	if err != nil {
    52  		fmt.Fprintf(vzHelper.GetOutputStream(), "\nReport generation failed, exiting.\n")
    53  		return fmt.Errorf("%s", err.Error())
    54  	}
    55  	return nil
    56  }
    57  
    58  // Analyze is exported for unit testing
    59  func Analyze(logger *zap.SugaredLogger, analyzerType string, rootDirectory string) (err error) {
    60  	// Call the analyzer for the type specified
    61  	analyzerFunc, ok := analyzerTypeFunctions[analyzerType]
    62  	if !ok {
    63  		return fmt.Errorf("Unknown analyzer type supplied: %s", analyzerType)
    64  	}
    65  	err = analyzerFunc(logger, rootDirectory)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	return nil
    70  }