another test

Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu>
This commit is contained in:
Thomas Schuetz
2023-04-02 17:45:15 +02:00
parent d246fc1ec2
commit c2bce75d82
7 changed files with 131 additions and 130 deletions

View File

@@ -2,6 +2,7 @@ package analysis
import (
"context"
"encoding/json"
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
@@ -21,15 +22,39 @@ type Analysis struct {
analysisResults []common.Result
}
func (a *Analysis) RunAnalysis() error {
func NewAnalysis(namespace string, noCache bool, explain bool, filters []string, aiProvider string) *Analysis {
var aiClient ai.IAI
var err error
ctx := context.Background()
client := viper.Get("kubernetesClient").(*kubernetes.Client)
if explain {
aiClient, err = ai.NewAIClient(aiProvider)
if err != nil {
fmt.Println("Error creating AI client: ", err)
}
}
return &Analysis{
Context: ctx,
Namespace: namespace,
NoCache: noCache,
Explain: explain,
Filters: filters,
Client: client,
AIClient: aiClient,
}
}
func (a *Analysis) RunAnalysis() error {
activeFilters := viper.GetStringSlice("active_filters")
analyzerList := analyzer.GetAnalyzerList()
// if there are no filters selected and no active_filters then run all of them
if len(a.Filters) == 0 && len(activeFilters) == 0 {
for _, al := range analyzerList {
thisanalysis, _ := analyzer.NewAnalyzer(al, a.Client, a.Context, a.Namespace)
thisanalysis, _ := analyzer.NewAnalyzer(al, a.Client, a.Context, a.Namespace, a.AIClient, a.Explain)
err := thisanalysis.Analyze()
if err != nil {
fmt.Println("Error running analysis: ", err)
@@ -44,7 +69,7 @@ func (a *Analysis) RunAnalysis() error {
for _, filter := range a.Filters {
for _, ali := range analyzerList {
if filter == ali {
thisanalysis, _ := analyzer.NewAnalyzer(ali, a.Client, a.Context, a.Namespace)
thisanalysis, _ := analyzer.NewAnalyzer(ali, a.Client, a.Context, a.Namespace, a.AIClient, a.Explain)
err := thisanalysis.Analyze()
if err != nil {
fmt.Println("Error running analysis: ", err)
@@ -63,3 +88,11 @@ func (a *Analysis) PrintAnalysisResult() {
fmt.Println(result)
}
}
func (a *Analysis) PrintJsonResult() {
output, err := json.MarshalIndent(a.analysisResults, "", " ")
if err != nil {
fmt.Println("Error marshalling json: ", err)
}
fmt.Println(string(output))
}