Files
k8sgpt/pkg/server/server.go
Matthis 8b49f708f3 feat: rework output format (#368)
* feat: remove warning output

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: improves the error output to respect the format

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: rework errorsList

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* Revert "feat: rework errorsList"

This reverts commit 1a3cfa88ae.

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
2023-05-03 14:23:30 +01:00

131 lines
3.2 KiB
Go

/*
Copyright 2023 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
json "encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/analysis"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Config struct {
Port string
Backend string
Key string
Token string
Output string
maxConcurrency int
}
type Health struct {
Status string `json:"status"`
Success int `json:"success"`
Failure int `json:"failure"`
}
var health = Health{
Status: "ok",
Success: 0,
Failure: 0,
}
type Result struct {
Analysis []analysis.Analysis `json:"analysis"`
}
func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
namespace := r.URL.Query().Get("namespace")
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")
var err error
s.maxConcurrency, err = strconv.Atoi(r.URL.Query().Get("maxConcurrency"))
if err != nil {
s.maxConcurrency = 10
}
s.Output = r.URL.Query().Get("output")
if s.Output == "" {
s.Output = "json"
}
config, err := analysis.NewAnalysis(s.Backend, language, []string{}, namespace, nocache, explain, s.maxConcurrency)
if err != nil {
health.Failure++
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
config.RunAnalysis()
if explain {
err := config.GetAIResults(s.Output, anonymize)
if err != nil {
health.Failure++
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
out, err := config.PrintOutput(s.Output)
if err != nil {
health.Failure++
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
health.Success++
fmt.Fprintf(w, string(out))
}
func (s *Config) Serve() error {
handler := loggingMiddleware(http.DefaultServeMux)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/analyze", s.analyzeHandler)
http.HandleFunc("/healthz", s.healthzHandler)
color.Green("Starting server on port %s", s.Port)
err := http.ListenAndServe(":"+s.Port, handler)
if err != nil {
fmt.Printf("error starting server: %s\n", err)
return err
}
return nil
}
func (s *Config) healthzHandler(w http.ResponseWriter, r *http.Request) {
js, err := json.MarshalIndent(health, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(js))
}
func getBoolParam(param string) bool {
b, err := strconv.ParseBool(strings.ToLower(param))
if err != nil {
// Handle error if conversion fails
return false
}
return b
}