mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-14 14:22:47 +00:00
feat: initial json implementation (#68)
Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
}
|
||||
|
8
pkg/analyzer/analysis.go
Normal file
8
pkg/analyzer/analysis.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package analyzer
|
||||
|
||||
type Analysis struct {
|
||||
Kind string
|
||||
Name string
|
||||
Error string
|
||||
Details string
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user