feat: initial json implementation (#68)

Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
This commit is contained in:
Alex Jones
2023-03-24 22:10:30 +00:00
committed by GitHub
parent 90a30f38bd
commit 979f13f043
5 changed files with 56 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ package find
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"os" "os"
@@ -16,6 +17,7 @@ import (
var ( var (
explain bool explain bool
backend string backend string
output string
) )
// problemsCmd represents the problems command // problemsCmd represents the problems command
@@ -61,10 +63,27 @@ var problemsCmd = &cobra.Command{
// Get kubernetes client from viper // Get kubernetes client from viper
client := viper.Get("kubernetesClient").(*kubernetes.Client) 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) color.Red("Error: %v", err)
os.Exit(1) 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") problemsCmd.Flags().BoolVarP(&explain, "explain", "e", false, "Explain the problem to me")
// add flag for backend // add flag for backend
problemsCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") 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) FindCmd.AddCommand(problemsCmd)
} }

8
pkg/analyzer/analysis.go Normal file
View File

@@ -0,0 +1,8 @@
package analyzer
type Analysis struct {
Kind string
Name string
Error string
Details string
}

View File

@@ -7,13 +7,14 @@ import (
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
) )
func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error {
err := AnalyzePod(ctx, client, aiClient, explain)
err := AnalyzePod(ctx, client, aiClient, explain, analysisResults)
if err != nil { if err != nil {
return err return err
} }
err = AnalyzeReplicaSet(ctx, client, aiClient, explain) err = AnalyzeReplicaSet(ctx, client, aiClient, explain, analysisResults)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -15,7 +15,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 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 // search all namespaces for pods that are not running
list, err := client.GetClient().CoreV1().Pods("").List(ctx, metav1.ListOptions{}) 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 { for key, value := range brokenPods {
fmt.Printf("%s: %s: %s\n", color.CyanString("%d", count), color.YellowString(key), color.RedString(value[0])) inputValue := strings.Join(value, " ")
count++ var currentAnalysis = Analysis{
Kind: "Pod",
Name: key,
Error: value[0],
}
if explain { if explain {
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner
s.Start() s.Start()
inputValue := strings.Join(value, " ")
// Check for cached data // Check for cached data
sEnc := base64.StdEncoding.EncodeToString([]byte(inputValue)) sEnc := base64.StdEncoding.EncodeToString([]byte(inputValue))
// find in viper cache // 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) color.Red("error decoding cached data: %v", err)
continue continue
} }
currentAnalysis.Details = string(output)
color.Green(string(output)) *analysisResults = append(*analysisResults, currentAnalysis)
continue continue
} }
@@ -110,9 +111,10 @@ func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI,
return err return err
} }
} }
currentAnalysis.Details = response
color.Green(response)
} }
*analysisResults = append(*analysisResults, currentAnalysis)
} }
return nil return nil

View File

@@ -15,7 +15,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 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 // search all namespaces for pods that are not running
list, err := client.GetClient().AppsV1().ReplicaSets("").List(ctx, metav1.ListOptions{}) 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 { for key, value := range brokenRS {
fmt.Printf("%s: %s: %s\n", color.CyanString("%d", count), color.YellowString(key), color.RedString(value[0])) var currentAnalysis = Analysis{
count++ Kind: "ReplicaSet",
Name: key,
Error: value[0],
}
if explain { if explain {
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner
s.Start() s.Start()
@@ -65,8 +68,8 @@ func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient
color.Red("error decoding cached data: %v", err) color.Red("error decoding cached data: %v", err)
continue continue
} }
currentAnalysis.Details = string(output)
color.Green(string(output)) *analysisResults = append(*analysisResults, currentAnalysis)
continue continue
} }
@@ -83,7 +86,9 @@ func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient
return err return err
} }
} }
currentAnalysis.Details = response
} }
*analysisResults = append(*analysisResults, currentAnalysis)
} }
return nil return nil