diff --git a/cmd/analyze/analyze.go b/cmd/analyze/analyze.go index 9bde5c84b8..535c0d08f3 100644 --- a/cmd/analyze/analyze.go +++ b/cmd/analyze/analyze.go @@ -51,12 +51,7 @@ var AnalyzeCmd = &cobra.Command{ os.Exit(1) } - analysisErrors := config.RunAnalysis() - if len(analysisErrors) != 0 { - for _, err := range analysisErrors { - color.Red("Error: %s", err) - } - } + config.RunAnalysis() if explain { err := config.GetAIResults(output, anonymize) diff --git a/cmd/root.go b/cmd/root.go index fd8518ce51..f09bafdac6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,12 +14,10 @@ limitations under the License. package cmd import ( - "fmt" "os" "path/filepath" "github.com/adrg/xdg" - "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/cmd/analyze" "github.com/k8sgpt-ai/k8sgpt/cmd/auth" "github.com/k8sgpt-ai/k8sgpt/cmd/filters" @@ -116,14 +114,7 @@ func performConfigMigrationIfNeeded() { err = util.EnsureDirExists(configDir) cobra.CheckErr(err) - if oldConfigExists && newConfigExists { - fmt.Fprintln(os.Stderr, color.RedString("Warning: Legacy config file at `%s` detected! This file will be ignored!", oldConfig)) - return - } - if oldConfigExists && !newConfigExists { - fmt.Fprintln(os.Stderr, color.RedString("Performing config file migration from `%s` to `%s`", oldConfig, newConfig)) - err = os.Rename(oldConfig, newConfig) cobra.CheckErr(err) } diff --git a/pkg/analysis/analysis.go b/pkg/analysis/analysis.go index 3487f304c5..f496b4dde0 100644 --- a/pkg/analysis/analysis.go +++ b/pkg/analysis/analysis.go @@ -39,6 +39,7 @@ type Analysis struct { Client *kubernetes.Client AIClient ai.IAI Results []common.Result + Errors []string Namespace string Cache cache.ICache Explain bool @@ -46,6 +47,7 @@ type Analysis struct { } type AnalysisStatus string +type AnalysisErrors []string const ( StateOK AnalysisStatus = "OK" @@ -53,6 +55,7 @@ const ( ) type JsonOutput struct { + Errors AnalysisErrors `json:"errors"` Status AnalysisStatus `json:"status"` Problems int `json:"problems"` Results []common.Result `json:"results"` @@ -113,7 +116,7 @@ func NewAnalysis(backend string, language string, filters []string, namespace st }, nil } -func (a *Analysis) RunAnalysis() []error { +func (a *Analysis) RunAnalysis() { activeFilters := viper.GetStringSlice("active_filters") analyzerMap := analyzer.GetAnalyzerMap() @@ -125,7 +128,6 @@ func (a *Analysis) RunAnalysis() []error { AIClient: a.AIClient, } - var errorList []error semaphore := make(chan struct{}, a.MaxConcurrency) // if there are no filters selected and no active_filters then run all of them if len(a.Filters) == 0 && len(activeFilters) == 0 { @@ -139,7 +141,7 @@ func (a *Analysis) RunAnalysis() []error { results, err := analyzer.Analyze(analyzerConfig) if err != nil { mutex.Lock() - errorList = append(errorList, fmt.Errorf(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err))) + a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err))) mutex.Unlock() } mutex.Lock() @@ -150,7 +152,6 @@ func (a *Analysis) RunAnalysis() []error { } wg.Wait() - return errorList } semaphore = make(chan struct{}, a.MaxConcurrency) // if the filters flag is specified @@ -166,7 +167,7 @@ func (a *Analysis) RunAnalysis() []error { results, err := analyzer.Analyze(analyzerConfig) if err != nil { mutex.Lock() - errorList = append(errorList, fmt.Errorf(fmt.Sprintf("[%s] %s", filter, err))) + a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("[%s] %s", filter, err))) mutex.Unlock() } mutex.Lock() @@ -175,11 +176,10 @@ func (a *Analysis) RunAnalysis() []error { <-semaphore }(analyzer, filter) } else { - errorList = append(errorList, fmt.Errorf(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter))) + a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter))) } } wg.Wait() - return errorList } var wg sync.WaitGroup @@ -195,7 +195,7 @@ func (a *Analysis) RunAnalysis() []error { results, err := analyzer.Analyze(analyzerConfig) if err != nil { mutex.Lock() - errorList = append(errorList, fmt.Errorf("[%s] %s", filter, err)) + a.Errors = append(a.Errors, fmt.Sprintf("[%s] %s", filter, err)) mutex.Unlock() } mutex.Lock() @@ -206,7 +206,6 @@ func (a *Analysis) RunAnalysis() []error { } } wg.Wait() - return errorList } func (a *Analysis) GetAIResults(output string, anonymize bool) error { diff --git a/pkg/analysis/output.go b/pkg/analysis/output.go index 7c852d4301..e4459660fd 100644 --- a/pkg/analysis/output.go +++ b/pkg/analysis/output.go @@ -44,6 +44,7 @@ func (a *Analysis) jsonOutput() ([]byte, error) { result := JsonOutput{ Problems: problems, Results: a.Results, + Errors: a.Errors, Status: status, } output, err := json.MarshalIndent(result, "", " ") @@ -55,6 +56,13 @@ func (a *Analysis) jsonOutput() ([]byte, error) { func (a *Analysis) textOutput() ([]byte, error) { var output strings.Builder + if len(a.Errors) != 0 { + output.WriteString("\n") + output.WriteString(color.YellowString("Warnings : \n")) + for _, aerror := range a.Errors { + output.WriteString(fmt.Sprintf("- %s\n", color.YellowString(aerror))) + } + } output.WriteString("\n") if len(a.Results) == 0 { output.WriteString(color.GreenString("No problems detected\n")) diff --git a/pkg/server/server.go b/pkg/server/server.go index 7796f30aa5..e65edbdaf1 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -75,15 +75,7 @@ func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) { return } - analysisErrors := config.RunAnalysis() - if analysisErrors != nil { - var errorMessage string - for _, err := range analysisErrors { - errorMessage += err.Error() + "\n" - } - http.Error(w, errorMessage, http.StatusInternalServerError) - health.Failure++ - } + config.RunAnalysis() if explain { err := config.GetAIResults(s.Output, anonymize)