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