Skip to content

Commit

Permalink
fix: no explain case, improved readability. (#825)
Browse files Browse the repository at this point in the history
Signed-off-by: bwplotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Jan 3, 2024
1 parent 70c6892 commit 035348d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 56 deletions.
17 changes: 12 additions & 5 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
98 changes: 51 additions & 47 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions pkg/analysis/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions pkg/server/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down

0 comments on commit 035348d

Please sign in to comment.