diff --git a/cmd/find/problems.go b/cmd/find/problems.go index 2c6de8ec90..7a6c947e3d 100644 --- a/cmd/find/problems.go +++ b/cmd/find/problems.go @@ -2,6 +2,7 @@ package find import ( "context" + "encoding/json" "fmt" "os" @@ -16,6 +17,7 @@ import ( var ( explain bool backend string + output string ) // problemsCmd represents the problems command @@ -61,10 +63,27 @@ var problemsCmd = &cobra.Command{ // Get kubernetes client from viper client := viper.Get("kubernetesClient").(*kubernetes.Client) - if err := analyzer.RunAnalysis(ctx, client, aiClient, explain); err != nil { + var analysisResults *[]analyzer.Analysis = &[]analyzer.Analysis{} + if err := analyzer.RunAnalysis(ctx, client, aiClient, explain, analysisResults); err != nil { color.Red("Error: %v", err) os.Exit(1) } + for n, analysis := range *analysisResults { + + switch output { + case "json": + j, err := json.Marshal(analysis) + if err != nil { + color.Red("Error: %v", err) + os.Exit(1) + } + fmt.Println(string(j)) + default: + fmt.Printf("%s %s: %s \n%s\n", color.CyanString("%d", n), color.YellowString(analysis.Name), color.RedString(analysis.Error), color.GreenString(analysis.Details)) + + } + } + }, } @@ -73,6 +92,8 @@ func init() { problemsCmd.Flags().BoolVarP(&explain, "explain", "e", false, "Explain the problem to me") // add flag for backend problemsCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") + // output as json + problemsCmd.Flags().StringVarP(&output, "output", "o", "text", "Output format (text, json)") FindCmd.AddCommand(problemsCmd) } diff --git a/pkg/analyzer/analysis.go b/pkg/analyzer/analysis.go new file mode 100644 index 0000000000..b62792591c --- /dev/null +++ b/pkg/analyzer/analysis.go @@ -0,0 +1,8 @@ +package analyzer + +type Analysis struct { + Kind string + Name string + Error string + Details string +} diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 4e9b4cda03..595a27f051 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -7,13 +7,14 @@ import ( "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" ) -func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { - err := AnalyzePod(ctx, client, aiClient, explain) +func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error { + + err := AnalyzePod(ctx, client, aiClient, explain, analysisResults) if err != nil { return err } - err = AnalyzeReplicaSet(ctx, client, aiClient, explain) + err = AnalyzeReplicaSet(ctx, client, aiClient, explain, analysisResults) if err != nil { return err } diff --git a/pkg/analyzer/podAnalyzer.go b/pkg/analyzer/podAnalyzer.go index c8e1215b07..ec01d08650 100644 --- a/pkg/analyzer/podAnalyzer.go +++ b/pkg/analyzer/podAnalyzer.go @@ -15,7 +15,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { +func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error { // search all namespaces for pods that are not running list, err := client.GetClient().CoreV1().Pods("").List(ctx, metav1.ListOptions{}) @@ -66,16 +66,17 @@ func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, } - count := 0 for key, value := range brokenPods { - fmt.Printf("%s: %s: %s\n", color.CyanString("%d", count), color.YellowString(key), color.RedString(value[0])) - count++ + inputValue := strings.Join(value, " ") + var currentAnalysis = Analysis{ + Kind: "Pod", + Name: key, + Error: value[0], + } if explain { s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner s.Start() - inputValue := strings.Join(value, " ") - // Check for cached data sEnc := base64.StdEncoding.EncodeToString([]byte(inputValue)) // find in viper cache @@ -92,8 +93,8 @@ func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, color.Red("error decoding cached data: %v", err) continue } - - color.Green(string(output)) + currentAnalysis.Details = string(output) + *analysisResults = append(*analysisResults, currentAnalysis) continue } @@ -110,9 +111,10 @@ func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, return err } } + currentAnalysis.Details = response - color.Green(response) } + *analysisResults = append(*analysisResults, currentAnalysis) } return nil diff --git a/pkg/analyzer/rsAnalyzer.go b/pkg/analyzer/rsAnalyzer.go index a08a6d0179..bebde099f7 100644 --- a/pkg/analyzer/rsAnalyzer.go +++ b/pkg/analyzer/rsAnalyzer.go @@ -15,7 +15,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { +func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error { // search all namespaces for pods that are not running list, err := client.GetClient().AppsV1().ReplicaSets("").List(ctx, metav1.ListOptions{}) @@ -39,10 +39,13 @@ func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient } } - count := 0 for key, value := range brokenRS { - fmt.Printf("%s: %s: %s\n", color.CyanString("%d", count), color.YellowString(key), color.RedString(value[0])) - count++ + var currentAnalysis = Analysis{ + Kind: "ReplicaSet", + Name: key, + Error: value[0], + } + if explain { s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner s.Start() @@ -65,8 +68,8 @@ func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient color.Red("error decoding cached data: %v", err) continue } - - color.Green(string(output)) + currentAnalysis.Details = string(output) + *analysisResults = append(*analysisResults, currentAnalysis) continue } @@ -83,7 +86,9 @@ func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient return err } } + currentAnalysis.Details = response } + *analysisResults = append(*analysisResults, currentAnalysis) } return nil