diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index e6350902e3..c00aefe70a 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -44,6 +44,8 @@ var ServeCmd = &cobra.Command{ Token: token, } + fmt.Println(server) + err := server.Serve() if err != nil { color.Red("Error: %v", err) diff --git a/go.mod b/go.mod index 2e6b3ce7b7..7df99fe4a5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 24fb5fcd61..39c6e29a15 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/server/main.go b/pkg/server/main.go index a760349f99..ade7012bb6 100644 --- a/pkg/server/main.go +++ b/pkg/server/main.go @@ -2,17 +2,14 @@ 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 { @@ -20,90 +17,92 @@ type K8sGPTServer struct { 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)) } @@ -116,3 +115,10 @@ func (s *K8sGPTServer) Serve() error { } return nil } + +func getBoolParam(param string) bool { + if param == "true" { + return true + } + return false +}