Skip to content

Commit

Permalink
feat: initial json implementation (#68)
Browse files Browse the repository at this point in the history
Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones committed Mar 24, 2023
1 parent 90a30f3 commit 979f13f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
23 changes: 22 additions & 1 deletion cmd/find/problems.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package find

import (
"context"
"encoding/json"
"fmt"
"os"

Expand All @@ -16,6 +17,7 @@ import (
var (
explain bool
backend string
output string
)

// problemsCmd represents the problems command
Expand Down Expand Up @@ -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))

}
}

},
}

Expand All @@ -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 changes: 8 additions & 0 deletions pkg/analyzer/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package analyzer

type Analysis struct {
Kind string
Name string
Error string
Details string
}
7 changes: 4 additions & 3 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 11 additions & 9 deletions pkg/analyzer/podAnalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions pkg/analyzer/rsAnalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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()
Expand All @@ -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
}

Expand All @@ -83,7 +86,9 @@ func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient
return err
}
}
currentAnalysis.Details = response
}
*analysisResults = append(*analysisResults, currentAnalysis)
}

return nil
Expand Down

0 comments on commit 979f13f

Please sign in to comment.