From 035348d8a0d290ac26b42425945eaafe038cedc5 Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Wed, 3 Jan 2024 17:59:28 +0100 Subject: [PATCH] fix: no explain case, improved readability. (#825) Signed-off-by: bwplotka --- cmd/analyze/analyze.go | 17 +++++-- pkg/analysis/analysis.go | 98 +++++++++++++++++++++------------------- pkg/analysis/output.go | 8 +++- pkg/server/analyze.go | 6 ++- 4 files changed, 73 insertions(+), 56 deletions(-) diff --git a/cmd/analyze/analyze.go b/cmd/analyze/analyze.go index 9f202d2e1a..ba5d469b8a 100644 --- a/cmd/analyze/analyze.go +++ b/cmd/analyze/analyze.go @@ -44,9 +44,17 @@ var AnalyzeCmd = &cobra.Command{ provide you with a list of issues that need to be resolved`, Run: func(cmd *cobra.Command, args []string) { - // AnalysisResult configuration - config, err := analysis.NewAnalysis(backend, - language, filters, namespace, nocache, explain, maxConcurrency, withDoc) + // Create analysis configuration first. + config, err := analysis.NewAnalysis( + backend, + language, + filters, + namespace, + nocache, + explain, + maxConcurrency, + withDoc, + ) if err != nil { color.Red("Error: %v", err) os.Exit(1) @@ -55,8 +63,7 @@ var AnalyzeCmd = &cobra.Command{ config.RunAnalysis() if explain { - err := config.GetAIResults(output, anonymize) - if err != nil { + if err := config.GetAIResults(output, anonymize); err != nil { color.Red("Error: %v", err) os.Exit(1) } diff --git a/pkg/analysis/analysis.go b/pkg/analysis/analysis.go index eea5476c95..6f23ea487c 100644 --- a/pkg/analysis/analysis.go +++ b/pkg/analysis/analysis.go @@ -17,12 +17,10 @@ import ( "context" "errors" "fmt" - "os" "reflect" "strings" "sync" - "github.com/fatih/color" openapi_v2 "github.com/google/gnostic/openapiv2" "github.com/k8sgpt-ai/k8sgpt/pkg/ai" "github.com/k8sgpt-ai/k8sgpt/pkg/analyzer" @@ -65,17 +63,56 @@ type JsonOutput struct { Results []common.Result `json:"results"` } -func NewAnalysis(backend string, language string, filters []string, namespace string, noCache bool, explain bool, maxConcurrency int, withDoc bool) (*Analysis, error) { - var configAI ai.AIConfiguration - err := viper.UnmarshalKey("ai", &configAI) +func NewAnalysis( + backend string, + language string, + filters []string, + namespace string, + noCache bool, + explain bool, + maxConcurrency int, + withDoc bool, +) (*Analysis, error) { + // Get kubernetes client from viper. + kubecontext := viper.GetString("kubecontext") + kubeconfig := viper.GetString("kubeconfig") + client, err := kubernetes.NewClient(kubecontext, kubeconfig) + if err != nil { + return nil, fmt.Errorf("initialising kubernetes client: %w", err) + } + + // Load remote cache if it is configured. + cache, err := cache.GetCacheConfiguration() if err != nil { - color.Red("Error: %v", err) - os.Exit(1) + return nil, err + } + + if noCache { + cache.DisableCache() + } + + a := &Analysis{ + Context: context.Background(), + Filters: filters, + Client: client, + Namespace: namespace, + Cache: cache, + Explain: explain, + MaxConcurrency: maxConcurrency, + WithDoc: withDoc, + } + if !explain { + // Return early if AI use was not requested. + return a, nil + } + + var configAI ai.AIConfiguration + if err := viper.UnmarshalKey("ai", &configAI); err != nil { + return nil, err } - if len(configAI.Providers) == 0 && explain { - color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth") - os.Exit(1) + if len(configAI.Providers) == 0 { + return nil, errors.New("AI provider not specified in configuration. Please run k8sgpt auth") } // Backend string will have high priority than a default provider @@ -93,49 +130,16 @@ func NewAnalysis(backend string, language string, filters []string, namespace st } if aiProvider.Name == "" { - color.Red("Error: AI provider %s not specified in configuration. Please run k8sgpt auth", backend) - return nil, errors.New("AI provider not specified in configuration") + return nil, fmt.Errorf("AI provider %s not specified in configuration. Please run k8sgpt auth", backend) } aiClient := ai.NewClient(aiProvider.Name) if err := aiClient.Configure(&aiProvider, language); err != nil { - color.Red("Error: %v", err) - return nil, err - } - - ctx := context.Background() - // Get kubernetes client from viper - - kubecontext := viper.GetString("kubecontext") - kubeconfig := viper.GetString("kubeconfig") - client, err := kubernetes.NewClient(kubecontext, kubeconfig) - if err != nil { - color.Red("Error initialising kubernetes client: %v", err) return nil, err } - - // load remote cache if it is configured - cache, err := cache.GetCacheConfiguration() - if err != nil { - return nil, err - } - - if noCache { - cache.DisableCache() - } - - return &Analysis{ - Context: ctx, - Filters: filters, - Client: client, - AIClient: aiClient, - Namespace: namespace, - Cache: cache, - Explain: explain, - MaxConcurrency: maxConcurrency, - AnalysisAIProvider: backend, - WithDoc: withDoc, - }, nil + a.AIClient = aiClient + a.AnalysisAIProvider = aiProvider.Name + return a, nil } func (a *Analysis) RunAnalysis() { diff --git a/pkg/analysis/output.go b/pkg/analysis/output.go index f65503d4e4..b638b93bb0 100644 --- a/pkg/analysis/output.go +++ b/pkg/analysis/output.go @@ -58,8 +58,12 @@ func (a *Analysis) jsonOutput() ([]byte, error) { func (a *Analysis) textOutput() ([]byte, error) { var output strings.Builder - // Print the AI provider used for this analysis - output.WriteString(fmt.Sprintf("AI Provider: %s\n", color.YellowString(a.AnalysisAIProvider))) + // Print the AI provider used for this analysis (if explain was enabled). + if a.Explain { + output.WriteString(fmt.Sprintf("AI Provider: %s\n", color.YellowString(a.AnalysisAIProvider))) + } else { + output.WriteString(fmt.Sprintf("AI Provider: %s\n", color.YellowString("AI not used; --explain not set"))) + } if len(a.Errors) != 0 { output.WriteString("\n") diff --git a/pkg/server/analyze.go b/pkg/server/analyze.go index 204f6ba803..5c93d0154a 100644 --- a/pkg/server/analyze.go +++ b/pkg/server/analyze.go @@ -9,8 +9,8 @@ import ( ) func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) ( - *schemav1.AnalyzeResponse, - error, + *schemav1.AnalyzeResponse, + error, ) { if i.Output == "" { i.Output = "json" @@ -34,6 +34,8 @@ func (h *handler) Analyze(ctx context.Context, i *schemav1.AnalyzeRequest) ( int(i.MaxConcurrency), false, // Kubernetes Doc disabled in server mode ) + config.Context = ctx // Replace context for correct timeouts. + if err != nil { return &schemav1.AnalyzeResponse{}, err }