Skip to content

Commit

Permalink
fix: no explain case, improved readability.
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 7969c9a
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)
}

Check warning on line 82 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L75-L82

Added lines #L75 - L82 were not covered by tests

// Load remote cache if it is configured.
cache, err := cache.GetCacheConfiguration()

Check warning on line 85 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L85

Added line #L85 was not covered by tests
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
return nil, err
}

Check warning on line 88 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L87-L88

Added lines #L87 - L88 were not covered by tests

if noCache {
cache.DisableCache()
}

Check warning on line 92 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L90-L92

Added lines #L90 - L92 were not covered by tests

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
}

Check warning on line 107 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L94-L107

Added lines #L94 - L107 were not covered by tests

var configAI ai.AIConfiguration
if err := viper.UnmarshalKey("ai", &configAI); err != nil {
return nil, err

Check warning on line 111 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L109-L111

Added lines #L109 - L111 were not covered by tests
}

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")

Check warning on line 115 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L114-L115

Added lines #L114 - L115 were not covered by tests
}

// 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)

Check warning on line 133 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L133

Added line #L133 was not covered by tests
}

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

Check warning on line 142 in pkg/analysis/analysis.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/analysis.go#L140-L142

Added lines #L140 - L142 were not covered by tests
}

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")))
}

Check warning on line 66 in pkg/analysis/output.go

View check run for this annotation

Codecov / codecov/patch

pkg/analysis/output.go#L61-L66

Added lines #L61 - L66 were not covered by tests

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.

Check warning on line 38 in pkg/server/analyze.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/analyze.go#L37-L38

Added lines #L37 - L38 were not covered by tests
if err != nil {
return &schemav1.AnalyzeResponse{}, err
}
Expand Down

0 comments on commit 7969c9a

Please sign in to comment.