Skip to content

Commit

Permalink
feat: updated api
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu>
  • Loading branch information
thschue committed Apr 12, 2023
1 parent b2e8add commit adae2ef
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 58 deletions.
2 changes: 2 additions & 0 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var ServeCmd = &cobra.Command{
Token: token,
}

fmt.Println(server)

err := server.Serve()
if err != nil {
color.Red("Error: %v", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/aquasecurity/trivy-operator v0.13.0
github.com/fatih/color v1.15.0
github.com/julienschmidt/httprouter v1.3.0
github.com/magiconair/properties v1.8.7
github.com/mittwald/go-helm-client v0.12.1
github.com/sashabaranov/go-openai v1.7.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
Expand Down
122 changes: 64 additions & 58 deletions pkg/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,108 +2,107 @@ package server

import (
"context"
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/analysis"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/schollz/progressbar/v3"
"github.com/spf13/viper"
"net/http"
"os"
"strings"
)

type K8sGPTServer struct {
Port string
Backend string
Key string
Token string
Output string
}

type Result struct {
Analysis []analyzer.Analysis `json:"analysis"`
Analysis []analysis.Analysis `json:"analysis"`
}

func (s *K8sGPTServer) analyzeHandler(w http.ResponseWriter, r *http.Request) {
namespace := r.URL.Query().Get("namespace")
ex := r.URL.Query().Get("explain")
explain := getBoolParam(r.URL.Query().Get("explain"))
anonymize := getBoolParam(r.URL.Query().Get("anonymize"))
nocache := getBoolParam(r.URL.Query().Get("nocache"))
language := r.URL.Query().Get("language")

// get ai configuration
var configAI ai.AIConfiguration
err := viper.UnmarshalKey("ai", &configAI)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}

explain := false
if len(configAI.Providers) == 0 {
color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth")
os.Exit(1)
}

if ex == "true" {
explain = true
var aiProvider ai.AIProvider
for _, provider := range configAI.Providers {
if s.Backend == provider.Name {
aiProvider = provider
break
}
}

output := Result{}
if aiProvider.Name == "" {
color.Red("Error: AI provider %s not specified in configuration. Please run k8sgpt auth", s.Backend)
os.Exit(1)
}

var aiClient ai.IAI
switch s.Backend {
case "openai":
aiClient = &ai.OpenAIClient{}
if err := aiClient.Configure(s.Token, "english"); err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}
default:
color.Red("Backend not supported")
aiClient := ai.NewClient(aiProvider.Name)
if err := aiClient.Configure(aiProvider.Password, aiProvider.Model, language); err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}

ctx := context.Background()
// Get kubernetes client from viper
client := viper.Get("kubernetesClient").(*kubernetes.Client)
// Analysis configuration
config := &analyzer.AnalysisConfiguration{

kubecontext := viper.GetString("kubecontext")
kubeconfig := viper.GetString("kubeconfig")
client, err := kubernetes.NewClient(kubecontext, kubeconfig)
if err != nil {
color.Red("Error initialising kubernetes client: %v", err)
os.Exit(1)
}

config := &analysis.Analysis{
Namespace: namespace,
Explain: explain,
AIClient: aiClient,
Client: client,
Context: ctx,
NoCache: nocache,
}

var analysisResults *[]analyzer.Analysis = &[]analyzer.Analysis{}
if err := analyzer.RunAnalysis(ctx, []string{}, config, client,
aiClient, analysisResults); err != nil {
err = config.RunAnalysis()
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}

fmt.Println(analysisResults)
if len(*analysisResults) == 0 {
fmt.Fprintf(w, "{ \"status\": \"OK\" }")
}

var bar = progressbar.Default(int64(len(*analysisResults)))
if !explain {
bar.Clear()
}
var printOutput []analyzer.Analysis

for _, analysis := range *analysisResults {

if explain {
parsedText, err := analyzer.ParseViaAI(ctx, config, aiClient, analysis.Error)
if err != nil {
// Check for exhaustion
if strings.Contains(err.Error(), "status code: 429") {
fmt.Fprintf(w, "Exhausted API quota. Please try again later")
os.Exit(1)
}
color.Red("Error: %v", err)
continue
}
analysis.Details = parsedText
bar.Add(1)
if explain {
err := config.GetAIResults(s.Output, anonymize)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}
printOutput = append(printOutput, analysis)

analysis.Error = analysis.Error[0:]
output.Analysis = append(output.Analysis, analysis)
}
j, err := json.MarshalIndent(output, "", " ")

output, err := config.JsonOutput()
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
}
fmt.Fprintf(w, "%s", j)
fmt.Fprintf(w, string(output))

}

Expand All @@ -116,3 +115,10 @@ func (s *K8sGPTServer) Serve() error {
}
return nil
}

func getBoolParam(param string) bool {
if param == "true" {
return true
}
return false
}

0 comments on commit adae2ef

Please sign in to comment.